question about loops

which of the loop is most commonly used in place of goto loops ,while loop , do while ,or for?
Depends on the situation :)

While-loops are used when something needs to be repeated until a certain condition is true, for example in games until the player ends the game.

Do-loops are used when something need to be done at least once and then is repeated until it's 'right', for example when the user needs to input a year, the program should repeat the question while the user inputs things as "I'm not gonna tell you when I was born!" in steat of "1840".

For-loops are used when something needs to be repeat a number of times, and that number is known at the moment the loop starts. For example, when you want you printer to print a certain document 200 times.

http://www.cplusplus.com/doc/tutorial/control.html
Thanks
I have made a program using loops
but I dont know how to start the program again
without using goto loop



#include <iostream>
using namespace std;
int main()
{
double radius;
double side;
double area;
double diameter;


cout <<"please enter the radius =";
cin >>radius;

cout <<"please enter the side =";
cin>>side;

area=(side*side)-(2*3.1425*radius);

diameter=(2*radius);

if (diameter>side)
cout<<"radius is not in the range\n ";
else
cout<<"the area is = "<<area<<" inches"<<"\n";


while(true)
{

char q;

cout << "Enter Q to exit the program or Enter C to continue";
cin>>q;

if ( q=='q')
break;
else if (q=='c')
{

break;
}
while(q=='c')
{cout <<"please enter the radius =";
cin >>radius;

cout <<"please enter the side =";
cin>>side;

}



return 0;
}
Place the whole program in a while-loop or do-while loop, use a bool declared outside this loop as condition and give the user the option to set this bool to true or false inside the loop.
what should be the condition in while loop for the whole program ?
1
2
3
4
5
6
7
8
9
bool goOn=true;
while (goOn)
{
...
cout<<"Do you want to go on?";
cin>>answer;
if (answer == "no")
goOn=false;
}
Pi is 3.1415, not 3.1425, just to be pedantic...
this program is not working still


#include <iostream>
using namespace std;
int main()
{
double radius;
double side;
double area;
double diameter;

bool goOn=true;
while (goOn)
{

cout <<"please enter the radius =";
cin >>radius;

cout <<"please enter the side =";
cin>>side;

area=(side*side)-(2*3.1425*radius);

diameter=(2*radius);

if (diameter>side)
cout<<"radius is not in the range\n ";
else
cout<<"the area is = "<<area<<" inches"<<"\n";

char answer;

cout<<"Do you want to go on?";
cin>>answer;
if (answer == "no")
goOn=false;

}



return 0;
}
My code was only ment to show the construction: it wasn't correct code. You made answer of type char (contains only one character), while "no" is op type string. Making answer of type string would solve the problem, or chancing the condition to answer == 'n'. Make sure the user knows what sort of answer are expected from him, by using something as cout<<"Do you want to go on? <j/n>";
Topic archived. No new replies allowed.