how do i do this

how do i make the program not crash when the user inputs an alphabet.
the program has to ask to re-enter if the user inputs alphabet. and "pause" when a number is entered.


int main()
{
int number;
cout << "Please type a user input:" << endl;
cin >> number;

//do something with the number... example:
cout << "The value you entered is " << number << endl;

system("pause");
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

int main()
{
    int number ;

    // ( std::cin >> number ) is 'true' if a number was read from std::cin
    while( std::cout << "enter a number: " && !( std::cin >> number ) )
    {
        std::cout << "you did not enter a number\n" ;
        std::cin.clear() ; // clear the error state
        std::cin.ignore( 1000, '\n' ) ; // discard the bad input
    }

    std::cout << "you entered " << number << '\n' ;
}
*********
Last edited on
thanks guys. that helps
was my solutions helpful ?
Topic archived. No new replies allowed.