When I execute the code below, it will execute which ever option you enter once. It will ask for another option as it is supposed to, but when you enter the second time, it ends the program. I have been trying to see in the book where I am going wrong, but not seeing it. Any help would be greatly appreciated. Thanks
#include <iostream>
usingnamespace std;
int main( )
{
int menu(), option;
double meters, feet, meters_length, meters_width, feet_length, feet_width, area;
do
{
option = menu();
if (option == 1)
{
cout << "How many meters?";
cin >> meters;
feet = meters * 3.28084;
cout << meters;
cout << " meters are equivalent to ";
cout << feet;
cout << " feet.\n";
option = menu();
}
elseif (option == 2)
{
cout << "How many feet?";
cin >> feet;
meters = feet * 0.3048;
cout << feet;
cout << " feet is equivalent to ";
cout << meters;
cout << " meters.\n";
option = menu();
}
elseif (option == 3)
{
cout << "What is the length of the rectangle in meters?";
cin >> meters_length;
cout << "What is the width of the rectangle in meters?";
cin >> meters_width;
feet_length = meters_length * 3.28084;
feet_width = meters_width * 3.28084;
area = feet_length * feet_width;
cout << "The area of the rectangle is ";
cout << area;
cout << " square feet.";
option = menu();
}
elseif (option == 4)
{
cout << "What is the length of the rectangle in feet?";
cin >> feet_length;
cout << "What is the width of the rectangle in feet?";
cin >> feet_width;
meters_length = feet_length * 0.3048;
meters_width = feet_width * 0.3048;
area = meters_length * meters_width;
cout << "The area of the rectangle is ";
cout << area;
cout << " square meters.";
option = menu();
}
} while (option =! 5);
return 0;
}
menu()
{
int option;
cout << "English-Metric Junoir\n";
cout << " 1) Convert from meters to feet\n";
cout << " 2) Convert feet to meters\n";
cout << " 3) Compute the area of a rectangle in square feet given the length and width in meters\n";
cout << " 4) Compute the area of a rectangle in square meters given the length and width in feet\n";
cout << " 5) Quit the program\n";
cout << "Please enter number (1-5) ->";
cin >> option;
while ((option < 1) || (option > 5))
{
cout << "Not a valid option. Please enter a number 1-5.";
cin >> option;
}
return option;
}
(option =! 5)
// is same as
( option = ! 5 )
// is same as
( option = (! 5) )
// Since you won't care about the assigned value of option,
you effectively test
( ! 5 )
You do have logical not and an assignment.
Try the (option != 5)
The != is a relational operator.