Hi, guys. Need a bit of help. I have a program with try/catch block. It works fine till I press 'q' to quit program, then it gets into an infinite loop. Can anybody advise what I am doing wrong?
Code:
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
const double CM_PER_INCH = 2.54;
const int INCHES_PER_FOOT = 12;
int main()
{
int feet, inches, totalInches;
double centimeter;
char option;
string nonDig = "You need to enter numeric digits please.";
while (option!='q')
{
try
{
cout << "Please enter the length in feet and inches or enter 'q' to quit the program: ";
cin >> feet >> inches;
cout << endl;
if (feet < 0 || inches < 0)
throw string ("Negative number");
else if (!cin)
throw nonDig;
totalInches = INCHES_PER_FOOT * feet + inches;
centimeter = CM_PER_INCH * totalInches;
cout << "The number in centimeters is: " << centimeter << "cm." << endl;
}
catch (int x)
{
cout << "Please enter other set of numbers." << endl;
}
catch (string s)
{
cout << s << endl;
}
catch (...)
{
cout << "Error in program. Try again" << endl;
}
}
return 0;
}