Question:
C++ Write a program conversion from ft and in's to cm?
anonymous
2008-10-23 08:49:44 UTC
The requirements for this program are as follows:
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()
Three answers:
conehead
2008-10-23 09:26:58 UTC
Your program seems fine to me.



You get the unreferenced local variable because you don't actually use the variable error. It's ok. If you want to get rid of the error, you could include error in your output statement.



try

{

if(inches < 0)

throw inches;



if(foot < 0)

throw foot;

}

catch (int error)

{

// handle exception error here

cout << "Negative foot/inches not accepted (" << error << ". Enter again."

<< endl << endl;

inputok = 0;

}



You're not going to catch alphabetic in your regular code. You're already testing for it and dealing with it in read_double. Never gets back to main. You could use a try throw catch there, but it seems like you're already fine.



Do you have a requirement to take care of this in your main program?
anonymous
2008-10-23 09:10:48 UTC
It work fine. I copy and paste it down and compiled it with a gcc compiler. It looks pretty cool. Visual C++ sometimes confuses itself, cache problem or something.



Sometime I'd create a new project and copy and paste the code from the old project. And it work.



Or you could try retyping them and rebuilding them.



There seem to be nothing wrong with the code itself.



give it a shot, =D
laurene
2016-05-28 18:54:54 UTC
I had written the length conversion But I don't write loop function.Because your loop describe no clear that how to loop. // The function got any length conversion function example double lengthconversion (double currentlength, double rate) { double result; result=currentlength*rate; return (result); } int main () { double feetToMeter; double feetToCentimeter; double inchToMeter; double inchToCentimeter; feetToMeter = lengthconversion (10,0.3048); feetToCentimeter = lengthconversion (10,30.48); inchToMeter = lengthconversion (10,0.02548); inchToCentimeter = lengthconversion (10,2.54); cout << "The feetToMeter is " << feetToMeter; cout << "The feetToCentimeter is " << feetToCentimeter; cout << "The inchToMeter is " << inchToMeter; cout << "The inchToCentimeter is " << inchToCentimeter; /////////// if you need a loop to repeat //include a loop to repeat the program until the user wishes to stop //You can be write loop code here. int x=1; while(x<10) { cout << "The x is " << x; x++; } }


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...