Program won't work?

Hi! So, I'm a beginner, so I don't have the ability to identify the mistake I made. Can someone please tell me what I did wrong(Please use beginner language)?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
  #include <iostream>
using namespace std;
int main()
{
 int op, fnum, snum, answer;
 cout << "Hello. This is a calculator.";
 cout << "\n1)Addition" "\n2)Subtraction";
 cout << "\nWhat operation will you like to begin:";
 cin >> op;
 if (op = 1){
    cout << string(3, '\n');
    cout << "First number:";
    cin >> fnum;
    cout << "\nSecond number:";
    cin >> snum;
    answer = fnum + snum;
    cout << "The answer is:" + (answer);
    return 0;
 }
 else if (op = 2)
    cout << string(3, '\n');
    cout << "First number:";
    cin >> fnum;
    cout << "\nSecond number:";
    cin >> snum;
    answer = fnum - snum;
    cout << "The answer is:" + (answer);
    return 0;
}
if (op = 1){

= is assignment
== is equality

and missing brackets for the else if block of code
Last edited on
In the future could you please describe what is wrong? As in what the program is doing currently and also what you want the program to do that it is not doing currently. Just saying find the problem is not usually going to get a good response on here.

Your problem is probably on lines 10 and 20, where you have used the assignment operator (single =) instead of the comparison operator (double ==)
metulburr,
Thanks! I didn't know about the = and == meanings.
kevinkjt2000,
Thank you! I'll make sure to do so next time. And thanks for the answer to my mistakes!
Oh, and when I build/run it, and I get my answer, instead of saying what it's supposed to say(The answer is:(Whatever the answer is)), it says "e answer is:" Anyone know?
Instead of the plus operator (+) on line 27, you might want the insertion operator (<<)
Nope. Didn't seem to fix it. :/
Same thing on line 17.

cout << "The answer is:" + (answer);
should be
cout << "The answer is:" << answer;.

In general, "+" won't work for concatenating something other than a std::string to a string literal (or C string in general).
So "Hello " + "world!" will not give you "Hello world!" (actually, it doesn't even compile for me), nor will "Hello" + 15 give you "Hello15".

(For the former, you can use std::string("Hello ") + "world!" to get what you want, and for the latter, you'll have to do something like "Hello" + std::to_string(15). But since you're just sending everything to cout, you don't even need to do any of that -- just use the << operator: std::cout << "Hello " << "world" << 15;.)
Last edited on
Thanks! Now it works!
Topic archived. No new replies allowed.