Help with Input Checking

This is a discussion on Help with Input Checking within the C Programming forums, part of the General Programming Boards category; im writing a currency conversion program. I know its been covered, but my questions werent answered. quite frankly Im terrified ...

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    7

    Help with Input Checking

    im writing a currency conversion program. I know its been covered, but my questions werent answered.

    quite frankly Im terrified of you guys cuz of some of the responses I have seen...maybe its just me...but it seems like you folx are not very friendly to the newbies looking for help. but Im desperate for help, so here goes nothing.

    my current project goes like this:

    a simple program that asks for input, checks to make sure it is an actual number, performs conversion, and prints results.


    <<disclaimers>>
    I've pored over the FAQ's, so please do not jump on me about that.

    I'm doing a homework assignment, and DO NOT want anyone write my code. I am paying a fortune to go to school, so I want to learn it for myself. Therefore my intention is to obtain some explanations of a few things...IN ENGLISH...so I can understand how some of this stuff works.

    Im using the following compiler:

    Miracle C version 3.2, by Tadeusz Szocik, download it here

    I have access to the following textbooks:

    C Primer Plus, third edition
    Absolute Beginners guide to programming, second edition

    Im not necessarily looking for the "best" way. Im looking for the simplest way for a total newbie to achieve the desired result.


    here is the code that asks for input:

    Code:
    printf("Enter the amount of US Dollars you want to convert:\n"); 
    
    scanf("%f",&usd);
    I know that scanf is not necessarily the best way to get the input...thats not my question.

    My question:

    I want to use an IF to check if the input is a number....


    something like this (psuedocode):

    if usd = number, print conversion values
    else print "put in a number, dummy!" and go back to input request.


    thats it. thats all I want to do, but im not sure how the syntax would go.

    I have the currency conversion compiling and executing perfectly...except for checking to see if the input is a number or not. I would be happy to include the rest of the code if it would help.

    again, im not looking for someone to do my work for me. but I dont understand how to write this small part. maybe its a mental block or something, but I dont get it.

    if you read this and can help me out, thanks in advance.
    Last edited by Derek; 06-16-2003 at 12:56 PM.

  2. #2
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    The best way I can think of is to set usd to some invalidly high value (or even something like nan or inf). If scanf doesn't receive a number, it won't change the variable. Then you can test that variable against the value you set it to before.

  3. #3
    Registered User
    Join Date
    Jun 2003
    Posts
    7
    ok I have been toying around with some ideas here, and what i have here kinda works:

    Code:
    printf("Enter the amount of US Dollars you want to convert:\n");
    
    
    while (scanf("%f",&usd) != 1) 
    {
    while (getchar() != '\n');
    printf("You lose!\n\nPress Enter to exit"), scanf("1");
    return 0;
    //i put the scanf and the return 0 in there to end the prog at this point. 
    }
    printf("You entered $%.2f\n",usd);
    //now it will continue to the calculations
    could someone educate me a little bit here? this code does what I want it to...but I would like to know WHY it does it...what is it doing with this code? its not enough for me to simply make it work and turn it in...I want to actually learn this stuff and understand what im telling the computer to do.

    again, thanks in advance.

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    >My question:
    >I want to use an IF to check if the input is a number....

    You might check the return value of scanf. If it was able to successfully convert the user-entered text into a floating point value, I would say you've got a number. Some things will get past this, such as "12.34X" or "0x12" (resulting in scanned values of 12.34 and 0, respectively).

    >if usd = number, print conversion values
    >else print "put in a number, dummy!" and go back to input request.

    I read this as, "loop until a valid number is scanned". Here would be one way to do it.
    Code:
    #include <stdio.h>
    
    int main(void)
    {
       float usd;
       for ( ;; )
       {
          int ch;
          printf("Enter the amount of US Dollars you want to convert:\n");
          fflush(stdout);
          if ( scanf("%f", &usd) == 1 )
          {
             break; /* Successfully converted user response to a float value. */
          }
          puts("put in a number, dummy!");
          /* Since we chose to use scanf, we better do some cleanup. */
          while ( (ch = getchar()) != '\n' && ch != EOF );
       }
       printf("scanned %f\n", usd);
       return 0;
    }
    
    /* my output
    Enter the amount of US Dollars you want to convert:
    F001
    put in a number, dummy!
    Enter the amount of US Dollars you want to convert:
    
    yellow
    put in a number, dummy!
    Enter the amount of US Dollars you want to convert:
    123.456X
    scanned 123.456001
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Derek, don't be afraid to ask Just make sure you've tried your best before doing so, and give us the right info up front.

    With regards to you already reading the FAQ, I hope you read this page, that gives a sample program that is almost identical to your requirements? There are also more advanced (and safer) ways of doing the input when you get more comfortable with coding.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Registered User
    Join Date
    Jun 2003
    Posts
    7
    thanks Hammer.

    yes I did read that page, and at first it didnt make sense...this was last night. so i spent most of the night doing some more reading, being as thorough as I could to try and understand what i was reading.

    then today I spent more time looking at samples of source code and trying out different things.

    finally I gave up, and walked away for about an hour or so...

    when I came back, I had left that page up, and there staring me in the face was:

    Code:
    #include <stdio.h> 
    
    int main(void)
    {
      int temp;
      
      printf ("Input your number: ");
    
      while (scanf("%d", &temp) != 1)
      {
        while (getchar() != '\n');
        printf ("Try again: ");
      }
    
      printf ("You entered %d\n", temp);
      
      return(0);
    }
    and for some reason, my mind just went "click" and I saw clear as a bell how it would work for me.

    so I made a few changes to the print statements, and used my own variable, and after a few tries at the compiler, it worked!

    is that ok practice programming practice to use source that I find and adapt it for my purposes or is that considered plagiarism or otherwise bad practice? should I give you credit in my comments? if you compare the two sets of code you will see the changes I made.

    I definitely want to get more comfortable with this...what I need to do is learn exactly what it is that code is doing, that will help me understand what to do next time...and what I dont want to do...if you know what I mean.

    I had a massive rush when it compiled. Then it built....and then I hit the run button.....

    what a rush. Im so totally hooked. I cannot begin to describe the exhilaration I felt at having it actually work. Is this sick? Should I seek help? Have I broken the geek-o-meter?

  7. #7
    Registered User
    Join Date
    Jun 2003
    Posts
    7
    ok here is a dumb question...


    can I use visual c++ to write C programs? I probably can, cuz it opens up all the c programs I have by default... guess what I mean is, I have to write these thing in C for the class...

    I have visual studio 6.0, and it includes c++

    the only reason Im using the c compiler Im using now is due to the class Im taking, and that is it. damn thing gives me trouble all the time.

    thanks a bunch for the linkz, I will check it out. would much rather have a REAL compiler.
    Last edited by Derek; 06-17-2003 at 12:43 AM.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Derek
    ok here is a dumb question...

    can I use visual c++ to write C programs?
    ...
    would much rather have a REAL compiler.
    Yep. It'll work fine. I use it here. It's a nice IDE for C. The MSDN is a decent piece of work for reference. The tool-tip for function arguments is also a very handy feature.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  2. Checking input
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 05-26-2002, 03:06 AM
  3. Checking input
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 05-10-2002, 09:00 AM
  4. checking if input is a number ?
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 12-06-2001, 08:07 PM

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