Link to home
Start Free TrialLog in
Avatar of fsadewat
fsadewat

asked on

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;
}
Avatar of hongjun
hongjun
Flag of Singapore image

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
Avatar of fsadewat
fsadewat

ASKER

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
You have a weird compiler
Try this

if (scanf("%f",&US) >=1 && US >= 0f)
ASKER CERTIFIED SOLUTION
Avatar of AlexFM
AlexFM

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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?
It is the way your compiler works

Even this should work well

US >= 0
Thanks for the help AlexFM and hongjun on this problem.
I beleive that my post is accepted by mistake, I only added small tip. Ask Community Support to change this.
It is fine with me if fsadewat feels that was the right one.
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.