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;
}
#include <iostream>
#include <string>
#include <stdio.h>
#define PI 3.141
usingnamespace 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
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 **/)
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.