Hi, guys.I am new to c++ programming and I've got a question about the cin.ignore() function I found in a program that displays the number of digits in a number entered by the user.
This is the code:
Code:
#include <iostream>
using namespace std;
int main()
{
unsigned int n;
int count = n;
while (true)
{
cout << "Please enter an integer: ";
if (cin >> n) break;
cin.clear();
cin.ignore( 1000, '\n' );
// I don't understand what's in the braces: 1000 and '\n' :confused:
cout << " It's not correct! ";
}
cout << " You entered the number " << n << ".\n";
while ( n > 0 )
n /= 10,count ++ ;
cout << "Your number has " << count << " digits.";
return 0;
}
I really hope you can explain me that line with cin.ignore() ( I know that adding cin.ignore clears the new line from the stream but what is with 1000? )and when is the perfect time to use it and as well the while(true) loop used in the program.
I really want to understand these things so please explain me .
Thanks in advance !