My goal here is to read a file (word.txt) and then output them added. the challenge is to skip over the lines that have words in them and just add the numbers. I really need help with this part and where to put it in my program
Code:
#include <iostream>
#include <fstream>
#include <iomanip>
#include <sstream>
using namespace std;
int main()
{
int n;
int total;
int count;
double average;
string filename;
cout << "Please enter the filename: ";
getline(cin, filename); //getting possibly more than one word for filename
cout << endl;
cout << "Opening " << filename << "." << endl;
ifstream file(filename.c_str());
if (!file.good()) //if the file isn't there
{
cout << "Sorry, I can't find that file." << endl;
return 0;
}
else //if the file is there
{
while (!file.eof()) //while the file hasn't ended
{
file >> n;
total += n; //sums all numbers
count++; //counts the numbers
}
average = total / count;
cout << "The sum is: " << total << endl;
cout << "The average is: " << average << endl;
cout << "The total amount of numbers is: " << count << endl;
}
}
Last edited by a moderator: