Do while loop not working?

0 aj burnett · June 26, 2015
. I have finished a class project but my do while loop is not working? I want the user to be able to pick from the menu repeatedly options 1-4 and then press 5 to end the program. When I run it and choose an option from the menu and get the calculations the program ends instead of looping back to the main menu to choose again?



 
 
 
 
 
  #include <iostream>
 
 using namespace std;
 
 double meters_infeet(double meters){
   double totalmetersinfeet = (meters * 3.280839895);
    return totalmetersinfeet;
    }
 double feet_inmeters(double feet){
 double totalfeetinmeters = (feet / 3.280839895);
 return totalfeetinmeters;
 }    
 double area_infeet(double length_inmeters, double width_inmeters){
 double totalareainfeet = (length_inmeters * 3.280839895) * (width_inmeters * 3.280839895);
 return totalareainfeet;
 }
 double area_inmeters(double length_infeet, double width_infeet){
 double totalareainmeters = (length_infeet / 3.280839895) * (width_infeet / 3.280839895);
 return totalareainmeters;
 }
 
    

 int main()
 {
 int choice;
 double meters,feet,length_inmeters,width_inmeters,length_infeet,width_infeet;
 
 do{

 cout << "English-Metric Junior" << endl;
 cout << "1)Convert from meters to feet" << endl;
 cout << "2) Convert from feet to meters" << endl;
 cout << "3) Compute the area of a rectangle in square feet given length and width in meters" << endl;
 cout << "4) Compute the area of a rectangle in square meters given the length and width in feet" << endl;
 cout << "5) Quit the Program" << endl;
 cout << "Please enter a number (1-5)" << endl;
 cin >> choice;
 }while(choice > 5);
 
 
 
 switch (choice)
{
case 1:
cout << "Enter Meters" << endl;
cin >> meters;
cout << meters_infeet(meters)<< "Feet " << endl;
break;
case 2:
cout << "Enter Feet" << endl;
cin >> feet;
cout << feet_inmeters(feet)<< "Meters " << endl;
break;
case 3:
cout << "Enter length in meters" << endl;
cin >> length_inmeters;
cout << "Enter width in meters" << endl;
cin >> width_inmeters;
cout << area_infeet( length_inmeters, width_inmeters)<< "Area in Feet " << endl;
break;
case 4:
cout << "Enter length in feet" << endl;
cin >> length_infeet;
cout << "Enter width in feet" << endl;
cin >> width_infeet;
cout << area_inmeters(length_infeet, width_infeet)<< "Area in Meters" << endl;
break;
}
}
 
 

Post a Reply

Replies

Oldest  Newest  Rating
0 Ananthakrishnan VK · June 27, 2015
dude 
change your main function as follows
use choice=getch();
also add header file #include<cstring> geany ide

do{
........;
..........;
switch(........)
{.................;
....................;
.........................
}
}while(........);

int main()
{
int choice;
double meters,feet,length_inmeters,width_inmeters,length_infeet,width_infeet;

do{

cout
0 James Juan · June 27, 2015
, .move your switch statement inside your do{...} statement

change your condition to (choice<5)

do{

switch(choice){

}
}while(choice<5);
0 Kenneth Malicay · June 26, 2015
Just include the switch in the do bracket.

e.g.

do {
 switch() {
  case
  case
  case
  case
 }
} while();

and change the while
while(choice!=5);

also add
case 5:
return 0;
default:
cout << "Invalid input.";
0 K D · June 26, 2015
Take the "} while(...);" and place it at the bottom of the main.  What if they enter 4 or 2 it won't continue because your condition will only be true if choice is Greater then 5...  Why not make it  "} while(choice > 0 && choice < 5);" or "} while(choice != 5); " ...  For the first one you may also need a second loop around the menu to make sure the input is a number from 1 to 5 this way they can't enter something stupid like 99...  I would also add a couple of line breaks after the switch just so there is space between your menu and the previous output this way its easier to read...  also if you go with with "choice != 5" why not have a 5th case that just contains a break and a default that happens if the user enters an invalid option....


I Really Hope This Help...  :)
  • 1

C++

107,205 followers
About

Used in many types of software including music players, video games, and many large scale applications.

Links
Moderators
Bucky Roberts Administrator