Difficulty Reading and couting numeric data from a file

So i've copied a program from my textbook that reads numbers from a file and then displays them. However for some reason I keep getting large random numbers for my output on the consol. Please helpe me fix this. The text file "TonyNumbers.txt" has the numbers 1 through 5, each with their own line.

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
  
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
    ifstream inFile;
    int val1, val2, val3, sum;

    inFile.open("TonyNumbers.txt");

    //read three numbers from the file
    inFile >> val1;
    inFile >> val2;
    inFile >> val3;

    inFile.close();

    cout << val1 << val2 << val3;


    return 0;
}
1.
cout << val1 << val2 << val3;

Should be :
cout << val1 << ' ' << val2 << ' ' << val3 << endl;

2.
inFile.open("TonyNumbers.txt");

Should be :
1
2
3
4
5
6
7
8
9
10
string fileName("TonyNumbers.txt");
inFile.open(fileName.c_str());

while(!inFile.is_open())
{
    cout << "The file " << fileName << " cannot be found.\n Enter another file : ";
    getline(cin, fileName);
    inFile.open(fileName.c_str());
}
cout << "The file " << fileName << " has been opened successfully." << endl;


Also include <string>.
+ What is the program output you are getting?
+ What are the contents of "TonyNumbers.txt"?
Last edited on
The text file contents are as follows:
1
2
3
4
5

It's only supposed to be numeric data so I don't need strings. Yet. I'm just building a simple driver to figure out why my other program wasn't functioning properly.
Try my suggestions and pinpoint the problem. Then tell me the problem you are having.
int val1, val2, val3, sum;

Should be :
int val1 = 0, val2 = 0, val3 = 0, sum = 0;
Okay so here's a link to a screenshot of my output I posted to imgur. It's the output of my original program.

https://imgur.com/hPrzXrl
Ok, try this :
1
2
3
inFile >> val1;
inFile >> val2;
inFile >> val3;


You change it to :
1
2
while(inFile >> val1) 
cout << "Reading " << val1 << " from the file..." << endl;


And let me know the output.
Last edited on
Nope, I still get a strange error. I made the screen shot larger so you can see my code too.

https://imgur.com/gallery/hw42J
Try this test driver.
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
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
	ifstream inFile;
	int val1 = 0, val2 = 0, val3 = 0, sum = 0;

	string fileName("TonyNumbers.txt");
	inFile.open(fileName.c_str());

	while(!inFile.is_open())
	{
		cout << "The file " << fileName << " cannot be found.\n Enter another file : ";
		getline(cin, fileName);
		inFile.open(fileName.c_str());
	}
	cout << "The file " << fileName << " has been opened successfully." << endl;

	// Read three numbers from the file
	inFile >> val1;
	inFile >> val2;
	inFile >> val3;

	inFile.close();

	cout << "The three values : " << val1 << ' ' << val2 << ' ' << val3 << endl;
	cout << "The sum of three values : " << val1 + val2 + val3 << endl;

	return 0;
}
Last edited on
If your program has successfully opened the data file, this message below should be displayed :
The file TonyNumbers.txt has been opened successfully.
You're program worked, sorta. It pops up saying it can't find my file (saved it on the desktop) and then asks me to enter the file name. I type in the same file name it says it couldn't find and then it suddenly finds it :/??


https://imgur.com/gallery/KWMUY

EDIT: Do you have any idea what those random numbers were and how I can avoid them in the future? I'm a lowly comp sci 1 student and don't know these things (if you couldn't tell already lol). My best guess is that it's somehow reading something from the buffer.
Last edited on
To avoid garbage numbers, you should initialize the numbers with 0.
int val1 = 0, val2 = 0, val3 = 0, sum = 0;
This is it working. I didn't get your successful message.

https://imgur.com/gallery/f9tdMoV
You're program worked, sorta. It pops up saying it can't find my file (saved it on the desktop) and then asks me to enter the file name. I type in the same file name it says it couldn't find and then it suddenly finds it :/??

I have updated my test driver, hopefully it works this time. I use a while loop.
Topic archived. No new replies allowed.