Output for ifstream not working correctly

I am having trouble printing the array of integers after they are inputted into the program from the file.
The prices.dat file I created contains 10 numbers, but when I try to display the integers after they have been read into the array the 1st element in the array is skipped over.

After the code is my output, and it should not be printing out any of the negative values you see. It should print out the array of numbers from 0th element to 10th (9th position to be exact) and the sum of the numbers.

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
 #include<iostream>
#include<iomanip>
#include<string>
#include<fstream>

using namespace std;

int main()
{
	string filename;
	int lineNum = 0;
	int sum = 0;
	cout << "Please provide the filename: ";
	cin >> filename;

	ifstream inFile(filename.c_str());
	
	if (inFile.fail())
	{
		cout << "The file did not open successfully. Please check that the file exists. " << endl;
		system("pause");
		exit(1);
	}
	inFile >> lineNum;
	while (!inFile.eof())
	{
		cout << setw(2) << lineNum++ << endl;
		inFile >> lineNum;
	}
	inFile.close();
	
	int *count;
	count = new int[lineNum];
	inFile.open(filename.c_str());
	inFile >> lineNum;
	while (!inFile.eof())
	{
		
		for (int i = 0; i < lineNum; i++)
		{
			inFile >> count[i];
			
		}
		sum += lineNum;
		cout << sum << endl;
	}
	inFile.close();

	/*for (int x = 0; x < lineNum; x++)
	{
		cout << count[x] << " ";
	}*/
	system("pause");
	return 0;

//Output:
100
99
77
54
32
93
102
21
93
10
 99 77 54 32 93 102 21 93 10 -842150451 -842150451 -33686019 -1414812757 

-1414812757 0 0 1471292178 5864 14597912 14588824 -17891602 -17891602 
-17891602 -17891602 -17891602 -17891602 -17891602 -17891602 -17891602 
-17891602 -17891602 -17891602 -17891602 -17891602 1437672211 452990700 
14593064 14597768 257563020 729 1 2 205 -33686019 -33686272 -1414812675 
-1414812757 -17891669 0 0 1420894994 503322349 14597704 14597840 257563020
 729 6 2 206 -33686019 1936482662 -33750939 -1414791683 -1414812757
 -17912917 -17891602 0 0 1420894994 520099564 14597768 14593152 257563020
 729 5 2 207 -33686019 1702195828 -33686272 -1414812675 -1414812757 -17891669 
-17891602 0 0 1907499828 5868 14598536 14597632 -17891602 -17891602 
-17891602 -17891602 -17891602 -17891602 -17891602 -17891602 -17891602 
-17891602 100
Last edited on
Hi,
Can you let us see the input file?
Firstly,
To check if a file couldn't be opened, don't use if(inFile.fail()), use :
if(inFile.is_open() == false)
Secondly,
1
2
3
4
5
6
inFile >> lineNum;
	while (!inFile.eof())
	{
		cout << setw(2) << lineNum++ << endl;
		inFile >> lineNum;
	}


I think you used the variable (lineNum) for wrong purpose.
You count the number of lines in a file. You should really not input lineNum. Better yet, use another variable to retrieve data from the file instead.

Additionally, to check if (inFile) inputs something successfully, move the code inside the if or while condition instead of having to use (inFile.eof()) :
1
2
3
4
5
6
7
int number;
lineNum = 0;

while ((inFile >> number) && ++lineNum)
 {
		cout << setw(2) <<  number << endl;
 }
Last edited on
Thirdly,
1
2
3
4
5
6
7
8
9
10
inFile >> lineNum;
while (!inFile.eof())
	{
		for (int i = 0; i < lineNum; i++)
		{
			inFile >> count[i];
		}
   }
		sum += lineNum;
		cout << sum << end;


The (sum) of course is not calculated this way.

So :
1
2
3
4
5
6
7
int lineNum2 = 0;
while ((inFile >> count[lineNum2]) && ++lineNum2)
sum += count[lineNum2 - 1];

cout << endl;
cout << "  number of lines in the file : " << numLine << endl;
cout << "  the sum is : " << sum << endl;
Last edited on
Fourthly, you forgot to delete (free) your dynamically allocated array (count) when it is nolonger needed :
1
2
3
system("pause");
delete [] count;
return 0;
Fifthly, use std::getline() instead of (cin >> filename), so that filename can contain space characters :
1
2
3
4
5
while(filename.length() <= 1)
{
    cout << "Please provide the filename: ";
	getline(cin, filename);
}
Does that help you? :)
@closed This is the input file. It is a .dat file with a list of integers
100
99
77
54
32
93
102
21
93
10

Second, my textbook shows me to do it in several ways, but this is one of them. What is incorrect about using .fail?
Also the code below purpose is to count the number of lines. It is being used to set the array size.
I tested the logic and it works.
The break down in my code was the summing of the elements in the array after they have been read in.
Using the same int variable(lineNum) for the input stream directly into the array was causing problems, as well as using "lineNum" itself as the variable being added into sum rather than the array.

I wonder if this was because lineNum already had data stored in it, and therefore reading into it again caused the issue.

@ closed >> Also in your post where you say "Thirdly" and are referencing the summing, you use a variable numLine in the cout statement. Is this a typo?

1
2
3
4
5
6
7
8
 

inFile >> lineNum;
	while (!inFile.eof())
	{
		cout << setw(2) << lineNum++ << endl;
		inFile >> lineNum;
	}


@ closed >> The code works beautifully now after I fixed the errors with the summation portion.

Thank you for you help! Quite frustrating that my brain doesn't see the logic, but I shall get there soon :)
Glad it helped :)
Topic archived. No new replies allowed.