Why does getline skip first ENTER?

For some reason, when running this, the first time getline() is called, i have to hit enter twice before execution continues. After the first time, everything works normally as expected, but then the while loop seems to go thru one more time even after the "break" should have happened after typing in "exit."

I am guessing these are two seperate issues but can't figure out how to fix either or why they are happening.. tried inserting "cin.clear();" randomly in different places before and after the call to "getline()" without much expectations or success. I was able to find a lot of people on forums like this one having similar issues with getline, but invariably those people all were using cin >>, cin.ignore, or cin.get in combination with getline, and as you can see in the code, only getline is used here or anywhere else in the program for getting input.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
unsigned short int e_console()
{
	unsigned short int e_error = 0;
	string s_input_line;
	
	cout << "System ready. Hello!\n";

	while (e_error == 0)
	{
		cout << "X> ";
		getline (cin, s_input_line);
		
		if (s_input_line == "exit")
		{
			break;
		}
	}

	return e_error;
}


System ready. Hello!
X> test

X> foo
X> bar
X> why tho
X> exit
X> 
buh bye!
Press any key to continue

Last edited on
I can't replicate that behaviour (test code below).

I think you have something involving input OUTSIDE this code snippet. If you can't find that then I think you would have to show more code.


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
#include <iostream>
#include <string>
using namespace std;

unsigned short int e_console()
{
   unsigned short int e_error = 0;
   string s_input_line;
   
   cout << "System ready. Hello!\n";

   while (e_error == 0)
   {
      cout << "X> ";
      getline (cin, s_input_line);
      
      if (s_input_line == "exit")
      {
         break;
      }
   }

   return e_error;
}


int main()
{
   unsigned short int e = e_console();
}
Last edited on
ok, I'll post the whole thing, but don't judge me.

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
#include <iostream>
#include <string>
#include <sstream>

using namespace std;

unsigned short int e_console()
{
	unsigned short int e_error = 0;
	string s_input_line;
	
	cout << "System ready. Hello!\n";

	while (e_error == 0)
	{
		cout << "X> ";
		getline (cin, s_input_line);
		
		if (s_input_line == "exit")
		{
			break;
		}
	}

	return e_error;
}

int main()
{
	unsigned short int g_error;
	g_error = e_console();
	cout << "buh bye!\n";
	return 0;
}


This is me trying to get back into writing code after a long break. So frustrating to be running into issues with such basic stuff -.- Thanks for taking the time to help me out with this
Last edited on
This is what it produced on C++ shell (gear-wheel icon to the top right of your code sample)

System ready. Hello!
X> test
X> foo
X> bar
X> why tho
X> exit
buh bye!


What sort of device are you running this on? Are you keying in replies directly or redirecting them from file?
Last edited on
that's weird huh

uhm..
this is an old lappy with Windows XP
Microsoft Visual Studio 6.0
*shrug*

I know it's all pretty old stuff but.. does that matter?

No, i'm just typing this stuff in directly to the console.. does the same thing whether i run from Studio or just run the exe normally

It's good to know that the code works right lol now i'm just wondering what's wrong with my end..
Last edited on
So i'm unsure whether to just bump this or create a new topic.. At this point I have for some reason different behavior on my machine than what the c++ shell on this website produces.. what would cause that? I don't even know enough about how a compiler works to know where to start looking for an answer to this.
Topic archived. No new replies allowed.