Error checking user input

This is a discussion on Error checking user input within the C++ Programming forums, part of the General Programming Boards category; I am coding this currency conversion app (into to prog class) and need to error check the user inputs. Currently ...

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    1

    Error checking user input

    I am coding this currency conversion app (into to prog class) and need to error check the user inputs. Currently if a user inputs an ASCII alpha character it throws the app into an infinante loop.


    My logic is to simply check the input to see if its an integer:

    {
    (currency !== int) // ????????
    }

    However the HOW to do this escapes me. I simply do not want the user to enter in an Alpha character, but I dont want to explicitly limit the range either.

    Any suggestions?

    Below is my code for your review.

    Thanks in advance.


    Code:
    #include <iostream>
    #include <stdio.h>
    #include <stdlib.h>
    
    using namespace std;
    
        //................................
        // defines constants
        //................................
       
    #define EUR 1.07892
    #define BRI 1.57719
    #define JAP 0.00835838
    #define AUS 0.601142
    #define IRA 3.21647
    
    int main ()
    
    {
    
        //................................
        //head
        //................................
        
        cout<<"Currency Conversion Calculator\n\n";
    
        //.................................
        //Declarations of variables
        //.................................
        
        int input = 0; 
        double currency = 0; 
        double EURC = 0; 
        double BRIC = 0; 
        double JAPC = 0; 
        double AUSC = 0; 
        double IRAC = 0; 
     
        
        //.................................    
        //request user input
        //................................
        
         while (input == 0 || !(input>= 1 && input <= 5))
             
        {
    
         cout<<"Select from the following 5 options:\n\n";
    
         cout<<"To convert the Euro to US $, please press 1\n\n";
    
         cout<<"To convert the British Pound to US $, please press 2\n\n";
    
         cout<<"To convert the Yen to US $, please press 3\n\n";
    
         cout<<"To convert the Australian $ to US $, please press 4\n\n";
    
         cout<<"To convert the Iraqi Dinar to US $, please press 5\n\n";
         
         cout<<"....:";
         
         cin>> input; 
         
         cout<<"\n";
         
        }
    
     
        //.................................    
        // request user's amount to be converted 
        //................................
        {
            
         cout<<"Input the Currency Ammount to be Converted: ";
         cin>> currency;
         
        
        //................................
        // Conversion table
        //................................
        
         EURC = currency * EUR;
         BRIC = currency * BRI;
         JAPC = currency * JAP;
         AUSC = currency * AUS;
         IRAC = currency * IRA;
          
        }  
        
    /*  switch(currency)
    
        {
         
         case1:   
         {   
         
         (currency !=int)
         
         }
         
         default: 
         {
             printf("Invalid input, please enter a valid currency. \n\n");
             break;
         }
           
          
    
        }
    */
        //................................
        // Calculate conversions and print
        //................................
        
        switch(input)
        {
        
         case 1: 
         {
              printf ("%0.2lf Euros is equal to %0.2lf US dollars\n\n", currency, EURC);
              break;
         }
         case 2:  
         
         {
              printf ("%0.2lf British pounds is equal to %0.2lf US dollars\n\n", currency, BRIC);
              break;
         }
         
         case 3:  
         
         {
              printf ("%0.2lf Yen is equal to %0.2lf US dollars\n\n", currency, JAPC);
              break;
         }
         
         case 4:  
         {
              printf ("%0.2lf Australian dollars is equal to %0.2lf US dollars\n\n", currency, AUSC);
              break;
              
         }    
         
         case 5:
         {    printf ("%0.2lf Iraqi Dinar is equal to %0.2lf US dollars\n\n", currency, IRAC);
              break;
              
         }    
         
         
         default: 
         {
             printf("Invalid input, please enter a valid currency. \n\n");
             break;
         }
           
        }
        //................................
        // stablize shell
        //................................
        
      system("PAUSE");	
      
        //................................
        // statisfy interger expectation of int main
        //................................ 
        
      return 0;
    }

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    There are several solutions. One solution is to read the input one line at a time. Another solution is to read each character at a time. In both cases, use isdigit() to validate an integer.

    Kuphryn

  3. #3
    Registered User
    Join Date
    Jun 2003
    Posts
    6
    isdigit() is in ctype.h

  4. #4
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    This:

    cin>> currency;


    is *already* checking to see if it's a number. If it's not a valid double, cin will enter a fail state, so cin will evaluate to false. When the error does happen, you're going to need to find the error and reset the stream completely.

    Here's a little example for you:

    Code:
    #include <iostream>
    
    
    int main(){
    	double d;
    
    	std::cout << "Enter a number > ";
    	std::cin >> d;
    
    	while (!std::cin){
    		std::cout << "Not a number! Try again. > ";
    		std::cin.clear(); // reset the stream to a good state
    		std::cin.ignore(0x7FFFL,'\n'); // Ignore the whole buffer
    		std::cin >> d;
    	}
    
    	std::cout << "You entered " << d << std::endl;
    }
    Here's one sample output:

    Code:
    Enter a number > a
    Not a number! Try again. > 37.gh
    You entered 37
    Note that there is still information in the stream (namely, gh) so you would have to detect and purge it. Note also that "37.gh" is legal to scan for a double because 37. is a legal number. You'll need more sophistication than this to deal with that input.

    Also, cin>>d cannot HELP but leave you with a number in d. Doubles cannot hold anything besides numbers, ever.

    One possibility is to use the string class (#include <string>) and then use std::getline(std::cin, myString); to read an entire line into a string; then you can process the string to get the character.
    Last edited by Cat; 07-21-2003 at 08:35 PM.

  5. #5
    Registered User
    Join Date
    Oct 2002
    Posts
    291
    Any special reason why you use 0x7fff (32767) as max number of char's to be discarded ?

    Code:
    std::cin.ignore(0x7FFFL,'\n'); // Ignore the whole buffer

  6. #6
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    It was big enough to fit the bill (who is going to type that much text, anyway) and it's not possible to be interpreted by the compiler as a negative number.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  2. SSH Hacker Activity!! AAHHH!!
    By Kleid-0 in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 03-06-2005, 02:53 PM
  3. Nested Structures - User Input
    By shazg2000 in forum C Programming
    Replies: 2
    Last Post: 01-09-2005, 09:53 AM
  4. ~ User Input script help~
    By indy in forum C Programming
    Replies: 4
    Last Post: 12-02-2003, 05:01 AM
  5. Getting user input for filename loop
    By jpchand in forum C++ Programming
    Replies: 1
    Last Post: 09-16-2003, 06:37 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21