Hey I am having some trouble with this one. My teacher asked us to make a program that asks the user to read a file (numbers.txt and words.txt) and output the sum. For the words.txt we have to tell the computer to skip over words and just use the numbers.
heres the numbers.txt
7
9
13
18
10
12
4
18
3
6
and the words.txt
Set1
7
9
13
Set2
18
10
12
4
Set3
18
3
6
and heres my program
whenever I run it, it keeps giving my 99 when the answer is 100. and im lost on the words.txt one. Please help!
heres the numbers.txt
7
9
13
18
10
12
4
18
3
6
and the words.txt
Set1
7
9
13
Set2
18
10
12
4
Set3
18
3
6
and heres my program
PHP:
#include <iostream>
#include <fstream>
#include <iomanip>
#include <sstream>
using namespace std;
int main()
{
string filename;
cout << "Please enter the filename: ";
getline(cin, filename);
ifstream file(filename.c_str());
int number;
int sum;
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
{
file >> number;
while (!file.eof()) //while the file hasn't ended
{
int num;
file >> num;
sum += num; //sum now = sum + num
}
cout << "The sum is: " << sum << endl;
}
}
whenever I run it, it keeps giving my 99 when the answer is 100. and im lost on the words.txt one. Please help!
Last edited by a moderator: