Thread: Input check problem with numbers

  1. #31
    Registered User
    Join Date
    Aug 2007
    Posts
    66
    I study your example and I have to say that is NOT good to combine the cin.ignore() method with the method of conversion. There is a problem in input stream and it's difficult to discover it. Anyway here is my source:

    Do you think that I can avoid the cin.clear and cin.ignore parts ?

    Code:
    // 
    // File:   temp.cc
    // Author: blackslash13
    //
    // Created on 29 Νοέμβριος 2007, 11:06 μμ
    //
    
    #include <stdlib.h>
    #include <iostream>
    #include <string>
    #include <sstream>
    #include <iomanip>
    #include <limits>
    
    bool inputDouble(std::string prompt, double &val)
    {
    	std::string input;
    	for(;;) 
    	{
    		std::stringstream sstr;
    		std::cout << prompt;
    		if (!std::getline(std::cin, input))   
    			return false;  // Failed to read input. 
    		sstr << input;
    		if (sstr >> val) {
    			return true;
    		}
    		else
    		{
    			std::cout << "Error, not a valid number" << std::endl;
    		}
    	}
    }
    
    double fahrenheitToCelsius(double fahrenheit)
    {
        double celsius;
        
        celsius = (fahrenheit-32)/1.8;
        return  celsius;
    }
    
    double celsiusToFahrenheit(double celsius)
    {
    	double fahrenheit;
    	
    	fahrenheit = 1.8 * celsius + 32 ;
    	return fahrenheit;
    }
    
    void  makeTable(double start, double end, double step, double choice)
    {
    	using namespace std;
    	cout.setf(ios::fixed);
    	double i(0),fahr,celsius;
            
            if(choice==1)
            {
                	for(i=start; i<=end; i+=step)
                    {	
    		fahr=celsiusToFahrenheit(i);
    		cout << setprecision(2) << setw(5) << i << "\t\t" <<  fahr << endl;
                    }
            }
            else
            {
                for(i=start; i<=end; i+=step)
                    {	
    		celsius=fahrenheitToCelsius(i);
    		cout << setprecision(2) << setw(5) << i << "\t\t" <<  celsius << endl;
                    }
                
            }
    
    }
    	
    
    //
    // 
    //
    int main(int argc, char** argv) {
        
        {
    	using namespace std;
    	
    	double choice,choice2;
    	double degree,lowBound,maxBound,step;
    	
    	cout << "Select a number" << endl
    	     << "1. Celsius to Fahrenheit\n2. Fahrenheit to Celsius" << endl
    	     << "Your Choice:";
    	
    	cin >> choice;
    	
    	while(!cin.good() || choice<1 || choice>2)
    	{
    		cin.clear();
    		cin.ignore(numeric_limits<streamsize>::max(),'\n');
    		cout << "Not valid selection.... Your Choice:";
    		cin >> choice;
    	}
    	
    	if(choice == 1)
    	{
    		cout << "Select a number" << endl
    	     	<< "1. Specific conversion\n2. Print table list" << endl
    	     	<< "Your Choice:";
    		cin >> choice2;
                    
                    
    	
    		while(!cin.good() || choice2<1 || choice2>2)
    		{
    			cin.clear();
    			cin.ignore(numeric_limits<streamsize>::max(),'\n');
    			cout << "Not valid selection.... Your Choice:";
    			cin >> choice2;
    		}
                    
                    /* Thrash the garbage from input strem */
                    cin.clear();
    		cin.ignore(1,'\n');
                    
    		if(choice2==1)
    		{
                            
                      	while(!inputDouble("Enter Celsius temperature degree:", degree) || degree<=-316 || degree>=316)
    			{
    				cout << "Not valid selection....";
    			}
    			
    			cout << degree << " Celsius = " << celsiusToFahrenheit(degree) << " Fahrenheit." << endl;
    		}
    		else
    		{
    			while(!inputDouble("Enter Celsius lower bound degree:",lowBound) || lowBound<=-316 || lowBound>=316)
    			{
    				cout << "Not valid selection....";
    			}
    
    			while(!inputDouble("Enter Celsius maximum bound degree:",maxBound) || maxBound<=-316 || maxBound>=316)
    			{
    				cout << "Not valid selection....";
    			}
    
    			while(!inputDouble("Enter increment value for the list:",step) ||  step<=0)
    			{
    				cout << "Not valid selection....";
    			}
    			
    			cout << "Celsius\t\tFahrenheit" << endl;
    			makeTable(lowBound, maxBound, step,1);
    		}
            }
            else
    	{
               	cout << "Select a number" << endl
    	     	<< "1. Specific conversion\n2. Print table list" << endl
    	     	<< "Your Choice:";
    		cin >> choice2;
                    
                    
    	
    		while(!cin.good() || choice2<1 || choice2>2)
    		{
    			cin.clear();
    			cin.ignore(numeric_limits<streamsize>::max(),'\n');
    			cout << "Not valid selection.... Your Choice:";
    			cin >> choice2;
    		}
                    
                    /* Thrash the garbage from input strem */
                    cin.clear();
    		cin.ignore(1,'\n');
                
    
                if(choice2==1)
    		{
                            
                      	while(!inputDouble("Enter Fahrenheit temperature degree:", degree) || degree<=-316 || degree>=316)
    			{
    				cout << "Not valid selection....";
    			}
    			
    			cout << degree << " Fahrenheit = " << fahrenheitToCelsius(degree) << " Celsius." << endl;
    		}
    		else
    		{
    			while(!inputDouble("Enter Fahrenheit lower bound degree:",lowBound) || lowBound<=-316 || lowBound>=316)
    			{
    				cout << "Not valid selection....";
    			}
    
    			while(!inputDouble("Enter Fahrenheit maximum bound degree:",maxBound) || maxBound<=-316 || maxBound>=316)
    			{
    				cout << "Not valid selection....";
    			}
    
    			while(!inputDouble("Enter increment value for the list:",step) || step<=0)
    			{
    				cout << "Not valid selection....";
    			}
    			
    			cout << "Fahrenheit\tCelsius" << endl;
    			makeTable(lowBound, maxBound, step,2);
    		}
                
    	}
    	return 0;
    }
    	
        
        return (EXIT_SUCCESS);
    }

  2. #32
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Sure, just use inputInt for your menu choices [or something similar] - you can do this:

    Code:
     bool inputChoice(std::string str, int &choice, int min, int max) {
        while(inputInt(str, choice))
        {
            if (choice >= min && choice <= max) {
               return true;
    	cout << "Not valid selection.... Your Choice:";
        }
        return false;
     }
    
    
    	inputChoice("Select a number\n"
    	       "1. Celsius to Fahrenheit\n2. Fahrenheit to Celsius\n"
    	        "Your Choice:", 
                    choice, 1, 2);
    I just hacked the above up right now, so you may need to do a little bit of debugging and "beautification" of it, but as a principle it should work OK. No cin.ignore() or cin.clear() there...

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #33
    Registered User
    Join Date
    Aug 2007
    Posts
    66
    Thanks matsp, you rock dude!!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. entering numbers to array in a row whithout length input
    By transgalactic2 in forum C Programming
    Replies: 55
    Last Post: 01-02-2009, 04:02 AM
  2. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  3. Problem getting the input from a temp variable into the Array
    By hello_moto in forum C++ Programming
    Replies: 3
    Last Post: 03-16-2006, 01:50 AM
  4. String input problem, gets
    By willc0de4food in forum C Programming
    Replies: 13
    Last Post: 03-05-2005, 02:05 PM
  5. Problem with Printing message
    By robert_sun in forum C Programming
    Replies: 2
    Last Post: 05-16-2004, 02:09 PM