can anyone help me with this?

Discussion in 'C++' started by Skysword, Aug 7, 2004.

  1. Skysword

    Skysword Guest

    Hi there,
    I got a problem with this. If I have a fuction like the following
    bool valid(int m, int n)
    { if(!isdigit(m)||!isdigit(n))
    return false;
    else
    return true;
    }

    int main()
    { int a,b;
    cin>>a>>b;
    cout<<valid(a,b);
    system("pause");
    return 0;
    }

    when I input "b 3", the program crashes, it's suppose to print "0",
    right? how could this happen? how can I fix it? I want foo to ensure
    input for a and b are in a certain range and do not accept letters, of
    course

    thanks!~

    Joseph
     
    Skysword, Aug 7, 2004
    #1
    1. Advertising

  2. Skysword

    Wiseguy Guest

    (Skysword) tried to express:
    > Hi there,
    > I got a problem with this. If I have a fuction like the following
    > bool valid(int m, int n)
    > { if(!isdigit(m)||!isdigit(n))
    > return false;
    > else
    > return true;
    > }
    >
    > int main()
    > { int a,b;
    > cin>>a>>b;
    > cout<<valid(a,b);
    > system("pause");
    > return 0;
    > }
    >
    > when I input "b 3", the program crashes, it's suppose to print "0",
    > right? how could this happen? how can I fix it? I want foo to ensure
    > input for a and b are in a certain range and do not accept letters, of
    > course


    the most likely culprit is that you are entering a (char) "b" when an
    (int) is expected for (int a).

    You need to read in a (string) of characters and parse the arguments by
    one of any number of different methods.
     
    Wiseguy, Aug 7, 2004
    #2
    1. Advertising

  3. Skysword

    Sharad Kala Guest

    "Skysword" <> wrote in message
    news:...
    > Hi there,
    > I got a problem with this. If I have a fuction like the following
    > bool valid(int m, int n)
    > { if(!isdigit(m)||!isdigit(n))
    > return false;
    > else
    > return true;
    > }
    >
    > int main()
    > { int a,b;
    > cin>>a>>b;


    When you input "b 3" cin goes into a bad state. No operations on a stream
    work once it goes into a bad state. So a and b hold garbage values.
    The way to check if the user has given right input here is to simply check
    state of cin.
    if (cin)
    {
    // ...
    }
    > cout<<valid(a,b);


    This will fail because a library could make some kind of checks on input
    given to it. On MS VC 7 this check is made -

    _ASSERTE((unsigned)(c + 1) <= 256);


    > system("pause");
    > return 0;
    > }
    >
    > when I input "b 3", the program crashes, it's suppose to print "0",


    I would use isdigit for character inputs. If I am using ints then checking
    stream state is good enough.

    -Sharad
     
    Sharad Kala, Aug 7, 2004
    #3
  4. Skysword

    Sharad Kala Guest


    > This will fail because a library could make some kind of checks on input
    > given to it. On MS VC 7 this check is made -
    >
    > _ASSERTE((unsigned)(c + 1) <= 256);


    in the isdigit function.
     
    Sharad Kala, Aug 7, 2004
    #4
  5. On 7 Aug 2004 00:01:30 -0700, Skysword <> wrote:

    > Hi there,
    > I got a problem with this. If I have a fuction like the following
    > bool valid(int m, int n)
    > { if(!isdigit(m)||!isdigit(n))
    > return false;
    > else
    > return true;
    > }
    >
    > int main()
    > { int a,b;
    > cin>>a>>b;
    > cout<<valid(a,b);
    > system("pause");
    > return 0;
    > }
    >
    > when I input "b 3", the program crashes, it's suppose to print "0",
    > right? how could this happen? how can I fix it? I want foo to ensure
    > input for a and b are in a certain range and do not accept letters, of
    > course
    >
    > thanks!~
    >
    > Joseph


    You are inputing a character 'b' when your program is expecting an integer
    because you said 'int a'. It the usual situation in programming, the
    program is doing exactly what you asked it to, not what you thought you
    asked it to.

    Here is one way to detect bad input.

    #include <iostream>
    #include <limits.h> // for INT_MAX
    using namespace std;

    int a;
    for (;;)
    {
    // prompt the user
    cout << "Enter a number from 0 to 999 ";
    // read integer and check if entered correctly and in correct range
    if (cin >> a && a >= 0 && a <= 999)
    break;
    // cin might be bad, so clear it
    cin.clear();
    // disard any extraneous input from cin
    cin.ignore(INT_MAX);
    // ask the user to input again
    cout << "Please try again\n";
    }

    Look up clear() and ignore() in you favourite C++ book.

    This code would be a good candidate to put into a function if you have
    several numbers to read.

    john
     
    John Harrison, Aug 7, 2004
    #5
  6. On Sat, 07 Aug 2004 09:15:59 +0100, John Harrison
    <> wrote:

    > Here is one way to detect bad input.
    >
    > #include <iostream>
    > #include <limits.h> // for INT_MAX
    > using namespace std;
    >
    > int a;
    > for (;;)
    > {
    > // prompt the user
    > cout << "Enter a number from 0 to 999 ";
    > // read integer and check if entered correctly and in correct range
    > if (cin >> a && a >= 0 && a <= 999)
    > break;
    > // cin might be bad, so clear it
    > cin.clear();
    > // disard any extraneous input from cin
    > cin.ignore(INT_MAX);
    > // ask the user to input again
    > cout << "Please try again\n";
    > }
    >


    This is actually pretty cheap and nasty code, it shouldn't really have
    suggested it. Suppose your user type in '1b23', I would imagine that you
    would want to report this as an error. But this code will not report an
    error, instead it will read '1' into variable a and then leave 'b23'
    unread. Next time you start to read from cin, you will start by reading
    'b23'.

    If oyu want to do this properly you should take Wiseguy's suggestion, read
    the input into a string, check that the string is exactly what you want
    (using isdigit for instance) and then convert the string into an integer.

    john
     
    John Harrison, Aug 7, 2004
    #6
  7. In message <>, Skysword
    <> writes
    >Hi there,
    >I got a problem with this. If I have a fuction like the following
    >bool valid(int m, int n)
    >{ if(!isdigit(m)||!isdigit(n))
    > return false;
    > else
    > return true;
    >}
    >


    Others have explained what's wrong with the rest of the program, so I'll
    just remark that that's an unnecessarily brain-bendingly convoluted way
    of writing

    bool valid(int m, int n)
    {
    return isdigit(m) && isdigit(n);
    }

    Just because the return type is 'bool', you don't have to return a
    literal 'true' or 'false'.
    --
    Richard Herring
     
    Richard Herring, Aug 17, 2004
    #7
  8. Richard Herring wrote:
    > In message <>, Skysword
    > <> writes
    >
    >> Hi there,
    >> I got a problem with this. If I have a fuction like the following
    >> bool valid(int m, int n)
    >> { if(!isdigit(m)||!isdigit(n))
    >> return false;
    >> else
    >> return true;
    >> }
    >>

    >
    > Others have explained what's wrong with the rest of the program, so I'll
    > just remark that that's an unnecessarily brain-bendingly convoluted way
    > of writing
    >
    > bool valid(int m, int n)
    > {
    > return isdigit(m) && isdigit(n);
    > }
    >
    > Just because the return type is 'bool', you don't have to return a
    > literal 'true' or 'false'.


    Oh man - you just reduced his line count by 3 and made him 43% less
    productive... :)
     
    Gianni Mariani, Aug 17, 2004
    #8
    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. JDS

    Anyone?? Anyone at all??

    JDS, Sep 26, 2005, in forum: HTML
    Replies:
    2
    Views:
    395
    Montgomery BOO...URNS
    Sep 29, 2005
  2. Steve Williams

    Anyone? Anyone?

    Steve Williams, Sep 16, 2003, in forum: Python
    Replies:
    0
    Views:
    394
    Steve Williams
    Sep 16, 2003
  3. Replies:
    30
    Views:
    997
    Arne Vajhøj
    Feb 10, 2008
  4. gregarican

    Anyone, anyone...Bueller?

    gregarican, Jun 22, 2007, in forum: Ruby
    Replies:
    2
    Views:
    104
    gregarican
    Jun 22, 2007
  5. 7stud --
    Replies:
    4
    Views:
    129
    7stud --
    Mar 27, 2009
Loading...

Share This Page