# Thread: need a little help-where is the error?

1. ## need a little help-where is the error?

Code:
```#include <iostream>
using namespace std;

int main()
{
double kilometers, double miles, double centigrade, double fahrenheit;
double square_miles, double acres, double inches, double centimeters;

cout << "Press enter after entering a value!\n";
cout << endl;
cout << endl;
cout << "1) Kilometers to Miles\n";
cout << "2) Centigrade to Fahrenheit\n";
cout << "3) Square Miles to Acres\n";
cout << "4) Inches to Centimeters\n";
cout << endl;
cout << "Please enter a number from the menu: ";

if (menu_number == '1');
{
cout << "Please enter the number of kilometers: ";
cin >> kilometers;
cout << endl;
miles = kilometers * 0.62;
cout << kilometers << " kilometers equals " << miles << " miles.\n";
}
else if (menu_number == '2');
{
cout << "Please enter the degrees centigrade: ";
cout << endl;
fahrenheit = ((9/5)*centigrade) + 32;
cout << centigrade << " degrees centigrade equals " << fahrenheit << " degrees fahrenheit.\n";
}
else if (menu_number == '3');
{
cout << "Please enter the square mileage: ";
cin >> square_miles;
cout << endl;
acres = square_miles * 640;
cout << square_miles << " square miles equals " << acres << " acres.\n";
}
else if (menu_number == '4');
{
cout << "Please enter the number of inches: ";
cin >> inches;
cout << endl;
centimeters = inches * 0.39;
cout << inches << " inches equals " << centimeters << " centimeters.\n";
}
else
{
cout << "You have entered an invalid number. Please try again.\n";
}

return 0;
}```
Code tags added by Hammer. Use them next time please.

2. first of all, declare your variables like this:

Code:
```double kilometers, miles, centigrade, fahrenheit;
double square_miles, acres, inches, centimeters;
You don't need to keep saying "double".

Next, you don't need semicolon's after if and elseif, ie

if (menu_number == 1)

ALSO, since the menu selection is an integer, you don't need the single quotes around the numbers in the if statements.

3. ## another problem

how do i loop the menu, and also--how do I keep the program running after i have entered an invalid number?

4. ## Re: Another Problem

Originally posted by papisuave
how do i loop the menu, and also--how do I keep the program running after i have entered an invalid number?
You Can Make another function for the Menu... and Input.. or you
you can use a goto statement in your last else part of the condition.
Check Out the Useage of goto Statement.

Code:
```#include <iostream>
using namespace std;

int main()
{
double kilometers,miles, centigrade, fahrenheit;
square_miles, acres, inches, centimeters;

cout << "Press enter after entering a value!\n";
cout << endl;
cout << endl;
cout << "1) Kilometers to Miles\n";
cout << "2) Centigrade to Fahrenheit\n";
cout << "3) Square Miles to Acres\n";
cout << "4) Inches to Centimeters\n";
cout << endl;
menu:  // See This Line ...for goto...
cout << "Please enter a number from the menu: ";

if (menu_number == 1)
{
cout << "Please enter the number of kilometers: ";
cin >> kilometers;
cout << endl;
miles = kilometers * 0.62;
cout << kilometers << " kilometers equals " << miles << " miles.\n";
}
else if (menu_number == 2)
{
cout << "Please enter the degrees centigrade: ";
cout << endl;
fahrenheit = ((9/5)*centigrade) + 32;
cout << centigrade << " degrees centigrade equals " << fahrenheit << " degrees fahrenheit.\n";
}
else if (menu_number == 3)
{
cout << "Please enter the square mileage: ";
cin >> square_miles;
cout << endl;
acres = square_miles * 640;
cout << square_miles << " square miles equals " << acres << " acres.\n";
}
else if (menu_number == 4);
{
cout << "Please enter the number of inches: ";
cin >> inches;
cout << endl;
centimeters = inches * 0.39;
cout << inches << " inches equals " << centimeters << " centimeters.\n";
}
else
{
cout << "You have entered an invalid number. Please try again.\n";
}

return 0;
}```

i don't undestand what you mean by the menu part, it keeps coming up with an error

6. Keep in mind that many people consider goto statements to be bad pogramming practice.

An alternative would be to place everything in a do-while loop.

PHP Code:
``` bool loopAgain; do { loopAgain = false; //menu //excute based on selection //if invalid set   else   {   cout << "You have entered an invalid number. Please try again.\n";   loopAgain = true;   } } while(loopAgain);  ```

everytime the loop started loopAgain is set to false, this means if they make a valid selection then the loop will break at the end, but if they make an invalid selection loopAgain is set to true so the while statement checks it and because it is true loops again.

We have to set loopAgain to false at the beginning each time so that if on that looping they make a valid selection then the loop will break.

Another approach would be to set loopAgain=false at the begginning, and only change it to false if they choose the menu option for Quit. This allows the user to repeatedly use the program again and again until they decide the want to exit.

Popular pages Recent additions