Click here to Skip to main content
15,881,898 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, I am in school and this is the first time when i use C++. My teacher sent me a code for addition of 2 numbers. I copyed it, pasted it in c++ compiler but it doesn't give me what i want.
The code is this:

C++
#include <iostream>
using namespace std;
int main()
{
    int a,b,s;
    cout<<"a=";
    cin>>a;
    cout<<"b=";
    cin>>b;
    s=a+b;
    cout<<"a+b="<<s;
    return 0;
}



When i run it, it says:
/tmp/MHJO6VFZiA.o
a=

What I have tried:

Also, if i put a=1 or anything like that, after i run it it says:a=1
Posted
Updated 14-Apr-21 0:52am
v2
Comments
Richard MacCutchan 14-Apr-21 6:19am    
The program is waiting for you to enter the values for a and b.
Member 15151299 14-Apr-21 6:27am    
I did, but it doesn't work.
When i run it, it only reads "a"

/tmp/yIpmsW4M9l.o
a=1
Richard MacCutchan 14-Apr-21 6:32am    
When you see the message a= you need to reply with a number. this is the result I got when I ran the code:
C:\Users\rjmac\Documents\VSCode\C++>test
a=23
b=44
a+b=67

The values 23 and 44 are what I typed in.
Member 15151299 14-Apr-21 6:42am    
Now i understand, i wasn't replying with a number. Thank you soooo much.
Richard MacCutchan 14-Apr-21 6:36am    
I just tried on that website and it works OK.

Your code compiles and runs fine.
I made a test on my Linux box:
$ g++ -Wall add.cpp
$ ./a.out 
a=1
b=2
a+b=3$ 
 
Share this answer
 
Comments
Member 15151299 14-Apr-21 6:18am    
When i run this code on https://www.programiz.com/cpp-programming/online-compiler/
Is only shows:
/tmp/yIpmsW4M9l.o
a=1

Is the site the one causing my problem?
CPallini 14-Apr-21 6:31am    
Maybe. I suggest you to use a compiler on your local machine.
Anyway, I've just got
/tmp/S9Z8SCaE7w.o
a=1
b=2
a+b=3
on the same site.
I also just tried your code, using https://www.onlinegdb.com/online_c++_compiler[^] and it ran fine.
If I enter:
23
17
Then it prints "40" which is correct.
The only way I could get it to go wrong is not to enter an integer number: "a=1" or "10.5" give be the "wrong" result because there is no error checking in your code.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900