Error when entering wrong data type in do-while loop

Discussion in 'C++' started by Jeff, Feb 5, 2005.

  1. Jeff

    Jeff Guest

    Hello

    I need help with a problem

    I have homework for a larger program than shown below but the code below
    effectively shows the problem whem compiled and executed.

    Basically, I want to be able keep the loop going even if a character
    (alphabet) is entered when it should be a number i.e. the wrong data type is
    entered by mistake.

    The concept is 'that the loop should work under all conditions'.

    At the moment if a character is entered, the result is that the screen
    constantly scrolls and is not readable.


    Ideally I would like to insert into the program a line to the effect

    {
    'if (n != int)
    cout << "that is not a valid option. Please try again"
    }

    However, for those of you who know c++ better than me, you will know that
    this cannot be entered

    Is there any way of entering this logical statement





    Below is a part of the code, when compiled and executed will demonstrate the
    problem


    #include <iostream.h>

    int main()
    {
    unsigned long n;
    do
    {
    cout<< " Enter number (0 to end): ";
    cin >> n;
    cout << "You entered: " << n << "\n" ;


    }
    while (n != 0);

    return 0;
    }
     
    Jeff, Feb 5, 2005
    #1
    1. Advertising

  2. Jeff wrote:
    > Hello
    >
    > I need help with a problem
    >
    > I have homework for a larger program than shown below but the code below
    > effectively shows the problem whem compiled and executed.
    >
    > Basically, I want to be able keep the loop going even if a character
    > (alphabet) is entered when it should be a number i.e. the wrong data type is
    > entered by mistake.
    >
    > The concept is 'that the loop should work under all conditions'.
    >
    > At the moment if a character is entered, the result is that the screen
    > constantly scrolls and is not readable.
    >
    >
    > Ideally I would like to insert into the program a line to the effect
    >
    > {
    > 'if (n != int)
    > cout << "that is not a valid option. Please try again"
    > }
    >
    > However, for those of you who know c++ better than me, you will know that
    > this cannot be entered
    >
    > Is there any way of entering this logical statement
    >
    >
    >
    >
    >
    > Below is a part of the code, when compiled and executed will demonstrate the
    > problem
    >
    >
    > #include <iostream.h>
    >
    > int main()
    > {
    > unsigned long n;
    > do
    > {
    > cout<< " Enter number (0 to end): ";
    > cin >> n;
    > cout << "You entered: " << n << "\n" ;
    >
    >
    > }
    > while (n != 0);
    >
    > return 0;
    > }
    >
    >


    Add something like:

    if (!isaplha(n) {
    cout << "You were supposed to enter a number" << endl;
    }
     
    Karsten Baumgarten, Feb 5, 2005
    #2
    1. Advertising

  3. "Jeff" <> wrote...
    > [...]
    > Basically, I want to be able keep the loop going even if a character
    > (alphabet) is entered when it should be a number i.e. the wrong data type
    > is entered by mistake.
    > [...]


    This is covered in the FAQ. Please look it up. If you're new here and
    don't yet know it, the link is http://www.parashift.com/c -faq-lite/

    V
     
    Victor Bazarov, Feb 5, 2005
    #3
  4. Karsten Baumgarten wrote:
    >
    > if (!isaplha(n) {
    > cout << "You were supposed to enter a number" << endl;
    > }


    Sorry for the typo! The function is called isalpha().
     
    Karsten Baumgarten, Feb 5, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steven

    while loop in a while loop

    Steven, Mar 24, 2005, in forum: Java
    Replies:
    5
    Views:
    2,241
    Tim Slattery
    Mar 30, 2005
  2. japh
    Replies:
    4
    Views:
    740
  3. Uday Bidkar
    Replies:
    4
    Views:
    497
    =?ISO-8859-15?Q?Juli=E1n?= Albo
    Dec 12, 2006
  4. Colin Howell
    Replies:
    2
    Views:
    575
    John Bokma
    Apr 25, 2010
  5. Isaac Won
    Replies:
    9
    Views:
    387
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page