Why is there a blank line? [duplicate] - c++

This question already has answers here:
Why is iostream::eof inside a loop condition (i.e. `while (!stream.eof())`) considered wrong?
(5 answers)
Why is “while( !feof(file) )” always wrong?
(5 answers)
Closed 7 years ago.
I have the following C++ program:
ofstream output("scores.txt");
output<<"John"<<" "<<"T"<<" "<<"Smith"<<" "<<90<<endl;
output<<"Eric"<<" "<<"K"<<" "<<"Jones"<<" "<<103<<endl;
output.close();
ifstream input;
input.open("scores.txt");
string line;
while (!input.eof()) {
getline(input, line);
cout<<line<<endl;
}
input.close();
cout<<"Done";
The output is:
John T Smith 90
Eric K Jones 103
Done
Why is there a blank line between Eric K Jones 103 and Done?

Structure your loop like this:
while (getline(input, line)) {
cout<<line<<endl;
}
Your duplicate line is because the way your read loop is structured, once you read the last line the eof bit is not set yet because readline succeeded. Therefore, you iterate one more time, doing a readline that does set the eof bit, then you do cout<<line<<endl and that last endl is your extra blank line.

Related

Output is losing a character after reading input [duplicate]

This question already has answers here:
Getline ignoring first character of input
(3 answers)
Closed 4 years ago.
I used the below code in C++ to read characters from the user into a string, including space characters:
cin.ignore();
string s;
getline(cin,s);
cout<<s<<endl;
After taking input, the output is not the same:
input: gee ks for gee ks
output: ee ks for gee ks
Why is this?
cin.ignore() is discarding the 1st character typed by the user, and then getline() reads the remaining characters until a line break is reached. That is why your output is missing the g character from gee.

Last line being printed twice (C++) [duplicate]

This question already has answers here:
Why is iostream::eof inside a loop condition (i.e. `while (!stream.eof())`) considered wrong?
(5 answers)
Closed 7 years ago.
I have a text file called scores.txt with the following data:
John T Smith 90
Eric K Jones 103
Ram S Krishnan 25
Marie A Bell 50
I am trying to read the data from this file and print it using the following C++ code:
ifstream input;
input.open("scores.txt");
string firstName;
char mi;
string lastName;
int score;
while (input) {
input>>firstName>>mi>>lastName>>score;
cout<<firstName<<" "<<mi<<" "<<lastName<<" "<<score<<endl;
}
input.close();
cout<<"Done"<<endl;
The output is:
John T Smith 90
Eric K Jones 103
Ram S Krishnan 25
Marie A Bell 50
Marie A Bell 50
Why is the last line (Marie A Bell 50) being printed twice? How can I prevent this from happening?
It's because just after you read the file's last line, input is not yet at end of file. Your program enters the while loop a fifth time, reads input (which set it to its end of file state), does not alter your variables and prints them.
A way (amongst many) to avoid this is to write something like
while (input >> var1 >> var2)
{
std::cout << var1 << "," << var2 << std::endl;
}

c++ program tries to read from stream after eof [duplicate]

This question already has answers here:
Why is iostream::eof inside a loop condition (i.e. `while (!stream.eof())`) considered wrong?
(5 answers)
Why is “while( !feof(file) )” always wrong?
(5 answers)
Closed 7 years ago.
I have a file containing only one character 1 (there is no newline symbol after it)
Reading the contents with this method:
int value;
ifstream in("somefile");
for (!in.eof())
{
in >> value;
cout << "input: " << value << " , eof" << in.eof() << "'\n";
}
gives me the following output:
input: 1 , eof: 0
input: 1 , eof: 1
The questions are:
1) Why EOF flag is not set after first reading try? I mean, if program successfully reads the number on the first try, it somehow knows that the string representatiion of it is over. To find it out it has to try read at least one byte after 1 character and there it should have hitted EOF. Why that doesn' happen?
2) That said, if I have a file with one value per line, I always do have a duplicate of last input. Does it mean that I always have to discard it in any way and that would be correct? Or for example add extra check for EOF after in >> value; and only if it succeeds, do any logic I want?
I know that I can work around with readline() or while(in >> value) methods but it's more a question of understanding what really happens there.
Thank you.

Sentence into word c++ string [duplicate]

This question already has answers here:
How do I iterate over the words of a string?
(84 answers)
Closed 8 years ago.
How to break down a sentence of string type into words and store it in a vector of string type in c++?
Example
String str="my name";
Into
Vector word={" my","name"}
You can write a simple loop:
std::vector<std::string> words;
std::istringstream is("my name");
std::string word;
while (is >> word) {
// ...
words.push_back(word);
// ...
}
which in my opinion is good idea because you'll most likely need to do other things with those words apart the simple extraction of them. The body of the loop can be easily extended.

Input file reading error [duplicate]

This question already has answers here:
Why is iostream::eof inside a loop condition (i.e. `while (!stream.eof())`) considered wrong?
(5 answers)
Closed 9 years ago.
Hi i currently wrote a program but I'm having problems with reading the file. It seems to have read the last line twice, as a result, producing the same results twice.
I had put the getline() function to read the first line, in order to store the string for the variable G.
The rest, I had stored it in a vector of the class object.
basically the file is opened and it performs this loop
file.open("bodies1.txt");
getline(file, G1);
....
while(!file.eof)
{
file >> body;
bodies.push_back(body);
}
this is what the text file look like (this is just a sample. not the actual thing)
0.02932
Sun 32 42 53 2 2
Moon 49 32 4 2 1
Jupiter 32 53 2 3 2
I really was wondering why it read the last line twice. Any suggestions?
while(!file.eof())
{
file >> body;
bodies.push_back(body);
}
After reading the last object in to body, eof is the next character. Then
file >> body;
tries to read it and fails. So whatever was in body before is still there and will be pushed in to the vector.
instead you should read like
while(file >> body)
{
bodies.push_back(body);
}
This way your while stops as soon as you encounter eof() and you will not do an extra push in to the vector.