+ 1
Help with my first calculator?
So i've cleared the 1st module of the C++ course and under a topic i saw a "challenge" that requires me to create a calculator, but with the things i learnt so far i'm just able to give A and B specific numbers and then use the int "sum" to output a result. I'll link you what i meant down here, but for now i can't understand how to make a program that outputs the result of any given number, without having me to input the numbers along the codes. The challenge should have been for the 1st module of the course, so how come i'm so disoriented and confused? am i doing something wrong? thanks in advance. https://code.sololearn.com/c4yX6PBuQugJ
3 Respostas
+ 5
Change your code to this and note that when you run, you will not get two separate input boxes, just the one. Knowing that you need two values, enter the first one (a) and press enter, then enter the second one (b). You should then get your result.
int a; // declares a variable but does not set it to anything
int b; // as per above
cin >> a; // sets the 'a' variable based on user input
cin >> b; // as per above
int sum = a + b; // your original code
cout << sum; // your original code
return 0; // your original code
+ 4
Excellent, my pleasure
I'm guessing you are new to programming and not just c++? Don't be too hard on yourself, it takes time to get going. I first tried programming about 20years ago and some things I really struggled with and others were not too bad. I've never done programming full time and it's only in the last 6 months that I've really started coming back to it.
As for including different operators, eg minus, times and divide, you could keep it super simple and just add after:
cout << sum;
sum = a - b; // reassign a new value to the 'sum' variable.
cout << sum; // this will output your new sum
+ 1
Thanks a lot, that was really easy to do and you were very clear!!, but i don't understand how sometimes lines of code for doing certain things don't come in mind to me. And what if i want to subtract, divide and multiply too? should i have to add something like "int sum = a - b" ecc? or do i have to make major changes to the code? would i need different keywords like the "If" thing, like i saw in some guy's code? thanks a lot again man!