0

I am trying to ask the user if he wants to go again on my c++ program

#include <iostream> #include <cmath> using namespace std; void selection() { cout << "Press 1 to find the area of a Triangle" << endl; cout << "Press 2 to find the area of a Square" << endl; cout << "Press 3 to find the area of a Rectangle" << endl; cout << "Press 4 to find the area of a Circle" << endl; } int main() { selection(); int select; const double pi = 3.14159265359; double base; double height; double side; double radius; double length; double width; double answer; char a; cout << endl; cin >> select; if (select == 1) { cout << endl; cout << "Enter the base" << endl; cin >> base; cout << endl; cout << "Enter the height" << endl; cin >> height; cout << endl; answer = base * height / 2; cout << "The area of the triangle is " << answer << endl; } else if (select == 2) { cout << endl; cout << "Enter the side length" << endl; cin >> side; cout << endl; answer = pow(side , 2); cout << "The area of the square is " << answer << endl; } else if (select == 3) { cout << endl; cout << "Enter the length" << endl; cin >> length; cout << endl; cout << "Enter the width" << endl; cin >> width; cout << endl; answer = length * width; cout << "The area of the rectangle is " << answer << endl; } else if (select == 4) { cout << endl; cout << "Enter the radius" << endl; cin >> radius; cout << endl; answer = pi * pow(radius , 2); cout << "The area of the circle is " << answer << endl; } while (a == 'y'||'Y') { cout << "Do you want to try again? [y/n] " << endl; cin >> a; selection(); } cout << "The End" << endl; return 0; } I tried to use a while loop to ask if the user wanted to play again but when the user enters in 'n

8th Jul 2017, 3:36 AM
Michael Addante
Michael Addante - avatar
5 odpowiedzi
+ 8
char a = 'y'; while (a == 'y') { // your entire program cout << "Do you want to restart the program? (y/n)"; cin >> y; // some input validation 'round here }
8th Jul 2017, 5:22 AM
Hatsy Rei
Hatsy Rei - avatar
+ 3
do { selection(); // process user input // ask user if she wants to have another go } while (a == 'y' || a == 'Y')
8th Jul 2017, 5:26 AM
Ipang
0
So you want to start your program all over again, you can make a label like start: and put a goto start;.
8th Jul 2017, 5:11 AM
Vazgen Sargisian
Vazgen Sargisian - avatar
- 1
Vazgen is right. However, using label and goto isn't consider as good programming. The code can quickly become impossible to read and understand. You can use a 'do ... while' loop. Here are the syntax : Label: int main(){ label beginning : int x=0; ++x; goto beginning ; // This code will run again and again. } Do ... while : int main(){ string answer; do{ int x=0; ++x; cout<<"Do you want to run the code adain ? "; cin>>answer; } while(answer!="no") }
8th Jul 2017, 5:32 AM
Jojo
- 1
Another, thing, I saw that you've finished the C++ course. You should use objects in your code and 'switch ... case' statement. If you don't know about object, try to read articles, other courses about it.
8th Jul 2017, 5:35 AM
Jojo