Problems using getline in various situations

I seem to have run into a problem using using the getline function. It works fine in my simple code shown here:

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
35
#include <iostream>
#include <cmath>
#include <string>
using namespace std;
void caesar_cipher(string in,int l, int k)
{
	int i=0;
	int aletter;
	int integer;
	char newletter;
	while (i<=l)
	{
		aletter = in[i];
		i++;
		integer = aletter + k;
		if (integer > 122){integer = integer - 91;}
		newletter = (char) integer;
		cout << newletter;
	}
	cout << "\n";
}
int main()
{
	string message;
	int length;
	int key;
	cout << "Enter a line of text to be encrypted\n";
	getline(cin,message);
	length = message.length() - 1;
	cout << "What key would you like to use?\n";
	cin >> key;
	caesar_cipher(message, length, key);
	system("pause");
	return 0;
}


However, when used in the more complex code shown below, choosing option 1 seems to skip the getline function and move straight to asking for the key. I have looked through the forum but I haven't been able to find someone with a similar problem. I would appreciate an extra pair of eyes looking at the code and seeing what I'm missing.

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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <iostream> 
#include <cmath>
#include <string>
using namespace std;
void caesar_cipher(string in,int l, int k)
{
	int i=0;
	int aletter;
	int integer;
	char newletter;
	while (i<=l)
	{
		aletter = in[i];
		i++;
		integer = aletter + k;
		if (integer > 122){integer = integer - 91;}
		else if (integer < 32){integer = integer + 91;}
		newletter = (char) integer;
		cout << newletter;
	}
	cout << "\n";
}
int main()
{
	string option;// The string used to identify which of the seven options of the program the user wants to select
	while (option != "0")
	{
	cout << "Welcome to the Super Secret Spy Encryption and Decryption program.\n";
	cout << "Please select from the following options:\n"; 
	cout << "0 - Quit\n";
	cout << "1 - Encrypt text using the Caesar Cipher and a key\n";
	cout << "2 - Decrypt text using the Caesar Cipher and a key\n";
	cout << "3 - Decrypt text using the Caesar Cipher without a key\n";
	cout << "4 - Encrypt text using the \"Super Cipher\" and a key\n";
	cout << "5 - Decrypt text using the \"Super Cipher\" and a key\n";
	cout << "6 - Decrypt text using the \"Super Cipher\" without a key\n";
	cin >> option;
	if (option == "0") {cout << "Rest well knowing your file is secure. Good hunting. \n\n"; system("pause"); return 0;}
	if (option == "1")
	{ 
		cout << "\n";
		string message = "";
		int length;
		int key;
		cout << "Enter a line of text to be encrypted\n";
		cin.clear();
		getline(cin, message);
		length = message.length() - 1;
		cout << "What key would you like to use?\n";
		cin >> key;
		caesar_cipher(message, length, key);
	}
	if (option == "2")
	{
		string encrypted_message;
		int length;
		int key;
		cout << "Enter the encrypted text here\n";
		cin >> encrypted_message;
		length = encrypted_message.length() - 1;
		cout << "What key would you like to use?\n";
		cin >> key;
		caesar_cipher(encrypted_message, length, -key);
	}
	/*if (option == "3")
	{
		string encrypted_message;
		int length;
		int key;
		cout << "Enter the encrypted text here\n";
		cin >> encrypted_message;
		length = encrypted_message.length() - 1;*/
}
	system("pause");
	return 0;
}
Without having looked over the code too much, here's my take on what (might be) happening:

After you run cin >> option; on line 37 (say you picked 1), there's still a newline character left in the input buffer (since cin only took out the 1 and left the newline character from you pressing the Enter key in).
Then getline(cin, message); runs on line 47, but it immediately encounters a newline character (that you still haven't gotten rid of), so it treats that as its signal to stop taking input, so message ends up blank.

In fact, if you want to test it out, when the program asks you to pick an option, type
1 This is my message, wheeeee!
and you'll find that it'll encrypt the message "This is my message, wheeeee!".

The "proper" way to get rid of that newline character (and whatever else you might have typed in between) is to put this right after cin >> option;:
cin.ignore(numeric_limits<streamsize>::max(), '\n');
(You'll need to #include <limits> as well).
Fantastic work, my friend! My program functions perfectly now. If you don't mind, can you explain in greater detail what cin.ignore(numeric_limits<streamsize>::max(), '\n'); does?
It basically tells cin to ignore (discard) as many characters from the input stream as possible, all the way until it hits a newline character ('\n'). (The newline character gets thrown out as well)

So if you entered
1 hi there blah blah blah<enter>
when it asked you to pick an option, then first the cin >> option; will read the "1" and put that into option, and then cin.ignore(numeric_limits<streamsize>::max(), '\n'); will throw all the rest out, so when the program gets to getline(cin, message);, the input stream will be completely empty and ready for you to input your message.
Topic archived. No new replies allowed.