## Volestar Newbie Poster

Ok, I've been working on this for about 4 hours, but I cannot get it to work! I'm starting with a char, then I need it to become a const char so I can use strcmp in my if statements. If there's anything wrong in that idea, please tell me what I have to do. If there's nothing wrong there, here's my code:

``````//Simple calculator.
#include <iostream>
#include <cmath>
#include <string>
using namespace std;
int calculate(int,const char,int);

int main()
{
using namespace std;
int a,b;
char operation;
cout << "Operations: +, -, *, /" << endl;
cout << "Number 1: ";
cin >> a;
cout << "Operation: ";
cin >> operation;
const char op = "";
strcpy(op,operation);
cout << "Number 2: ";
cin >> b;
cout << endl;
cout << "The answer is " << calculate(a,op,b);
cout << "Press any key to close this program.";
system("pause > nul");
return 0;
}

int calculate(int a, const char operation, int b)
{
using namespace std;
if(operation == "+")
{
return a + b;
}
if(operation == "-")
{
return a - b;
}
if(operation == "*")
{
return a * b;
}
if(operation == "/")
{
return a / b;
}
}``````

## sheff_cc

``````#include <iostream>
#include <cmath>
#include <string>
using namespace std;
int calculate(int,const char,int);

int main()
{
using namespace std;
int a,b;
char operation;
cout << "Operations: +, -, *, /" << endl;
cout << "Number 1: ";
cin >> a;
cout << "Operation: ";
cin >> operation;
char op = operation;
cout << "Number 2: ";
cin >> b;
cout << endl;
cout << "The answer is " << calculate(a,op,b);
cout << "\nPress any key to close this program.";
system("pause > nul");
return 0;
}

int calculate(int a,char operation, int b)
{
using namespace std;
if(operation == '+')
{
return a + b;
}
if(operation == '-')
{
return a - b;
}
if(operation == '*')
{
return a * b;
}
if(operation == '/')
{
return a / b;
}
}``````

## sheff_cc

The op just need to store one character,so you don't need to use char[],and to use strcpy().
For strcpy(char*,const char*),if you want use it,you must transform the proper arguments into the function.

## WaltP 2,905

A one-character array is not a c-string. `strcpy()` only works on null-terminated strings. As sheff said, there is no need to define a 1-element array. Just define the value as a `char` and copy it to another `char` with =.

## EngSara -2

the array has positions from 0 to size-1. so if u declare operation then the array have one element which is operation not operation.

commented: How many times do we have to tell him this? Third time's the charm? -2

## Volestar

``````#include <iostream>
#include <cmath>
#include <string>
using namespace std;
int calculate(int,const char,int);

int main()
{
using namespace std;
int a,b;
char operation;
cout << "Operations: +, -, *, /" << endl;
cout << "Number 1: ";
cin >> a;
cout << "Operation: ";
cin >> operation;
char op = operation;
cout << "Number 2: ";
cin >> b;
cout << endl;
cout << "The answer is " << calculate(a,op,b);
cout << "\nPress any key to close this program.";
system("pause > nul");
return 0;
}

int calculate(int a,char operation, int b)
{
using namespace std;
if(operation == '+')
{
return a + b;
}
if(operation == '-')
{
return a - b;
}
if(operation == '*')
{
return a * b;
}
if(operation == '/')
{
return a / b;
}
}``````

Thanks for the help! What if I wanted 2 characters, for example, **?

## WaltP 2,905

Thanks for the help! What if I wanted 2 characters, for example, **?

Does this question have anything to do with your program? Or are you just asking?

What is it you really want to do?