Solved

How to error check for Charters when using int

Posted on 2006-07-06
7
293 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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

705 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