Thread: Question on getting making sure input is a number

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    9

    Question on getting making sure input is a number

    In my program I need the user to input a price. The purchase price must be a number.


    My assignment says:
    Ignore any data that may be on the life after the amount. In a non-numeric is entered for the price, issue an error message, and ask the user to input the price again.

    Does anybody know how to do this? Thank you.

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    wpr101,

    >Does anybody know how to do this?
    Short answer? Yes, a great many of us do.

    Long(er) answer? We help with problems that you've attempted to solve first yourself, particularly with school assignments.

    Let us know what you think about the solution to this problem and we can go from there.

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

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    9

    Post

    well since it is a float it should ignore any none numeric info after a number is entered I believe.

    i was thinking use cin.get (float)? but that would only get one value, or use multiple cin.gets, but then I don't know how long the price is going to be. I've thought it through and still am unsure. Can you help me please?

  4. #4
    Registered User
    Join Date
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    You might find that reading in a string is easier, since cin will blow up if you say you are reading a float and I enter "I'm a real dummy". If you read in a string and I do enter this, you can do an if check and come right back with

    cout<<"As the matter of fact you are, but thats not what I asked. Please try again"<<endl;

    and the user will be so Impressed that they will be sure to get it right the second time around.

    After all that, you can do a search on google for strtod or stringstream (if you are feeling really adventurous) and see how you can manage to convert a string to a float.
    The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.

  5. #5
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    take it as a char array input then check each element of the array to make sure its ASCII code is that of a number with a compound if statement.

    If it goes through without finding a non-numerical character, use atof() to convert it to a float, and voila.

  6. #6
    Registered User
    Join Date
    Oct 2002
    Posts
    9
    Is there an easier way to do it than using arrays? thanks

  7. #7
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    you might be able to get by with something like this

    Code:
    int c;
    float f;
    
    c = fgetc(stdin);
    if (!isdigit(c))
    {
           if (c == '.' || c == '-')
           {
                  int d = fgetc(stdin);
                  if (!isdigit(d))
                         error();
                  ungetc(d, stdin);
            }
            else
            {
                  error();
            }
    }
    ungetc(c, stdin);
    if (scanf("%f", &f) != 1)
    {
            error();
    }

  8. #8
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    you will also probably want to skip white space before
    doing it so put in something like
    Code:
    while(isspace(c = fgetc(stdin)))
            continue;
    ungetc(c, stdin);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File input in C++ - S1mple question...
    By darknite135 in forum C++ Programming
    Replies: 4
    Last Post: 12-23-2007, 08:44 AM
  2. adding a number to a number
    By bigmac(rexdale) in forum C Programming
    Replies: 11
    Last Post: 10-24-2007, 12:56 PM
  3. Number input and formatted output
    By scoobasean in forum C++ Programming
    Replies: 4
    Last Post: 12-20-2004, 11:17 PM
  4. File input question
    By Beast() in forum C Programming
    Replies: 16
    Last Post: 07-09-2004, 03:23 PM
  5. question on visual c++ (C) 6.0 - help
    By momo20016 in forum C Programming
    Replies: 1
    Last Post: 12-19-2002, 09:13 AM