954,498 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

"for infinite loops:

I'm a noob when it comes to C programming. I wrote a simple program that runs an infinite for loop. But inside that loop I wanted to create another loop to stop the user three times after entering incorrect passwords.
I tested from different angles it seems like the infinite loop ignore my other loop.

What should I do to stop the user from entering endless bad password?

for(;;)
{
if(password==USER_ID)
{
run program;
break;
// if true the program runs and stop loop, no problem
}

else if(password!=USER_ID)
{
for(count=0;count<3;count++)
{
display an error message;
ask user to type password again;

/* I put a break keyword, but it doesn't stop the endless loop if a person type in a bad password. */

}

}


}

sftwr21
Newbie Poster
7 posts since Jun 2008
Reputation Points: 10
Solved Threads: 0
 

If you don't want an endless loop why did you write one into your program?

gerard4143
Nearly a Posting Maven
2,272 posts since Jan 2008
Reputation Points: 512
Solved Threads: 387
 
If you don't want an endless loop why did you write one into your program?

Because I know that I can stop an endless loop. The first if function, breaks the loop if it's true. And if I can break the second loop after running three loops it will work. The problem is how?

sftwr21
Newbie Poster
7 posts since Jun 2008
Reputation Points: 10
Solved Threads: 0
 
if I can break the second loop after running three loops it will work.

After the triple for loop put in the line break like for (i = 0; i < 3; ++i)
{
//do something
}
break;

gerard4143
Nearly a Posting Maven
2,272 posts since Jan 2008
Reputation Points: 512
Solved Threads: 387
 
int cnt = 0;

for infinite loop
{
   //do stuff
   if(//do stuff is what i need) // then break;
  else cnt++;

  //if cnt is above the limit then break;
}
firstPerson
Senior Poster
3,923 posts since Dec 2008
Reputation Points: 841
Solved Threads: 608
 

Interesting...

sftwr21
Newbie Poster
7 posts since Jun 2008
Reputation Points: 10
Solved Threads: 0
 
break;

always puts the control out of immediate loop in which it is declared. In your case it will come out of the second for loop not the first one.

codeguru_2009
Light Poster
31 posts since Aug 2009
Reputation Points: 46
Solved Threads: 6
 
always puts the control out of immediate loop in which it is declared. In your case it will come out of the second for loop not the first one.


I tried but didn't work as planned, thanks anyway.

sftwr21
Newbie Poster
7 posts since Jun 2008
Reputation Points: 10
Solved Threads: 0
 

Thank you very much firstPerson! I understand what I did to do and it is executing the way I wanted.

sftwr21
Newbie Poster
7 posts since Jun 2008
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: