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[1];
    cout << "Operations: +, -, *, /" << endl;
    cout << "Number 1: ";
    cin >> a;
    cout << "Operation: ";
    cin >> operation[1];
    const char op[1] = "";
    strcpy(op[1],operation[1]);
    cout << "Number 2: ";
    cin >> b;
    cout << endl;
    cout << "The answer is " << calculate(a,op[1],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;
                    }
}

Just so you know, I'm a newbie at C++, so I don't understand anything about maps. Please help!

#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[1];
    cout << "Operations: +, -, *, /" << endl;
    cout << "Number 1: ";
    cin >> a;
    cout << "Operation: ";
    cin >> operation[1];
    char op = operation[1];
    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;
                    }
}

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.

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 =.

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

Comments
How many times do we have to tell him this? Third time's the charm?
#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[1];
    cout << "Operations: +, -, *, /" << endl;
    cout << "Number 1: ";
    cin >> a;
    cout << "Operation: ";
    cin >> operation[1];
    char op = operation[1];
    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, **?

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?

This article has been dead for over six months. Start a new discussion instead.