John Walter
John Walter

Reputation: 141

Stuck with exiting from a loop in C

The problem is that I don't know how to exit from my loop.

printf ("Do you order fish? (Y/N): ");
scanf  ("%c", &f);

while((f == 'y')||(f == 'Y'))
{
  do {
    fish = getfish_choice();
    printf ("Total of you fish is %.2lf\n", sum);
    printf ("Do you want to order more fish?(Y/N)");
    scanf  (" %c", &morefish);
  }
  while ((morefish=='Y')||(morefish=='y'));
}

Printf ("Hello");

How could I exit this loop so the output of my code will read Hello?

Upvotes: 0

Views: 229

Answers (3)

Mysticial
Mysticial

Reputation: 471209

What's happenning is that when Y or y is entered, you manage to break out of the inner loop, but you're still stuck in the outer loop. (f is never modified)

You don't even need two loops:

printf ("Do you order fish? (Y/N): ");
scanf  ("%c", &morefish);

while ((morefish=='Y')||(morefish=='y')){
    fish = getfish_choice();
    printf ("Total of you fish is %.2lf\n", sum);
    printf ("Do you want to order more fish?(Y/N)");
    scanf  (" %c", &morefish);
}

printf ("Hello");

Upvotes: 1

dave
dave

Reputation: 12806

Change the outer while loop to an if: if(f == 'y'||f == 'Y')

Upvotes: 1

Oliver Charlesworth
Oliver Charlesworth

Reputation: 272467

You have a while loop whose condition involves checking f. But you never modify f.

So perhaps that first while should actually be an if.

Upvotes: 2

Related Questions