Question on if/else statements

Discussion in 'C++' started by thetwistedpanda, Aug 20, 2004.

  1. I am taking a c++ class at msms . ( mississippi school for math and
    science ) But anyways, we are doing a program in which, you enter a
    number 0-6 , and it outputs a domino. what I am wondering is...

    Somewhat of a newbie question..

    Is there anyway to for say , if to prevent users from entering a 'c' ,
    err character , and decimals ?
     
    thetwistedpanda, Aug 20, 2004
    #1
    1. Advertising

  2. thetwistedpanda wrote:
    > I am taking a c++ class at msms . ( mississippi school for math and
    > science ) But anyways, we are doing a program in which, you enter a
    > number 0-6 , and it outputs a domino. what I am wondering is...
    >
    > Somewhat of a newbie question..
    >
    > Is there anyway to for say , if to prevent users from entering a 'c' ,
    > err character , and decimals ?


    There is no way in C++ to prevent them from pressing those keys, but
    if they do, the way to work around it is to read a string (symbols)
    and check for the correct contents and reject something that cannot
    be processed (with a message).

    Victor
     
    Victor Bazarov, Aug 20, 2004
    #2
    1. Advertising

  3. thetwistedpanda

    David Hilsee Guest

    "thetwistedpanda" <> wrote in message
    news:...
    > I am taking a c++ class at msms . ( mississippi school for math and
    > science ) But anyways, we are doing a program in which, you enter a
    > number 0-6 , and it outputs a domino. what I am wondering is...
    >
    > Somewhat of a newbie question..
    >
    > Is there anyway to for say , if to prevent users from entering a 'c' ,
    > err character , and decimals ?


    You may want to browse the FAQ (http://www.parashift.com/c -faq-lite/).
    Section 15 ("Input/output via <iostream> and <cstdio>") should help you out.

    --
    David Hilsee
     
    David Hilsee, Aug 20, 2004
    #3
  4. thetwistedpanda

    Anil Mamede Guest

    On Thu, 19 Aug 2004 16:17:26 -0700, thetwistedpanda wrote:

    > I am taking a c++ class at msms . ( mississippi school for math and
    > science ) But anyways, we are doing a program in which, you enter a
    > number 0-6 , and it outputs a domino. what I am wondering is...
    >
    > Somewhat of a newbie question..
    >
    > Is there anyway to for say , if to prevent users from entering a 'c' ,
    > err character , and decimals ?


    maybe with scanf

    char str[20] = "";
    int result = scanf("%[abd-z]", str);

    if(result == 0) {
    /* Error */
    } else {
    /* ok */
    }

    Anil Mamede
     
    Anil Mamede, Aug 21, 2004
    #4
  5. thetwistedpanda

    Mabden Guest

    "Anil Mamede" <> wrote in message
    news:p...
    > On Thu, 19 Aug 2004 16:17:26 -0700, thetwistedpanda wrote:
    >
    > > I am taking a c++ class at msms . ( mississippi school for math and
    > > science ) But anyways, we are doing a program in which, you enter a
    > > number 0-6 , and it outputs a domino. what I am wondering is...
    > >
    > > Somewhat of a newbie question..
    > >
    > > Is there anyway to for say , if to prevent users from entering a 'c' ,
    > > err character , and decimals ?

    >
    > maybe with scanf
    >
    > char str[20] = "";
    > int result = scanf("%[abd-z]", str);
    >
    > if(result == 0) {
    > /* Error */
    > } else {
    > /* ok */
    > }
    >


    I would probably use get() in a loop and discard any key that wasn't a 0-6.
    Any other key should just be ignored, except whatever you use to terminate
    the input (like 'x' for exit or something).

    C / C++ greatest strength is its ability to read a character and decide to
    do nothing with it - and to do it fast!
    ;-)

    --
    Mabden
     
    Mabden, Aug 21, 2004
    #5
  6. thetwistedpanda wrote:

    > I am taking a c++ class at msms . ( mississippi school for math and
    > science ) But anyways, we are doing a program in which, you enter a
    > number 0-6 , and it outputs a domino. what I am wondering is...
    >
    > Somewhat of a newbie question..
    >
    > Is there anyway to for say , if to prevent users from entering a 'c' ,
    > err character , and decimals ?


    There is no method to prevent the user from doing anything; at
    least from the _standard_ C++ perspective.

    You can input an integer and test for success. Inputting an
    integer will fail if the user inputs a character, symbol or
    decimals:
    unsigned int value;
    bool invalid_input(false);

    do
    {
    cout << "Enter a value: ";
    cout.flush(); // make sure the prompt is displayed.
    if (!(cin >> value))
    {
    invalid_input = true;
    }
    else
    {
    if (value > 6)
    {
    invalid_input = true;
    }
    }
    if (invalid_input)
    {
    cout << "\nInvalid input, try again.\n" << endl;
    }
    } while (invalid_input);

    Validating the user input is always a good thing.

    --
    Thomas Matthews

    C++ newsgroup welcome message:
    http://www.slack.net/~shiva/welcome.txt
    C++ Faq: http://www.parashift.com/c -faq-lite
    C Faq: http://www.eskimo.com/~scs/c-faq/top.html
    alt.comp.lang.learn.c-c++ faq:
    http://www.comeaucomputing.com/learn/faq/
    Other sites:
    http://www.josuttis.com -- C++ STL Library book
     
    Thomas Matthews, Aug 23, 2004
    #6
    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. Neil Zanella
    Replies:
    8
    Views:
    1,232
    mfmehdi
    Oct 20, 2006
  2. Devin Panchal

    if and else statements.

    Devin Panchal, Dec 9, 2003, in forum: Java
    Replies:
    2
    Views:
    462
    Devin Panchal
    Dec 9, 2003
  3. Harry George
    Replies:
    6
    Views:
    444
    Bart Nessux
    Feb 23, 2004
  4. Vince
    Replies:
    12
    Views:
    775
    Martin Gregorie
    Jan 21, 2008
  5. John Crichton
    Replies:
    6
    Views:
    289
    John Crichton
    Jul 12, 2010
Loading...

Share This Page