Solved

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

Posted on 2006-07-08
10
295 Views
Last Modified: 2010-04-15
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;
}
0
Comment
Question by:fsadewat
  • 4
  • 4
  • 2
10 Comments
 
LVL 33

Expert Comment

by:hongjun
ID: 17064059
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
 

Author Comment

by:fsadewat
ID: 17064078
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
 
LVL 33

Expert Comment

by:hongjun
ID: 17064082
You have a weird compiler
Try this

if (scanf("%f",&US) >=1 && US >= 0f)
0
 
LVL 48

Accepted Solution

by:
AlexFM earned 500 total points
ID: 17064088
if (scanf("%f",&US) >=1 && US >= 0.0f)
0
 

Author Comment

by:fsadewat
ID: 17064108
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
Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

 
LVL 33

Expert Comment

by:hongjun
ID: 17064122
It is the way your compiler works

Even this should work well

US >= 0
0
 

Author Comment

by:fsadewat
ID: 17064265
Thanks for the help AlexFM and hongjun on this problem.
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 17064273
I beleive that my post is accepted by mistake, I only added small tip. Ask Community Support to change this.
0
 
LVL 33

Expert Comment

by:hongjun
ID: 17064292
It is fine with me if fsadewat feels that was the right one.
0
 

Author Comment

by:fsadewat
ID: 17064368
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

Featured Post

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now