Skip to main content
gamedev.net
🔒 Locked

do-while statement is infinite loop?

Started by pizzaman 2 replies 200+ views
Original Post
pizzaman
pizzaman
how do i get my program to repeat in c++. i use the example in my book. heres my source. #include int main() { char answer;//my answer thingy do //beginning o loop { double age; cout<<"Enter age "; cin>>age; if(age==1) cout<<"Liar"; cout<<"What is your real age"; double realage; cin>>realage; if(realage=1) cout<<"That does it this program is self destructing"; cout<>answer; }while(answer==''y''); cout<<"Thats nice"<
i am the best
Noctimanic
Noctimanic
use a loop like this

while(TRUE)
{
//code goes here

}

this loops forever, because true is always true.
Jumpster
Jumpster
if you mean to immediately go to the top of the loop it''s continue;

exampe:

while (1)
{
if !(checkit()) continue;

// can go further..
...
}

Regards,
Jumpster


Semper Fi
Regards,JumpsterSemper Fi
Jaxks
Jaxks
do {}while();
Is loop that loops at least one time because testing(while) is at the bottom of the loop.Your example uses do while statement because at least one name should be typed.
continue statement jumps at the start of the loop and break stops current loop. So you could make your program like this

#include
int main()
{
char answer;//my answer thingy
do //beginning o loop
{
double age;
cout<<"Enter age ";
cin>>age;
if(age==1)
cout<<"Liar";
cout<<"What is your real age";
double realage;
cin>>realage;
if(realage=1)
cout<<"That does it this program is self destructing";
cout cout<<"Do just one more y/n";
cin>>answer;
if(answer==''y'')
break;
}while(1);
cout<<"Thats nice" }
I hope this helped.Paste moved code to =&%¤&()=¤;

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.