?
Solved

How to error check for Charters when using int

Posted on 2006-07-06
7
Medium Priority
?
294 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 1000 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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 tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
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.
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.
Suggested Courses
Course of the Month13 days, 2 hours left to enroll

777 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