Do while loop problem

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.

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
55
56
57
58
59
60
61
62
63
64
65
66
67
68

#include <iostream>
using namespace 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;
}
Line 23 you assign 0 to selection, not ==

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";


I think much easier to see what is happening.
Last edited on
Solved
Topic archived. No new replies allowed.