Solved

How to error check for Charters when using int

Posted on 2006-07-06
7
291 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
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.

 
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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
c++ VC2015.  Run-Time Check Failure #2 - Stack around the variable 'house' was corrupted. 2 235
C++ vs C compilers 13 160
valid enum? 6 75
nested if statement in excel help 4 37
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…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

832 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