Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

[C++] Basic programming problem, user inputting wrong variable type

  1. Oct 15, 2012 #1
    I'm fairly new to programming. I'm doing an undergrad ENG degree and one of my first year mandatory courses is programming. It's my first time ever doing it so I don't know much yet. I'm currently learning C++. I'm in the middle of working on a problem that pretty much involves me making a calculator. A lot of my variable types are either int or double. If I have some while loops set up so if the user puts in an incorrect number it tells them to reenter, ie putting a a value in for sin^-1 that is greater than 1. However if I put a character in my program crashes.
    In the question it isn't mandatory that my calculator can handle this but I feel like my code is very unstable.
    How do I deal with this problem?
     
  2. jcsd
  3. Oct 15, 2012 #2

    Pythagorean

    User Avatar
    Gold Member

    Re: Basic programming problem, user inputting wrong variable type

    you use typeid to detect what data type is entered, then use if statements to handle the different cases. Not sure how that works out if you declare double but then enter a char there, though.
     
  4. Oct 15, 2012 #3
    Re: Basic programming problem, user inputting wrong variable type

    I'm not sure I understand. for example if this was my code:
    //other code
    double anynumber(0);
    cout<<"Please enter any number: ";
    cin>>anynumber;

    How would I use this typeid (function?) , if the user entered for example the character ' a '
     
  5. Oct 15, 2012 #4

    Pythagorean

    User Avatar
    Gold Member

    Re: Basic programming problem, user inputting wrong variable type

    It looks like 'isalpha' is another function (that tells you whether a variable is an alphabet character).

    You would use isalpha on the variable anynumber. Does it error on the cin>>anynumber line itself or later on when you try to implement anynumber?

    (C++ is not my native language by the way)
     
  6. Oct 16, 2012 #5

    chiro

    User Avatar
    Science Advisor

    Re: Basic programming problem, user inputting wrong variable type

    Hey tbarker5 and welcome to the forums.

    Since you have provided the standard input with a double, stdin should do all the formatting for you and the only thing left is to check for what is called a null value.

    But cin doesn't do this so you will have to look at the string itself and you can get the string by using getline function:

    http://www.cplusplus.com/reference/string/getline/

    You can do checks from as simple as the string not being empty to things like making sure the whole format is correct (digits 0-9, one decimal place, etc) but cin should do the formatting for you.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: [C++] Basic programming problem, user inputting wrong variable type
Loading...