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.
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;
}
elseif (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();
returnfalse;
}
returntrue;
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
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 beforegetline(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();aftergetline(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/