Nebie in need

This is a discussion on Nebie in need within the C++ Programming forums, part of the General Programming Boards category; I'm pretty new and teaching myself threw tutorials. One issue in the use of the programs i make is the ...

  1. #1
    Unregistered
    Guest

    Nebie in need

    I'm pretty new and teaching myself threw tutorials. One issue in the use of the programs i make is the user can easily crash the program by inputting a variable of the wrong data type. Currently the variable is an int, and if anyone inputs a char, it sends the program into a continuous loop.
    If there is a way to limit the user or a data type that will support both integers and chars, i would love to know.

    Thank you immensly in advance.

  2. #2
    Shadow12345
    Guest
    could you post your problematic code for me to see?? It is possible you aren't doing something right in the program itself.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    The best way to avoid this problem is to read all input as string data and then convert it as necessary to integer or floating point values.

    -Prelude
    My best code is written with the delete key.

  4. #4
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493

    Re: Nebie in need

    Originally posted by Unregistered
    If there is a way to limit the user or a data type that will support both integers and chars, i would love to know.

    Thank you immensly in advance.
    just fyi, in variable length functions, all variables are promoted upwards so that it can't tell the difference between int and char. (for instance, scanf and printf are variable length functions). there's probably a way to check for this kind of error using cin, though.


    (in hindsight the above shouldn't apply anyway in your situation, but it's there for reference. follow prelude's advice)

  5. #5
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    Do you mean like cin.fail()?

  6. #6
    Sir Mister Insane Sako Klinerr1's Avatar
    Join Date
    May 2002
    Posts
    608
    i think hes in my problem in witch he has a c++ calculator the user is supposed to input a number but the user puts ina letter and its hard to add f+3. (unless f is a variable)
    Email: Klinerr1@nc.rr.com || AIM: MisterSako || MSN: sakotheinsane@hotmail.com

    -the shroom has spoken

  7. #7
    Unregistered
    Guest
    cin.fail() and/or cin.good() in combination with cin.clear() can be used to do at least some input error screening, although not all input error screening.

  8. #8
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    cin.good() works well for the first character. Absolutely agreed. After that, it's anyone's guess. Testing with cin.good() for a float value, as an example, "1abc.def" passes. Not at all a good thing.

    Shadow makes a good point, as well. If the expected input is restricted to 0-9, cin.good() will do the job. If your expected input is of an 'int' type, potentially greater than 9, cin.good() will not do the job properly for you. Upshot: This will depend on your code and what you are trying to accomplish.

    As usual, Prelude is correct. (There's a news flash .) Simply put, you can isalpha()/isdigit()/iswhatIwant() yourself into Never-Never Land and you won't come up with pre-defined code that allows you to properly check for the input you want without "brute-forcing" it.

    (Skipper's note: "brute-forcing" is programmer-dependent and a subjective term. What one person finds to be a daunting task is a 'no-brainer' for someone else. An uncomfortable truth, but a truth nonetheless, particularly at the stage that you're at.)

    Hang in there. You'll get it.

    -Skipper
    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

  9. #9
    Unregistered
    Guest
    Damn, you people know alot :)
    The expected input is 1-9, so what do i do with the cin.good() ?

    Thanks again.

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >The expected input is 1-9, so what do i do with the cin.good() ?
    This method of reading input is imprecise and easily messed up if you don't cover for every possible occurance. My recommendation of reading everything into a string with getline and then validating that string still holds. But if you must do it the other way, here is one possible implementation:
    Code:
    #include <iostream>
    
    using std::cin;
    using std::cout;
    using std::cerr;
    
    int main()
    {
      int input;
      bool done = false;
    
      while ( !done ) {
        cout<<"Enter a number 1 - 9: ";
        cin>>input;
        if ( !cin.good() ) {
          cerr<<"Invalid input\n";
          /*
          ** C I/O was always easier to work with. :p
          */
          while ( getchar() != '\n' );
          cin.clear();
        }
        else {
          if ( input < 1 || 9 < input )
            cerr<<"Invalid number\n";
          else
            done = true;
        }
      }
      cout<<"Your number is "<< input <<"\n";
      return 0;
    }
    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. nebie simply needs help creating 2 dimensional char array
    By MegaManZZ in forum C++ Programming
    Replies: 13
    Last Post: 01-14-2008, 09:13 AM
  2. pointer functions - nebie question
    By bbresc512 in forum C Programming
    Replies: 3
    Last Post: 03-24-2006, 12:36 PM
  3. nebie help
    By yonjun in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2006, 10:48 PM
  4. Help! nebie help
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 11-11-2001, 10:27 AM

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