0

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;
}
```