Solved

How to error check for Charters when using int

Posted on 2006-07-06
7
289 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
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!

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
c language help - file paths 7 141
Detect CR LF to each line 12 152
What is atomic operation? 6 46
Computer slow / BSOD 10 42
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 how to use strings and some functions related to them in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops 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

21 Experts available now in Live!

Get 1:1 Help Now