newbie question help

This is a discussion on newbie question help within the C++ Programming forums, part of the General Programming Boards category; I am wriing a school assignment it asks the user to enter a char a short and an int... if ...

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    20

    newbie question help

    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;
    }

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    cin is very versatile in this aspect, you can use the return value from cin to determine if the input was correct.
    Code:
    int num;
    cin>>num; 
    // If the user enters a char cin will flag an error
    // So use that error to check the input
    if ( cin>>num )
      cout<<"Good"<endl;
    else
      cout<<"Bad"<<endl;
    -Prelude
    My best code is written with the delete key.

  3. #3
    Unregistered
    Guest
    interesting syntax. will have to try it out. I would have used good() or fail()

    [code]
    int num;
    cin >> num;
    if(cin.fail())
    {
    cout << "input error" << endl;
    cin.clear();
    }
    [\code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stupid Newbie question
    By TimL in forum C++ Programming
    Replies: 4
    Last Post: 07-22-2008, 04:43 AM
  2. C prog newbie question
    By Draginzuzu in forum C Programming
    Replies: 1
    Last Post: 02-03-2003, 05:45 PM
  3. a stupid question from a newbie
    By newcomer in forum C++ Programming
    Replies: 4
    Last Post: 01-11-2003, 03:38 PM
  4. confusion with integers (newbie question)
    By imortal in forum C Programming
    Replies: 7
    Last Post: 12-06-2002, 03:09 PM
  5. newbie class templates question
    By daysleeper in forum C++ Programming
    Replies: 2
    Last Post: 09-18-2001, 09:50 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21