954,496 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

loop iterates an extra time

ifstream mystream(file_name);
ofstream outputstream(filename, ios::out);


do {

mystream.getline(line, 100);

mystream >> country_name >> happiness_value;

total_happiness = total_happiness + happiness_value;

count++;

outputstream << country_name << " \t \t " << happiness_value << endl;

} while (!mystream.eof());

the file ifstream is reading from is a .dat file, in this format:

first 1
second 2
third 3
fourth 4

This is the exact file ifstream is opening, but for some reason when I check my output file is shows like it should except it displays "fourth 4" twice,

e.g :

fourth 4
fourth 4

Why is my loop iterating more than it should?

Don't know if it helps, but my count has been initialized as 0 and equals 5 at the end of the loop.

baldwindc
Light Poster
35 posts since Nov 2010
Reputation Points: 10
Solved Threads: 3
 

Do you have an extra empty line in the file? Or the file just ends right after "fourth 4"? You may try both to see what happen.

Taywin
Posting Virtuoso
1,727 posts since Apr 2010
Reputation Points: 229
Solved Threads: 239
 

Thought I tried it before but just tried it again and no success, still iterates the last line of code twice.

See!:

Country Happiness
------------------------------------------
first 1.2
second 2.7
third 3.4
fourth 4.2
fourth 4.2


and my data.dat file:

first 1.2
second 2.7
third 3.4
fourth 4.2

baldwindc
Light Poster
35 posts since Nov 2010
Reputation Points: 10
Solved Threads: 3
 

Search this forum for why using eof() is not a right way to control the read loop.

do {
    mystream.getline(line, 100);
    mystream >> country_name >> happiness_value;
    total_happiness = total_happiness + happiness_value;
    count++;
    outputstream << country_name << " \t \t " << happiness_value << endl;
} while (!mystream.eof());


In short, to trigger eof() to return true, you need to read beyond the last character of the file. The last good read reads up to (and including) the last character, and no more; eof() is not triggered, and the loop runs one more time (hitting eof right away). In this last run nothing gets read, and your variables retain their values from the previous pass.

The right way is

while(mystream.getline(line, 100)) {
    ...
}
nezachem
Posting Shark
903 posts since Dec 2009
Reputation Points: 719
Solved Threads: 194
 

#include
#include
#include
using namespace std;

//void open_file_function();
double average_calculator(double total_happiness, int count);

int main() {

char country_name[60];
double happiness_value;

char file_name[30];
double average_happiness=0;
double total_happiness=0;
char line[100];
int count=0;
cout << "Enter Filename : ";
cin >> file_name;


ifstream mystream(file_name);
ofstream outputstream("happiness.out", ios::out);

if (!mystream.is_open())
{
cout << "Input file " << file_name << " does not exist." ;
return(100);
}
outputstream << "Country \t \t \t" << "Happiness" << endl;
outputstream << "------------------------------------------" << endl ;
setprecision(2);
do {
mystream.getline(line, 100);

mystream >> country_name >> happiness_value;

total_happiness = total_happiness + happiness_value;

count++;

outputstream << country_name << " \t \t \t \t " << happiness_value << endl;


} while (!mystream.eof());

mystream.close();
setprecision(3);
outputstream << endl << endl << average_calculator(total_happiness, count);



}

double average_calculator(double total_happiness, int count)
{
double average_happiness;
average_happiness = (total_happiness / count);
return(average_happiness);

}

baldwindc
Light Poster
35 posts since Nov 2010
Reputation Points: 10
Solved Threads: 3
 

while(mystream.getline(line, 100))
{
//mystream.getline(line, 100);

mystream >> country_name >> happiness_value;

total_happiness = total_happiness + happiness_value;

count++;

outputstream << country_name << " \t \t \t \t " << happiness_value << endl;


}

Does not work. I tried it without the omitted line as well.

With the omitted line the code does the same thing as it did before.

When i remove the // the output is as follows:

first 1.2
third 3.4
third 3.4

baldwindc
Light Poster
35 posts since Nov 2010
Reputation Points: 10
Solved Threads: 3
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: