Problems with numbers
Nov 18, 2013 at 5:57am UTC
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;
}
Nov 18, 2013 at 6:20am UTC
check your braces
Nov 18, 2013 at 7:00am UTC
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 Nov 18, 2013 at 7:01am UTC
Nov 18, 2013 at 7:48am UTC
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.
Nov 18, 2013 at 7:51am UTC
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.