Do While Loop not looping

So this is my program, but when I get to the part where the user enters y to try again, it doesnt loop back.


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
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <iomanip>


using namespace std;

const double pi = 3.141592654;

double circumference(double r);
double area_circle(double r);
double area_sphere(double r);
double volume_sphere(double r);
void print_menu(void);


int main(void)
{
	int choice, answer;
	double r;
	

	do{

	cout << "Enter a radius. \n";
	cin >> r;
	
	print_menu();   
	

	cin >> choice;
	cout << "\n";


	switch(choice){
		case 1:		
			cout<<"The circumference of the circle with radius "<< r <<" is "<< circumference(r);
			break;

		case 2:
			cout << "The area of a circle is " << area_circle(r);
			break;

		case 3:                        
			cout << "The area of a sphere is " << area_sphere(r);
			break; 

		case 4:						
  			cout << "The volume of a sphere is " << volume_sphere(r);
			break;

     
		default : cout<<"Invalid option.\n";
	}
	
	
	cout << "Enter y to try again: " ;
	cin >> answer;
	cin.ignore(80,'\n');
    

	}while(answer == 'y' || answer == 'Y');


	system("Pause");

}


void print_menu(void)
{
	//prompt the user for the desired equation
	cout << "Which would you like to calculate?";
	//menu options
	cout << "1 The circumference of a circle";
	cout << "2 The area of a circle";
	cout << "3 The area of a sphere";
	cout << "4 The volume of a sphere";
}
[the corresponding functions are here, I know it's not a problem there] 
Answer is an int. If you want to hold a character in a variable, you need a char array or string for that. If you want to use single quotes in comparison with a string or char array, you need to do this:

1
2
3
4
if(mystring[integer] == 'character') // Or mychar
{
     // Code
}


Replace "integer" with an integer and "character" with a character. Or you could just use a string and double quotes, and you wouldn't need square brackets.

EDIT: Remember that arrays start counting at 0. In case you forgot. So to find the first letter of a string or char array you would do that and replace "integer" with 0.
Last edited on
Oh yes, thank you.
Topic archived. No new replies allowed.