Help with total sum.

Hey all, new user here. Im having a problem with one of my assignment questions, ill post the code in a sec. Basically the program has to ask the user for a set of integers and keep asking til the first number is greater than the second number or the input is invalid, now the problem im having is with counters.
I have to add every number entered then display a total and average of said numbers. This is what I've got 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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
using namespace std;

int main()
{
int firstNumber = 0;
int lastNumber = 0;
int sum = 0;
int total = 0;
int average = 0;

cout << "Please enter a pair of integer numbers: ";
cin >> firstNumber;
cin >> lastNumber;
if(cin.fail())                                     //checks whether the input is valid(i.e a number).
{
    cout << "Invalid input." <<endl;              //displays "Invalid Input" if the value is anything other than a number.
    system("pause");
    return 0;
    cin.clear();
    cin.ignore();
}

while (firstNumber <= lastNumber)                 //loop allows the user to keep entering numbers as long as firstNumber is smaller than lastNumber.
{     
      cout << "Please enter another pair of integer numbers: ";
      cin >> firstNumber;
      cin >> lastNumber;
      {
          cout << "Invalid input." <<endl;              //displays "Invalid Input" if the value is anything other than a number.
           system("pause");
           return 0;
           cin.clear();
           cin.ignore();
    }
      total += (firstNumber + lastNumber);
      sum++;
      
      
}

      
                                    
total = sum;                 //this calculates the average of stored numbers. 
average = total / sum;
cout << "The total is: " << total << endl;
cout << "The average is: " << average << endl;


system("pause");
return 0;

}


Everything works fine, even the counter, BUT it doesnt work as I intend it to, because it only counts how many (i.e 10 numbers) numbers were entered, not the sum of the entered numbers.

Thanks in advance.
Last edited on
Look at line 44, total = sum basically kills all your hard work in getting your nice total by assigning the value of the counter to it.
Also, you will miss the values of the first pair of numbers entered since your total only kicks in after the first loop, even if the input from the first pair is valid.
That's the problem.
If total = sum and average = total/sum, average is always 1.
Ah I see. I've got it now.

Thanks for the help :)
Topic archived. No new replies allowed.