anonymous
2008-10-23 08:49:44 UTC
Write a program that prompts the user to enter a length in feet and inches and outputs the equivalent length in centimeters. If the user enters a negative number or a nongigit number, throw and handle an appropriate exception and prompt the user to enter another set of numbers.
I am using visual C++ to write my code. I will be attaching the code that I have done below. Here are the problems I am having with this.
1. I get a warning that states: program1.cpp(91) : warning C4101: 'error' : unreferenced local variable.
This is what is on line 91: catch (int error)
2. I got the script to catch the error for negative numeric values, but I have not gotten it to catch alphabetic values.
Can someone help me correct these things? I am so burned out from working on this. I sure would appreciate it is someone can help me. I feel so overwhelmed and I am EXTREMELY new at C++. Now here is the script that I have gotten done so far.
#include
#include
using namespace std;
// VARIABLE DECLARATIONS:
double inches; // number of inches
double foot; //number of feet
double centimeters; // number of centimeters
int choice; // users menu choice
void flush(istream & is)
{
is.clear();
char nextChar;
while( (nextChar = is.get()) != '\n' && nextChar != EOF)
{ }
is.clear();
}
double read_double(char *message)
{
double dnum;
do {
cout << message;
cin >> dnum;
if (cin.fail()) {
cout << endl << "** Enter a number **" << endl;
flush(cin);
} else {
break;
}
} while (1);
return dnum;
}
int main()
{
//give the user some options and begin actual program
do
{ // while choice is not equal to zero
choice = -1;
// present menu of options
cout << endl;
cout << "Conversion of inches,feet to cetimeters" << endl;
cout << endl;
cout << "1 - Convert inches and feet to centimeters" << endl;
cout << "0 - Quit" << endl;
cout << endl;
cout << "Enter choice : ";
// get users response
cin >> choice;
if (cin.fail()) {
cout << endl << "** Enter a number **" << endl;
flush(cin);
}
if (choice == 1)
{
int inputok;
do {
inputok = 1;
foot = read_double("Enter length in feet: ");
inches = read_double("Enter inches as well: ");
cout<
//begin throw,try and catch commands--
try
{
if(inches < 0 || foot < 0)
throw -1;
}
catch (int error)
{
// handle exception error here
cout << "Negative foot/inches nor Alphabetic letters not accepted. Enter again."
<< endl << endl;
inputok = 0;
}
} while (inputok != 1);
// process inches to centimeters
//figure out how many feet and inches is centimeters
//To convert from Inches to Centimeter, multiply
// the Inches by 2.54
//To convert from Feet to Inches, multiply the
// Feet by 12
//To convert from Feet to Centimeters, multiply
// the Feet by 30.48
centimeters = (foot * 30.48f) + (inches * 2.54f);
//output the results
cout << foot << " foot and " << inches << " inches equals "
<< centimeters << " centimeters." << endl;
//***************************************************************
} //<- end if
}while (choice != 0);
return 0; // return to the operating system
} // end of main()