Automatically generated variables in loops?

Hi Guys and Girls,

Just started C++ not long ago, and I want to make a console app based calculator to test my new knowledge. To save a whole heap of coding I'm going to try and use a repeat... let me explain in detail...

When you start the app there is the welcome message etc
Then after pressing enter you get to the actual calculator...
It starts by asking you how many numbers your going to work with - this will generate the amount of times to repeat the code...

Now, this is what happens...
The calculator shows a message saying "Number: " and then "1" (this changes as you go on) Then it minuses/pluses the repeat operators... then loops.

What I'd like it to do is:
Say Number: #
Then create a variable called number1
Take an input for variable number1
(The rest I can deal with... getting and input for an operator eg + or - or *)
... Then Repeat
Saving the variable number1 and replacing it with variable number2
That's where the generation of variables comes in...
(If you can show me the concept I'll adapt it for the operators)

I hope that's clear enough... if not please say... I'll be truly greatful if someone helps as I'm teaching myself and there is no one to turn to.

Thanks.
Oh and this is what I have so far:

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
30
31
32
33
/* 
    calc_rev2
*/
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;

int main(int nNumberofArgs, char* pszArgs[])
{
   //Introduction stuff here (left it out for now...
        
   //Start of calc - check how many number will be used...
   int operationamount;
   cout << "How many numbers will be used in you calculation?\n";
   cin  >> operationamount;
   
   //loop to collect variables
   int numcount = 1;
   while (operationamount > 0)
   {
   cout << "\nNumber" << numcount << ": " << "\n";
   
   //need my generation of variables and cin code here?!?!?

   operationamount = operationamount - 1;
   numcount = numcount + 1;
   }
   
   //end
   system("PAUSE");
   return 0;
}


Once again thanks if you can either help me or lead me in the right direction (links to help)..
Please also let me know if I'm doing any bad habits in the code above...
I heard something about system("PAUSE") not being good... I'm not sure...
Last edited on
1
2
3
4
5
6
7
8
9
//you mean this?
double number;
cin >> number;
/*
 * Also, it seems you ask for an initial number
 * and then another number to preform the operations on.
 * The initial number only needs to be seeded once,
 * so its code need not be in a loop.
 */
Last edited on
Right not entirely sure what you meant...

I need to have a variable that dynamically creates more variables during the loop...
This is because if I set my input variable to be "number" and store the first number input... then when it repeats it will overwrite "number" and I have lost my first input.

I think its a job for arrays....

Please explain your post right down as if your talking to kid, because that's what level I'm at :P

Cheers.
Accumulate the values in something like a vector.

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
30
31
/* 
    calc_rev2
*/
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <vector>
using namespace std;

int main(int nNumberofArgs, char* pszArgs[])
{
   //Introduction stuff here (left it out for now...
        
   //Start of calc - check how many number will be used...
   int operationamount;
   cout << "How many numbers will be used in you calculation?\n";
   cin  >> operationamount;
   
   vector<double> numbers;
   for (int i = 0; i < operationamount; ++i)
   {
      double num;
      cin >> num;
      numbers.push_back(num);
   }
   
   //end
   system("PAUSE");
   return 0;
}
Why do you need to keep track of every number and perform the operations at the last moment? Shouldn't you do it as you go along, like a real 4-function calculator?
Topic archived. No new replies allowed.