help making a loop to reiterate menu choices?

so i wrote a program based on the perimeters of shapes and am having a hard time finding a way to write a loop. can someone help?




#include <iostream>
#include <string>
#include <stdio.h>
#define PI 3.141
using namespace std;

int main()
{
string menuChoices[] = {"Circle","Rectangle","Rhombus","Triangle"}; //Choices for the menu
cout<<"What shape would you like to find the perimeter for?"<<endl;
for (int i=0;i<4;i++)
{
cout<<i+1<<"\t\t"<<menuChoices[i]<<endl; //Prints every choice in the menu like:
//1 Circle
//2 Triangle
//etc...
}

int myChoice;
cout<<"(Please enter a number): ";
cin>>myChoice; //get the choice

switch (myChoice)
{
case 1:
float r,l,w,h,a,b,c;
float perimeter;
printf("\n\nEnter the radius of the circle : ");
scanf("%f",&r);
perimeter = 2 * PI * r;
printf("Perimeter of circle is: %.3f",perimeter);
break;
case 2:
printf("\n\nEnter width and length of the rectangle : ");
scanf("%f%f",&w,&l);
perimeter = 2 * (w +l);
printf("Perimeter of rectangle is: %.3f",perimeter);
break;
case 3:
printf("\n\nEnter any side of the rhombus : ");
scanf("%f",&a);
perimeter = 4 * a;
printf("Perimeter of rhombus is: %.3f",perimeter);
break;
case 4:
printf("\n\nEnter the size of all sides of the triangle : ");
scanf("%f%f%f",&a,&b,&c);
perimeter = a + b + c;
printf("Perimeter of triangle is: %.3f",perimeter);
break;
}
system("PAUSE");
return 0;
}
First of all, please use the code tags. They make it a hundred times easier to read your code. It makes it look pretty...
Like this. :D

Second, could you be more specific on what the problem is?
having a hard time finding a way to write a loop

This doesn't really tell us much about whats wrong. Whats the program doing that wrong? What are you wanting it to do? etc.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
#include <string>
#include <stdio.h>
#define PI 3.141
using namespace std;

int main()
{
  string menuChoices[] = {"Circle","Rectangle","Rhombus","Triangle"}; //Choices for the menu
  cout<<"What shape would you like to find the perimeter for?"<<endl;
  for (int i=0;i<4;i++)
  {
    cout<<i+1<<"\t\t"<<menuChoices[i]<<endl; //Prints every choice in the menu like:
    //1		Circle
    //2		Triangle
    //etc...
  }

  int myChoice;
  cout<<"(Please enter a number): ";
  cin>>myChoice; //get the choice

  switch (myChoice)
  {
    case 1:
         float r,l,w,h,a,b,c;
         float perimeter;
      printf("\n\nEnter the radius of the circle : ");
      scanf("%f",&r); 
      perimeter = 2 * PI * r;
      printf("Perimeter of circle is: %.3f",perimeter);
      break;
    case 2:
      printf("\n\nEnter width and length of the rectangle : ");
      scanf("%f%f",&w,&l);
      perimeter = 2 * (w +l);
      printf("Perimeter of rectangle is: %.3f",perimeter);
      break;
    case 3:
      printf("\n\nEnter any side of the rhombus : ");
      scanf("%f",&a);
      perimeter = 4 * a;
      printf("Perimeter of rhombus is: %.3f",perimeter);
      break;
    case 4:
      printf("\n\nEnter the size of all sides of the triangle : ");
      scanf("%f%f%f",&a,&b,&c);
      perimeter = a + b + c;
      printf("Perimeter of triangle is: %.3f",perimeter);
      break;
  }
  system("PAUSE");
  return 0;
}


nothing is wrong with the program. i'd just like to find a way to make my code loop
Last edited on
Much prettier. :)

Well you have a couple of loops at your disposal: while, do-while, for, goto statements (eh,,, kinda).

The idea is to pick out which one will do the job you want the most efficiently.
A for loop would be nice, it involves incrementing an index variable and you have no use for that.

Goto statements are generally avoided, for reasons you can Google search, but they aren't entirely useless.

A while loop would work perfectly, but a do-while loop would be preferrable.

A do-while loop does the exact same thing as a while loop, but the condition is checked at the end of the loop rather than the beginning. This means that everything in it will always run AT LEAST once (hence... what you want).

A do-while loop can easily be implemented as such:
1
2
3
4
5
6
do
{
    cout << "What shape would you...."
    ...
    ...
} while (/** condition **/)
Last edited on
thank you so much!!
Tresky wrote:
A while loop would work perfectly, but a do-while loop would be preferrable.


I much prefer while loops over do loops. I only use a do loop if I really have to- which is rare.

In this situation a do loop often means a repeated test.

I would prefer this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
bool Quit = false;

while (!Quit) {
//show menu
//get input

//switch statement
switch (option) {
....
.....

case 'Q':
//user wants to quit
Quit = true;
break;

default:
std::cout << "Bad Menu Selection" << std::endl;
break;

}//end of switch

}//end of while

//execution continues here

...

return 0;
} // end of main 


@OP

You have printf & scanf in your code - can you learn to use cout & cin?

Always put a default case in your switch to catch bad input - like I have shown.

Last edited on
where exactly would i put that loop? can you guys input it for me, please?
Your code has a switch - so does mine - put it there.

Writing code is your job - we give advice & ideas.

Have a go - see how you get on - any problems, then post your new code and any compilers errors in full.
Topic archived. No new replies allowed.