For Loops

Consider the program below. This program reads in 3 numbers and finds the total:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//Read in 3 numbers: find total, version 1
#include <iostream>
using namespace std;
void main() {
  int num1, num2, num3, total;
  cout<<"Enter a number:";
  cin>>num1;
  cout<<"Enter a number:";
  cin>>num2;
  cout<<"Enter a number:";
  cin>>num3;
  total=num1+num2+num3;
  cout<<"Total="<<total<<"\n";
  system("pause");
} //main
CODE
Enter a number:6
Enter a number:90
Enter a number:5
Total=101


Press any key to continue . . .

The program works, and gives the right total. If we want to read in and find the total of 2 or 3 numbers, using a loop may not make much difference. But what if we want to read in and find the total of 10 numbers or 100?

The first step is to look at the program above and rewrite it so that it uses just 2 variables.

NEXT: Read number and add to total 3 times