I'm a newbie to c++, and as I'm reading my textbook, I'm applying different rules to this program for converting gallons to liters and vice versa, My question is how can I check that the user input is numeric and not a string of characters? I've tried several things with isalpha and isdigit, but they didn't seem to work and the program just continued to return an infinite loop. I really appreciate any input on this.

Thanks!

#include <iostream>
using namespace std;


int main()
{
	const double GAL_CONVERSION = 3.78533;
	const double LIT_CONVERSION = .264;
	double gallons, litres;
	char ans;
	const char error_neg[] = "Unable to calculate negative volume, please enter a positive amount\n";


	cout << "\nWelcome to the Volume Converter!\n" << "Enter 0 to quit" <<endl;

	do
	{	
		cout <<"Please insert the number of gallons you would like to convert: ";
		cin >> gallons;
	
		litres = gallons * GAL_CONVERSION;
	
		if (gallons > 0)
		{
			cout << "You have " << litres << " litres " << "if you have " << gallons;
	
			if (gallons == 1)
			cout << " gallon\n";

			else 
			cout << " gallons\n";
		}
		
		if (gallons < 0)
			
			cerr << error_neg;
		else;
	
	} while (gallons != 0);
	
		
		cout << "Do you want to convert litres to gallons? (y/n): ";
		cin >> ans;
	
	if (ans == 'y')
	
	{	do
		{	
			cout << "please enter the number of liters you would like to convert: ";
			cin >> litres;

			gallons = litres * LIT_CONVERSION;

			if (litres > 0)
			{
				cout << "You have " << gallons << " gallons " << "if you have " << litres;

				if (litres == 1)
				cout << " litre\n";

				else
				cout << " litres\n";
			}

			if (litres < 0)

				cerr << error_neg;

			
	
		}while (litres != 0);
	}

	else;
	
	cout << "Good-bye\a" << endl;


	return 0;
}

Recommended Answers

All 4 Replies

Get the input as a string then check each of the characters. If all is ok then convert that string to int (or other data type). Something along these lines.

#include <iostream>
#include <string>
#include <sstream>
using namespace std;

int main()
{
    int gallons;
    std::string input;
    cout << "Enter gallons\";
    cin >> input;
    bool ok  = true;
    for(int i = 0; i < input.length(); i++)
    {
       if( !isdigit(input[i]) )
       {
            cout << "Error\n";
            ok = false;
       }
   }
   if( ok == true )
   {
         stringstream str(input);
         str >> gallons;
   }

if you don't want to get the user's input as a string you can
do the following (not tested) :

int num = 0;
 cout<<"Number please : ";
 cin >> num;

 while(!cin)//if input fails
{
   cin.clear();
   while(cin.get() != '\n')
     continue;
  cout<<"\nInvalid Input\n";
  cout<<"Try again : ";
  cin >> num;
}

Thank you very much for the replies. I'll give these a try today and report back with the results :)

At this point, passing the input as a string seems a bit above my level of experience, so I tried the other solution, and it absolutely worked (except in the case of both valid and invalid characters in the input *will work on that*)

Thank you!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.