• C

limit to postive numbers and no characters for the output of scanf ()

I want to use the scanf () statement to check so that it only excepts positve numbers and will not except negative numbers or characters. If this can not be done what other method should be used to limit the output of the scanf statement ? Example code below.

#include <stdio.h>              /* To Accept One Input Currency */


int main()        
{                      
     
     //Declare main function variables and initalize as needed
     
     /*beginning functionality*/ //Display foreign and us currencies //
     
     float AUD,US;
     
     AUD=0.742151f;      /* Austrialia Dollar */
     US=1.00f;          /* United States Dollar */
     
     
   
     printf("\n\nU.S. Dollars Currency Conversion For Austrialia Dollar.\n"); /* print display */
     printf("--------------------------------------\n");                      /* print display */
     printf("Use postive numbers only.\n");                                   /* print display */
     printf("--------------------------------------\n");                      /* print display */
     printf("\nPlease enter a Austrialia Dollar Amount: $ ");           /* Enter Dollar Amount */  
     
     
     if (scanf("%f",&US) >=1)                   /* error check for characters and negative numbers */  
         printf("\nConversion");
     else
          printf("\nYou enter negative number or character. Program ended\n");
     
     printf("\n%.2f = US Dollars ",US/AUD);                       /* print display */
     printf("--------------\n\n");                                        /* print display */
     printf("hit the enter key to close screen. The Program ended \n");                                          /* print display */
     
     getch();   /* WAITS UNTIL A KEY IS PRESSED, getch() WAIT FOR A KEY AND TELLS YOU WHICH */
    return 0;
}
fsadewatAsked:
Who is Participating?
 
AlexFMConnect With a Mentor Commented:
if (scanf("%f",&US) >=1 && US >= 0.0f)
0
 
hongjunCommented:
Try this

#include <stdio.h>              /* To Accept One Input Currency */

int main()        
{                      
   
    //Declare main function variables and initalize as needed
   
    /*beginning functionality*/ //Display foreign and us currencies //
   
    float AUD,US;
   
    AUD=0.742151f;      /* Austrialia Dollar */
    US=1.00f;          /* United States Dollar */
   
    printf("\n\nU.S. Dollars Currency Conversion For Austrialia Dollar.\n"); /* print display */
    printf("--------------------------------------\n");                      /* print display */
    printf("Use postive numbers only.\n");                                   /* print display */
    printf("--------------------------------------\n");                      /* print display */
    printf("\nPlease enter a Austrialia Dollar Amount: $ ");           /* Enter Dollar Amount */  
   
   
    if (scanf("%f",&US) >=1 && US >= 0)                   /* error check for characters and negative numbers */  
    {
        printf("\nConversion");
        printf("\n%.2f = US Dollars ",US/AUD);                       /* print display */
        printf("--------------\n\n");                                        /* print display */
    }
    else
        printf("\nYou enter negative number or character. Program ended\n");
   
    printf("hit the enter key to close screen. The Program ended \n");                                          /* print display */
   
    getch();   /* WAITS UNTIL A KEY IS PRESSED, getch() WAIT FOR A KEY AND TELLS YOU WHICH */
    return 0;
}




hongjun
0
 
fsadewatAuthor Commented:
Hongjun I tried this before but I keep getting an error when compling

Miracle C Compiler (r3.2), written by bts.
Compiling c:\program files\miracle c\currency_converison_one_currency_ver1.c
main

c:\program files\miracle c\currency_converison_one_currency_ver1.c: line 25: unrecognised types in comparison
'if (scanf("%f",&US) >=1 && US >= 0)                      printf("\nConversion")'
aborting compile
0
Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

 
hongjunCommented:
You have a weird compiler
Try this

if (scanf("%f",&US) >=1 && US >= 0f)
0
 
fsadewatAuthor Commented:
Hongjun using your statement now it  gives a different error

Miracle C Compiler (r3.2), written by bts.
Compiling c:\program files\miracle c\currency_converison_one_currency_ver1.c
main

c:\program files\miracle c\currency_converison_one_currency_ver1.c: line 25: lexical: nondigits in number and not hexadecimal
'if (scanf("%f",&US) >=1 && US >= 0f)                    printf("\nConversion")'
aborting compile

However, AlexFM yours worked great. Is the reason why yours worked is because of the decimal placement in the float US=1.00f and or because of the way the compiler reads the code?
0
 
hongjunCommented:
It is the way your compiler works

Even this should work well

US >= 0
0
 
fsadewatAuthor Commented:
Thanks for the help AlexFM and hongjun on this problem.
0
 
AlexFMCommented:
I beleive that my post is accepted by mistake, I only added small tip. Ask Community Support to change this.
0
 
hongjunCommented:
It is fine with me if fsadewat feels that was the right one.
0
 
fsadewatAuthor Commented:
AlexFM your comment worked but if you want to split the points that is fine with me.  I accepted your comment because it solved the problem and it is my understanding that this is the way awarding points works. If I am wrong my apologies to you both.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.