0

I want to create a program that reads a string containing two numbers and a operator and prints out the results. It is continuously showing error on arithmetic operators. For instance, how would I add two strings together?

int main()
{
    string number1;
    string number2;
    string operation;
    string answer;

    cout << "Enter numbers with respective operations";
    cout << "number 1";
    cin >> number1; 
    cout << "number2";
    cin >> number2;
    cout << "operation";
    cin >> operation;
    if (operation == "+")
    {
        answer = number1 + number2;
        cout << "the sum is   " << answer << endl;
    }
    else if (operation == "-")
    {
        answer = number1 - number2;
        cout << "the difference is   " << answer << endl;
    }
    else if (operation == "*")
    {
        answer = number1 * number2;
        cout << "the product is   " << answer << endl;
    }
    else if (operation == "/")
    {
        answer = number1 / number2;
        cout << "the answer is   " << answer << endl;
    }
    else
    {
        cout << "invalid input" << endl;
    }
    getchar();
    return 0;
} 
Ben Holland
  • 2,309
  • 4
  • 34
  • 50
Sumair
  • 11
  • 1
  • 2
    What are you expecting the result of a `string` divided by a `string` to be?? – Xirema Oct 11 '17 at 15:50
  • what are the errors? – 463035818_is_not_an_ai Oct 11 '17 at 15:50
  • according to your code there is no reason for the numbers to be strings, why are they ? since they are strings you need to cast them to numbers if you want to calculate with them. – xyious Oct 11 '17 at 15:51
  • why do you use `string`s when you want `double`s ? – 463035818_is_not_an_ai Oct 11 '17 at 15:51
  • 1
    You have to first [convert the string to a numeric type](https://stackoverflow.com/questions/5290089/how-to-convert-a-number-to-string-and-vice-versa-in-c) and then do the calculations on that value. – Bo Persson Oct 11 '17 at 15:52
  • Could be an [XY Problem](http://xyproblem.info/). What are you _actually_ trying to do? – Jabberwocky Oct 11 '17 at 16:10
  • 1
    @xyious -- you can't **cast** a string to a number; you can **convert** it. A cast is something you write in your source code to tell the compiler to do a conversion. – Pete Becker Oct 11 '17 at 16:10
  • Welcome to Stack Overflow! To help people answer your question, you'll need to be more specific about the error. Please [edit] your post to incorporate the exact errors you get from your [mcve] (preferably using copy+paste to avoid transcription errors). – Toby Speight Oct 11 '17 at 17:42
  • This question is unclear because, among other things, you do not provide an explanation for your choice to read in strings instead of some kind of data type which can actually be used for calculation/arithmetic. – Yunnosch Oct 11 '17 at 20:13
  • I am creating a program that reads a string containing two numbers and a operator and print out the results – Sumair Oct 12 '17 at 11:04

2 Answers2

5

You need to change the input types to numeric types, so you read actual numbers, not their string representations. As you can see here, the only one of those operators that string overloads is + - and that's for concatenation of strings.

Changing...

string number1;
string number2;
string answer;

...to...

double number1;
double number2;
double answer;

...should fix your issue.

Alternatively you can read strings as you do now then convert them to numbers (see here), but that's just adding more work when you don't need it. Unless you want to detect invalid numeric values (like 1234abc8), in which case reading a string then parsing and checking for invalid input is a good idea.

hnefatl
  • 5,860
  • 2
  • 27
  • 49
  • yeah I am always 1 second late, but eventually I gave up to update my comments ;) – 463035818_is_not_an_ai Oct 11 '17 at 15:58
  • I can do it by changing string into double data type. But this is an assignment question that taking two operand and one operator via string and perform basic calculating operation. – Sumair Oct 12 '17 at 10:47
  • @Sumair Then take a look at [this](https://stackoverflow.com/questions/4754011/c-string-to-double-conversion), to do the conversion. – hnefatl Oct 12 '17 at 11:19
0

Both number1 and number2 in your example are strings, which do not support adding values together. You could simply switch the types of number1 and number2 as well as answer to ints:

int main()
{
    float number1;
    float number2;
    string operation; // could switch this to char too
    float answer;
    // ...

If you really want to use strings you could use stringstreams contained in <sstream> : http://www.cplusplus.com/reference/sstream/stringstream/stringstream/

Edit1: float might be the better type if you care about post floating point values.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
Folling
  • 133
  • 2
  • 13
  • `int`s will probably not produce the results that OP wants for division – 463035818_is_not_an_ai Oct 11 '17 at 15:59
  • basically this is my assignment question in which I am facing difficulty. The question is. create a program that reads a string containing two numbers and a operator and print out the results. The string are to be interpreted by your programme. – Sumair Oct 12 '17 at 10:51
  • In that case, you will need to use stringstreams. – Folling Oct 12 '17 at 19:11