I am in a c++ class and I am having a problem with part of my program. The code is as follows:
#include <iostream.h>
void main()
{
int num;
cout << "Enter # of credits: ";
cin >> num;
while(cin.fail()!=0)
{
cout << "error";
cout << "Enter # of credits: ";
cin >> num;
}
}
Basically, I want to prompt the user to enter the number of credits, then test to see if the user entered an int. But if the user were to enter a letter, the program should report an error and re-prompt for # of credits. What happens is that when a user enters a non int value such as a letter, is the program goes into the while loops and "error" and "Enter # of credits" keeps getting displayed over and over, never stopping. And it doesnt let me re-enter the # of credits. Is the input buffer flooded? can I clear it out before I ask to re-enter the credits? I need this field to be and int so i can preform calculations with it later. Does anyone have any suggestions? [sig][/sig]
#include <iostream.h>
void main()
{
int num;
cout << "Enter # of credits: ";
cin >> num;
while(cin.fail()!=0)
{
cout << "error";
cout << "Enter # of credits: ";
cin >> num;
}
}
Basically, I want to prompt the user to enter the number of credits, then test to see if the user entered an int. But if the user were to enter a letter, the program should report an error and re-prompt for # of credits. What happens is that when a user enters a non int value such as a letter, is the program goes into the while loops and "error" and "Enter # of credits" keeps getting displayed over and over, never stopping. And it doesnt let me re-enter the # of credits. Is the input buffer flooded? can I clear it out before I ask to re-enter the credits? I need this field to be and int so i can preform calculations with it later. Does anyone have any suggestions? [sig][/sig]