variables Questions

So say you're making a calculator (and i am), and you want to make the user input the first number, then the second number, then use the sum of the 2nd number to add the third input number, then use the sum of the three number and add it to the newer input, then...

OMG IS THERE ANOTHER WAY TO DO THIS???

like can you just make one or two or three variables to do this??? because, a normal calculator would be doing this but the coder of that program must can't have done something like...
1
2
3
4
5
6
7
8
cin>>1input;
cin>>2input;
cout<<1input + 2input;
cin>>3 input;
...
...
...
...

IT TAKES FOREVER AND dumb.

SO can you PLEASE help me on this weird thing???

and if the answer is a very very very beginner thing, please forgive me its like 23:13pm at the time posting...

THX YOU ALL FOR YOUR TIME.
the dude
Last edited on
I believe your problem could be solved with a for loop.
1
2
3
4
5
6
7
int a, b;
for(int i = 0; i < numberofproblems; i++)
{
     std::cin >> a;
     std::cin >> b;
     std::cout << a + b << std::endl;
}

Where numberofproblems is however many times you want to run it.
Last edited on
@gentleguy i think Too Explosives got the answer thank you

@Too Explosives the answer is really like i guessed "very very very beginner stuff" thx you

though i didn't try it out yet but i know it will work thank you again
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>

using namespace std;

int main()
{
    int number = 0;
    int running_total = 0;
    
    cout << "Please enter a number or any letter to quit: ";
    while(cin >> number)
    {
        running_total += number;
        cout << "Total so far: " << running_total << endl;
    }
    
    return 0;
}
Last edited on
and i always had a question about std::cin and other things... what are those for cant you just do something like cin>> and cout<< ?
@Thedudeplaysmc3 std::cin is just saying use cin which lives in the std namespace. I believe you are probably adding using namespace std; at the beginning of your code, which removes the need to put it behind each one.

Look at http://www.cplusplus.com/doc/oldtutorial/namespaces/ to learn about them.
Last edited on
closed account (48T7M4Gy)
@OP

Yep just use namespace std until you are comfortable using it. There are a lot of permutations in its use and quite often std:: as a prefix everywhere gets in the way of legible program writing. So take your pick.

I'll change mine in a second :) ... Done!
Last edited on
closed account (48T7M4Gy)
@OP give the thread a green tick if the job is done. That way nobody needs to revisit except for private study purposes, as one might. :)
closed account (48T7M4Gy)
If you want to progressively subtract then running_result -= number;
Topic archived. No new replies allowed.