I have used a do/while loop in an assignment before and believe I understand them (some on here helped me with it last week), but I cant seem to get my code to run how I think it should, the assignment specifies functions and calling functions within functions which is why its like this, the problem is I cant get it to repeat the ReadInputShape function with the do/while loop. The code compiles and runs fine. Any help would be much appreciated.
#include <iostream>
usingnamespace std;
void ReadShapeDimensions();
float calculateBasicVolume(double,double);
void printResult();
int ReadInputShapeChoice(int);
//declaring global variables
int selection;
float volume;
double radius;
double height;
void ReadInputShape() //main menu/shape selection
{
volume=0;
radius=0;
height=0;
cout<<"Volume Calculator\n\nPlease choose the shape you would like to calculate the volume for\n(1)Cylinder\n(2)Cone\n(0)Exit\n";
cin>>selection;
ReadShapeDimensions();
if (selection=0)
{
system("exit");
}
}
void printResult()//calling calculation and printing result
{
volume=calculateBasicVolume(radius,height);
cout<<"Volume = "<<volume<<"u^3\n";
}
float calculateBasicVolume(double radius, double height)//calculation function
{
switch (selection)
{
case 1:
{
volume=3.14*radius*radius*height;
}
case 2:
{
volume=0.33*3.14*radius*radius*height;
}
return volume;
}
}
void ReadShapeDimensions()//input data and calling calculation and result fumctions
{
cout<<"\nPlease enter the radius of the shape\n";
cin>>radius;
cout<<"\nPlease enter the height of the shape\n";
cin>>height;
printResult();
}
int main()//calling the function(s)
{
do
{
ReadInputShape();
system("pause");
}while (selection!=0);
return 0;
}
But you shouldn't need that condition anyway. The do/while will take care of that.
Also, try not to post lines of code longer than 80 characters on this site, they are harder to read. Basically, it breaks the word wrapping of non-code text.
As a coding tip, try to format the code in a "what you see is what you get" manner:
1 2 3 4 5
cout << "Volume Calculator\n\n"
<< "Please choose the shape you would like to calculate the volume for\n"
<< "(1)Cylinder\n"
<< "(2)Cone\n"
<< "(0)Exit\n";