Can't read single line using std::getline

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
Look at line 14 carefully. There is a word before the first parenthesis that you need to understand the implications of.
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
What do you think line 12 is for? You have it coded such that it skips every other line.
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;
}
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.
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.
closed account (48T7M4Gy)
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.