简体   繁体   中英

My while loop in C replays one more time than it should before it ends

When given input, my while loops print one time extra before closing its loop. For example, 8 7 38 3 -5 -1 q is my input that prints out

Provide 6 integer coefficients for the brute force equation solver: Solution found: x = 3, y = 2

Run program again? Type 'q' to quit or any other key to continue:

Provide 6 integer coefficients for the brute force equation solver: Solution found: x = 3, y = 2

Run program again? Type 'q' to quit or any other key to continue:

When it should end after the first iteration. Can anyone help me out on this? My code is pasted below

#include <stdio.h>
#include <math.h>
#include <stdbool.h>

int main(void) 
{
   //Equations should be a1x + b1y = c2 and a2x + b2y = c2
   int a1, b1, c1, a2, b2, c2, x, y;
   char input;
   bool solFound = false;
   bool runAgain = true;
   
   //brute force x and y [-10, 10]
   while (runAgain == true) 
   {
      printf("Provide 6 integer coefficients for the brute force equation solver: ");
      scanf("%d %d %d %d %d %d", &a1, &b1, &c1, &a2, &b2, &c2);
      for (x = -10; x<=10; x++)
      {
         for (y = -10; y<=10; y++)
         {
            if (a1*x + b1*y == c1 && a2*x + b2*y == c2)
            {
                  printf("\nSolution found: x = %d, y = %d\n\n", x, y);
                  solFound = true;
                  runAgain = false;
            }
         }
      }
      if (solFound != true)
      {
         printf("No solution found\n\n");
         runAgain = false;
      }
      scanf("%c", &input);
      printf("Run program again? Type 'q' to quit or any other key to continue:");
      if (input != 'q')
      {
         runAgain = true;
      }
      printf("\n\n");
   }
} ```

For starters the variable sqlFound should be declared within the while loop or at least reinitialized within the while loop. For example

   while (runAgain == true) 
   {
       bool solFound = false;
       //...

Otherwise it will keep the value of a previous iteration of the loop.

In this if statement

  if (solFound != true)
  {
     printf("No solution found\n\n");
     runAgain = false;
  }

setting the variable runAgain

 runAgain = false;

does not make a sense because after this if statement you are asking again whether to repeat the loop

  printf("Run program again? Type 'q' to quit or any other key to continue:");
  if (input != 'q')
  {
     runAgain = true;
  }

So remove this setting

 runAgain = false;

from the if statement.

Another problem is that this call of scanf

  scanf("%c", &input);

must follow the question

  printf("Run program again? Type 'q' to quit or any other key to continue:");

And moreover the format string must contain a leading blank

  scanf(" %c", &input);
        ^^^^^

Otherwise white space characters will be read from the input stream. That is you need to write

  printf("Run program again? Type 'q' to quit or any other key to continue:");

  scanf(" %c", &input);

  runAgain = input != 'q' && input != 'Q';

You're going to a lot of trouble to make sure that runAgain is false before you ask the user to run again. This isn't doing what you think:

if (solFound != true)
{
   printf("No solution found\n\n");
   runAgain = false;
}

Once solFound becomes true , it stays that way, and if when the program loops there is no solution, you fail to print "No solution found" as well as failing to change runAgain to false.

This causes a problem if runAgain is already true :

  if (input != 'q')
  {
     runAgain = true;
  }

This lets you change false (if there is no solution) back to true (by entering something other than 'q' ). But entering 'q' can't change true to false !

The problem only stops if runAgain went false and you enter 'q' .

If you wanted to stop only based on entry of q , you can eliminate the connection in the first block between solFound and runAgain , and replace the second block by:

runAgain = (input != 'q');

This way runAgain is always changed to match the user choice, never left alone.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM