Solved

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

Posted on 2006-07-08
10
283 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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

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…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

708 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

13 Experts available now in Live!

Get 1:1 Help Now