Can't read single line using std::getline
Sep 24, 2015 at 3:59am UTC
I am new to c++ so this might be a simple question. I am trying to read a single line at a time, however when I use std::getline, it reads all of them at once.
My input .txt file is:
3 //Number of cases
Jake
Ronald Skiles Jr
Timmy
I want my output .txt file to read:
Case 0:
Jake
Case 1:
Ronald Skiles Jr
Case 2:
Timmy
However I am getting:
Case 0:
Jake
Ronald Skiles Jr
Timmy
Case 1:
Case 2:
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 <string>
int main()
{
int i;
int case_number = 1;
std::cin >> i;
for (i; i >= case_number; ++case_number)
{
std::cout << "Case: " << case_number - 1 << "\n" ;
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n' );
std::string name;
while (std::getline(std::cin, name))
{
std::cout << name << "\n" ;
}
//std::string name;
//std::cin >> name;
//std::cout << name << "\n";
}
return 0;
}
Last edited on Sep 24, 2015 at 4:00am UTC
Sep 24, 2015 at 4:14am UTC
Look at line 14 carefully. There is a word before the first parenthesis that you need to understand the implications of.
Sep 24, 2015 at 4:29am UTC
Thank you for responding.
So on line 14, I removed the while statement and now my output is:
Case: 0
Jake
Case: 1
Timmy
Case: 2
It now skips my second line. Any ideas as to why?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include <iostream>
#include <string>
int main()
{
int i;
int case_number = 1;
std::cin >> i;
for (i; i >= case_number; ++case_number)
{
std::cout << "Case: " << case_number - 1 << "\n" ;
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n' );
std::string name;
std::getline(std::cin, name);
std::cout << name << "\n" ;
}
return 0;
}
Last edited on Sep 24, 2015 at 4:29am UTC
Sep 24, 2015 at 4:35am UTC
What do you think line 12 is for? You have it coded such that it skips every other line.
Sep 24, 2015 at 4:44am UTC
Hmmm... I'm making some progress. You were right, line 12 was unnecessary. Now however I'm getting an ouput like this:
Case: 0
Case: 1
Jake
Case: 2
Ronald Skiles Jr
I feel like line 14 is the one giving me trouble because I don't really understand it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include <iostream>
#include <string>
int main()
{
int i;
int case_number = 1;
std::cin >> i;
for (i; i >= case_number; ++case_number)
{
std::cout << "Case: " << case_number - 1 << "\n" ;
std::string name;
std::getline(std::cin, name);
std::cout << name << "\n" ;
}
return 0;
}
Sep 24, 2015 at 4:49am UTC
Line 12 wasn't unnecessary, it was just in the wrong place. After line 8 there is still a newline character left in the buffer and that gets sucked up as an empty line on the first loop iteration.
Sep 24, 2015 at 4:52am UTC
I finally get it now. Thank you so much for your help. It was a huge help with you not just giving me the answer but guiding me through it.
Sep 24, 2015 at 5:02am UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include <iostream>
#include <string>
int main()
{
int i;
int case_number = 1;
std::cin >> i;
std::cin.ignore();
for (i; i >= case_number; ++case_number)
{
std::cout << "Case: " << case_number - 1 << "\n" ;
std::string name;
std::getline(std::cin, name);
std::cout << name << "\n" ;
}
return 0;
}
Topic archived. No new replies allowed.