Solved

How to error check for Charters when using int

Posted on 2006-07-06
7
286 Views
Last Modified: 2010-04-15
I am looking for a statement that can check for charter input when needing only input of numbers. I tried to use isdigit but a char is need instead of int when checking. Is there any way of using isdigit by coverting int to char? if not what other statements can be used to check input for charters. Here is example code for my small program.

#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 or negative numbers only.\n");               /* print display */
        printf("--------------------------------------\n");                      /* print display */
        printf("\nPlease enter a Austrialia Dollar Amount: $ ");        /* Enter Dollar Amount */  
      scanf("%f",&US);                                                          
        printf("\n%.2f = US Dollars ",US/AUD);                               /* print display */
        printf("--------------\n\n");                                                 /* print display */
               
        if (isdigit()) /* what statement should be used? */
      Printf("\nthank you");  
      else
      printf("\nYou did not enter a negative or postive number. Please re-enter number\n");        
   
    getch();   /* WAITS UNTIL A KEY IS PRESSED, getch() WAIT FOR A KEY AND TELLS YOU WHICH */
    return 0;
}
0
Comment
Question by:fsadewat
  • 3
  • 3
7 Comments
 
LVL 12

Expert Comment

by:rajeev_devin
ID: 17056318
You can simply check in this way

if (scanf("%f",&US) >= 1)
      printf("\nthank you");  
else
      printf("\nYou did not enter a negative or postive number. Please re-enter number\n");
0
 
LVL 12

Accepted Solution

by:
rajeev_devin earned 250 total points
ID: 17056325
Your complete code would be something like this

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 or negative numbers only.\n");               /* print display */
      printf("--------------------------------------\n");                      /* print display */
      printf("\nPlease enter a Austrialia Dollar Amount: $ ");        /* Enter Dollar Amount */  
      
      if (scanf("%f",&US) >= 1)
            printf("\nthank you");  
      else
            printf("\nYou did not enter a negative or postive number. Please re-enter number\n");
      
      printf("\n%.2f = US Dollars ",US/AUD);                               /* print display */
      printf("--------------\n\n");                                                 /* print display */
      
      getch();   /* WAITS UNTIL A KEY IS PRESSED, getch() WAIT FOR A KEY AND TELLS YOU WHICH */
    return 0;
}
0
 

Author Comment

by:fsadewat
ID: 17056393
Thank you for the help it was much needed.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 5

Expert Comment

by:cryptosid
ID: 17056397
Hi,

Another way is to use gets() function to read the string and use sprintf() function to convert it into whichever variable you want.

Regards,
Siddhesh
0
 

Author Comment

by:fsadewat
ID: 17063850
What if I wanted to only use positive numbers how would the statement change ?
0
 
LVL 12

Expert Comment

by:rajeev_devin
ID: 17064281
Simply change the if statement
 int ret = scanf("%f",&US);
 if (ret >= 1) && US > 0.0) // Check if it is valid and positive value
       printf("\nthank you");  
 else
       printf("\nYou did not enter a postive number. Please re-enter number\n");
0
 

Author Comment

by:fsadewat
ID: 17064628
used this statement and it worked but had a problem with if (scanf("%f",&US) >=1 && US >= 0)  or if (scanf("%f",&US) >=1 && US >= 0f) had to add 0.0f to work. Worked this problem out on another question sorry you did not see the other question for the points. I am still not clear why the first statement did not work but it was blamed on my compiler. Thanks for the response to my question. The program works great now!

Working statement
if (scanf("%f",&US) >=1 && US >= 0.0f)  
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

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…
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 pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.

706 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

20 Experts available now in Live!

Get 1:1 Help Now