Problems with numbers

why does the total only shows the last number?

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
 #include <iostream>
#include <fstream>
using namespace std;
int main()
{
    ifstream inputfile;
    int number;
    int total = 0;
    inputfile.open("C:\\Users\\Caitlyn\\Desktop\\42.txt");
    if (inputfile) 
   
    while(inputfile >> number)
     {
                  cout << number << endl;  
                    }
                    
               
    {
          total +=number;
           cout << "total is: " << total << endl;
          
          }
          
               inputfile.close();
          system("pause");
          return 0;
          }
check your braces
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
	ifstream inputfile;
	int number;
	int total = 0;
	inputfile.open("C:\\Users\\Caitlyn\\Desktop\\42.txt");
	if (inputfile) 
        {
		while(inputfile >> number)
		{
			cout << number << endl;  
			total +=number;

		}
         }
		cout << "total is: " << total << endl;
		inputfile.close();
		system("pause");
		return 0;
}
Last edited on
the last thing i need to make the program do is tell me how many numbers there are and divide the total by the numbers to get an average is some kind of tutorial i can look up. i don't want to ask for the answer directly i want to try to figure it out.
May be make a count variable to keep track of the number of times in the loop then divide the total...
Topic archived. No new replies allowed.