Need help w/getline & cin.ignore

Basically, in this code, after it loops around at least once in the end, because you haven't written either yes or no, it keeps skipping the first letter of what you've written. For example: (go to bottom)

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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <iostream>
#include <string>
#include <limits>
using namespace std;

int searcher(int search[], int size, int value)
{
	int low = 0;
	int high = size - 1;

	while (low <= high)
	{
		int mid = (high + low) / 2;

		if (value == search[mid])
		{
			return mid;
		}
		else if (value > search[mid])
		{
			low = mid + 1;
		}
		else
		{
			high = mid - 1;
		}
	}

	return -1;
}

int main()
{

	int toSearch[8] = { 12, 22, 34, 47, 55, 67, 82, 98 };

	while (1 == 1)
	{

		cout << endl << "What number do you want to search?: ";

		int num = 0;

		while (!(cin >> num))
		{
			cin.clear();
			cin.ignore(numeric_limits<streamsize>::max(), '\n');

			cout << endl << "Error! You either didn't enter a number or your number was too long!" << endl << endl << "Retry: ";
		}

		int result = searcher(toSearch, 8, num);

		if (result >= 0)
		{
			cout << endl << endl << "The number \"" << num << "\" was located in the array at index \"" << result << "\"" << endl;
		}
		else
		{
			cout << endl << "The number \"" << num << "\" was not found in the array..." << endl << endl;
		}

		string choice = "";
		int stupidity = 0;

		do
		{
			cout << endl << endl << "Restart program?: ";
			cin.ignore();
			getline(cin, choice);

			if (choice == "yes")
			{

			}
			else if (choice == "no")
			{
				return 0;
			}
			else
			{
				cout << endl << endl << "Type either \"yes\" or \"no\"" << endl;
				stupidity++;

				if (stupidity >= 10 && stupidity < 12)
				{
					cout << endl << "How many times are you gonna make a mistake here!?" << endl;
				}
				else if (stupidity >= 21 && stupidity < 23)
				{
					cout << endl << "Just say either \"yes\" or \"no\"!";
				}
			}
		} while (choice != "yes" && choice != "no");
	}

	system("pause");
	return 0;
}

First let it run normally, then let it ask you for the 1st time whether or not you want to restart, type something, anything, but make it sure it's at least 2 words separated with a space or anything like that. Normally, it goes to the ELSE part, does those things, then goes back around, but this time, no matter what you write, it won't accept it.
Last edited on
Or to make it more clearly, here are some steps to reproduce the problem:

1 - run program

2 - where it asks you for a number, type any number

3 - where it asks you to restart, type for example "yes and no" without the quotes

And after you've written at least 2 words at the end, the program bugs out and when it asks you again, no matter what you write, it won't accept it, because at the part where it says

1
2
cin.ignore();
getline(cin, choice);

it always skips the first letter of what you've written.
Actually, I think I've fixed it by making an IF statement, where the 1st time it goes around normally, but the second time it doesn't do cin.ignore() by making it like this:

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
		string choice = "";
		int stupidity = 0;
		int protection = 0;

		do
		{
			cout << endl << endl << "Restart program?: ";
			if (protection == 0)
			{
				cin.ignore();
				getline(cin, choice);
				protection++;
			}
			else
			{
				getline(cin, choice);
			}

			if (choice == "yes")
			{

			}
			else if (choice == "no")
			{
				return 0;
			}
			else
			{
				cout << endl << endl << "Type either \"yes\" or \"no\"" << endl;
				stupidity++;

				if (stupidity >= 10 && stupidity < 12)
				{
					cout << endl << "How many times are you gonna make a mistake here!?" << endl;
				}
				else if (stupidity >= 21 && stupidity < 23)
				{
					cout << endl << "Just say either \"yes\" or \"no\"!";
				}
			}
		} while (choice != "yes" && choice != "no");

But does anyone know a better way to prevent this bug? I don't even know why it's caused, I only know how it works - the 2nd time it goes around the "do while" loop, due to cin.ignore(), it ignores the first letter of whatever you've written, but I don't know why it doesn't do that the first time. So far this is the only way I've found to prevent this. But is there a better way?
Last edited on
But now this brings to my attention another bug:

At the very beginning, there's a small bug, where, if you write 2 numbers instead of one, separated by a space or anything like that, it takes the first number and uses it at the beginning, but it also keeps the second and uses it at the next opportunity. How do I make it so that the very first cin gets the first number? (Not digit, but number. For example, if you write "12 24" without the quotes, how do I make it only accept the first "12"?
Last edited on
If there's no answer, which I wouldn't blame, since now the title doesn't completely meet with the current question, I could make the question in the previous post into it's own topic.
For your original question, you could have just moved the cin.ignore(); to before your do while loop. If you want to know why you needed it in the first place, it's because after you read in the number, the '\n' is left in the stream. getline(cin, choice); will read in characters to the string until it hits '\n'. So if you don't ignore that '\n', you would be storing an empty string in choice.

For your second question, I've seen LB link an article he wrote that I think answers it pretty well.

http://www.lb-stuff.com/user-input
Thx, I think I've finally fixed everything. If you're interested, here's the final 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
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

int searcher(int search[], int size, int value)
{
	int low = 0;
	int high = size - 1;

	while (low <= high)
	{
		int mid = (high + low) / 2;

		if (value == search[mid])
		{
			return mid;
		}
		else if (value > search[mid])
		{
			low = mid + 1;
		}
		else
		{
			high = mid - 1;
		}
	}

	return -1;
}

int main()
{

	int toSearch[8] = { 12, 22, 34, 47, 55, 67, 82, 98 };

	while (1 == 1)
	{

		cout << endl << "What number do you want to search?: ";

		string line;
		int num;
		
		while (getline(cin, line)
		) 
		{
			istringstream is{ line };
			if ((is >> num) && !(is >> line)) 
			{
				break; 
			}
			cerr << endl << "Invalid input, try again." << endl << endl << "Enter a number" << endl;
		}

		int result = searcher(toSearch, 8, num);

		if (result >= 0)
		{
			cout << endl << endl << "The number \"" << num << "\" was located in the array at index \"" << result << "\"" << endl;
		}
		else
		{
			cout << endl << "The number \"" << num << "\" was not found in the array..." << endl << endl;
		}

		string choice = "";
		int stupidity = 0;
		int protection = 0;

		do
		{
			cout << endl << endl << "Restart program?: ";
			
			getline(cin, choice);

			if (choice == "yes")
			{

			}
			else if (choice == "no")
			{
				return 0;
			}
			else
			{
				cout << endl << endl << "Type either \"yes\" or \"no\"" << endl;
				stupidity++;

				if (stupidity >= 10 && stupidity < 12)
				{
					cout << endl << "How many times are you gonna make a mistake here!?" << endl;
				}
				else if (stupidity >= 21 && stupidity < 23)
				{
					cout << endl << "Just say either \"yes\" or \"no\"!";
				}
			}
		} while (choice != "yes" && choice != "no");
	}

	system("pause");
	return 0;
}
Topic archived. No new replies allowed.