I am wriing a school assignment
it asks the user to enter a char a short and an int...
if for example you enter 43 1234 45455
it accepts it as good input....how do i recject it if a user enters a number where a char should be entered..
here's my code.....
#include <iostream.h>
#include <iomanip.h>
#define MAX_LINE 1024 // maximum length of input line
//------------------------------------------
// Overload function display
// This function displays
// a character
//-----------------------------------------
void display(char letter)
{
cout << endl;
cout << "You entered:" << endl;
cout << " A char: " << letter << endl;
}
//-----------------------------------------
// Overload function display
// This function displays a short
// and a long integer
//-----------------------------------------
void display(int short_int, int long_int)
{
cout << " An int: " << short_int << endl;
cout << " An int: " << long_int << endl;
}
//-----------------------------------------
// Overload function display
// This function displays a
// floating point number and a
// double number
//-----------------------------------------
void display(float number1, double number2)
{
cout << endl;
cout << "You entered:" << endl;
cout << " A double: " << number1 << endl;
cout << " A double: " << number2 << endl;
}
//--------------------------------------------
// Overload function display
// This function displays a
// string
//-------------------------------------------
void display(char string[])
{
cout << " A string: " << string << endl;
}
//-------------------MAIN----------------------
int main()
{
int short_int, long_int; // integer specifications
float number1; // float specification
double number2; // double specification
char letter; // character specification
char string[100]; // string specification, maximum length of 100
char c;
cout << "This program will continuously ask for input data";
cout << endl;
while ( true )
{
while ( true )
{
cout << "\nEnter a character, a short and an integer:"; //prompt user to input a character, short integer and a long integer
cout << endl;
cin >> letter >> short_int >> long_int;
if ( !cin.good() )
{
cout << "\nError: Invalid data!\n";
cin.clear(); // clear error flags
cin.ignore(MAX_LINE, '\n'); // skip to a newline
break; // go to prompt
}
display(letter); // call overload function to display character
display(short_int, long_int); // call overload function to display short integer and long integer
cout << "\nEnter a float, a double and a string:"; // prompt user to input float and double numbers
cout << endl;
cin >> number1 >> number2;
cin.getline(string, 100);
if ( !cin.good() )
{
cout << "\nErorr: Invalid data!\n";
cin.clear(); // clear error flags
cin.ignore(MAX_LINE, '\n'); // skip to a newline
break; // go to prompt
}
display(number1, number2); // call overload function to display float and double numbers
display(string); // call overload function to display string
break; // go to prompt
} // end-of while
cout << "\n\nDo you want to continue? (Y/N) ";
cin >> c ;
if ( 'Y' != c && 'y' != c )
break;
} return 0;
}