Solved

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

Posted on 2006-07-08
10
306 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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
 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Linux context switch  - loop takes long time to process 6 115
How to learn Linux? 10 60
delete-remove 14 84
NEED HELP WITH VISUAL STUDIO 2017 (beginner) 6 38
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
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 and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

786 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