clearing user input

the code asks the user to enter a 10 DIGIT number(no letters). but when i gave it a test run, it stores the size of the previous input making the next input 100% wrong.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  string Num;
	loop:
	cout << "Enter a 10 digit number:  ";
	getline(cin, Num);
	verify(Num);
	int size = Num.length();
	
	while (size != 10)
	{
		if (size < 10)
		{
			cout << "You have entered less than 10 digits. " << size << endl;
		}
		else if (size > 10)
		{
			cout << "You have entered more than 10 digits. " << endl;
		}
		goto loop;
		
	}


the function(bool):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int check = 1;
	for (int i = 0; i < Num.length(); i++)
	{
		if (Num[i] < '0' || Num[i] > '9')
		{
			check = 0;
		}
	}
	if (check == 0)
	{
		cout << "You did not enter a 10 digit numeric number." << endl;
		cout << "Enter a valid 10 digit number : ";
		goBack();
		return false;
	}
	return true;


example output:
1
2
3
4
5
6
7
8
9
10
11
12
Enter a 10 digit number:  123123a
You did not enter a 10 digit numeric number.

Enter a valid 10 digit number : 1231234a
You did not enter a 10 digit numeric.

Enter a valid 10 digit number : 1231231234 // <-- clearly 10 digits
You have entered less than 10 digits. 7 //the 7 is from the previous input
                                      //how do i make it count the current input
Enter a 10 digit number:  1231231231
The number you entered is: 1231231231
closed account (2LzbRXSz)
I'm no expert (so if this doesn't work/or isn't very efficient, my apologies), but after doing some research I've found cin.sync(); which you would put before getline(cin, Num);
This should be a quick fix. If you want to read a little more about it, feel free to check out this link:)
http://www.cplusplus.com/reference/istream/istream/sync/

Otherwise, I'd suggest cin.clear(); or cin.ignore(); after getline(cin,num); (though, I've found that those doesn't always get the job done.)

Here's the reference for cin.clear();
http://www.cplusplus.com/reference/ios/ios/clear/

Here's the reference for cin.ignore();
http://www.cplusplus.com/reference/istream/istream/ignore/
no expert? thats a lie. thanks!! :D
closed account (2LzbRXSz)
Haha, no problem! Thank you too, I try my best :)
Topic archived. No new replies allowed.