开发者

C Programming Logic Error?

开发者 https://www.devze.com 2022-12-29 05:03 出处:网络
The following code compiles fine, but does not allow the user to choose whether or not the program is to run again. After giving the user the answer, the program automatically terminates. I placed the

The following code compiles fine, but does not allow the user to choose whether or not the program is to run again. After giving the user the answer, the program automatically terminates. I placed the main code in a "do while" loop to have the ability to convert more than one time if I wanted too. I have tried to run the program in the command line (Mac and Ubuntu machines) and within XCode with the exact same results. Any assistance would be greatly appreciated.

  • C Beginner

P.S. Compiling on MacBookPro running Snow Leopard.


#include <stdio.h>
#include <stdlib.h>

int main(void)
{

        char anotherIteration = 'Y';

        do
        {
           开发者_如何学编程     const float Centimeter = 2.54f;
                float inches = 0.0f;
                float result = 0.0f;

                // user prompt
                printf("\nEnter inches: ");
                scanf("%f", &inches);

                if (inches < 0) {
                        printf("\nTry again. Enter a positive number.\n");
                        break;
                } else {
                        // calculate result
                        result = inches * Centimeter;
                }

                printf("%0.2f inches is %0.2f centimeters.\n", inches, result);

                // flush input
                fflush(stdin);

                // user prompt
                printf("\nWould you like to run the program again? (Y/N): ");
                scanf("%c", &anotherIteration);

                if ((anotherIteration != 'Y') || (anotherIteration != 'N'))
                {
                        printf("\nEnter a Y or a N.");
                        break;
                }

        } while(toupper(anotherIteration == 'Y'));

        printf("Program terminated.\n");

        return 0;
}


You probably meant...

} while(toupper(anotherIteration) == 'Y');

since you want to convert the character, and then compare it with 'Y'.


Condition

if ((anotherIteration != 'Y') || (anotherIteration != 'N')) 

is always true, so your program will terminate regardless of user input.

Moreover, you put break into virtually every if in your code intended to handle incorrect input. break will terminate the cycle and the program. This is a rather strange logic: ask user to try again and then immediately terminate the program without giving the user opportunity to actually try again. Why are you terminating the program instead of allowing the user to reenter the input?


Well,

while(toupper(anotherIteration == 'Y'));

looks like you meant to say

while(toupper(anotherIteration) == 'Y');

.. but there may be other issues.


This works.

/* convert inches to centimeters */

#include <stdio.h>
#include <ctype.h>

int main(void)
{

 char anotherIteration = 'Y';

 do
 {
  const float Centimeter = 2.54f;
  float inches = 0.0f;
  float result = 0.0f;

  // user prompt
  printf("\nEnter inches: ");
  scanf("%f", &inches);

  if (inches < 0)
  {
   printf("\nTry again. Enter a positive number.\n");
   break;
  }
  else

   // calculate result
   result = inches * Centimeter;

  printf("%0.2f inches is %0.2f centimeters.\n", inches, result);

  // flush input
  fflush(stdin);

  // user prompt
  printf("\nWould you like to run the program again? (Y/N): ");
  scanf("%c", &anotherIteration);

 } while(toupper(anotherIteration) != 'N');

 printf("Program terminated.\n");

 return 0;
}


You have two big bugs here. The first one is what you have asked for in your question:

} while(toupper(anotherIteration == 'Y'));

anotherIteration == 'Y' will return either 1 or 0, which both equal 0 after being passed through toupper.

What you want instead is:

} while(toupper(anotherIteration) == 'Y');

The other bug lies here:

printf("\nWould you like to run the program again? (Y/N): ");
scanf("%c", &anotherIteration);

if ((anotherIteration != 'Y') || (anotherIteration != 'N'))
{
    printf("\nEnter a Y or a N.");
    break; // This breaks out of hte main program loop!
}

What you really want to do is ask the user again if they enter something wrong, like this:

do
{
    printf("\nWould you like to run the program again? (Y/N): ");
    scanf("%c", &anotherIteration);
    if ((anotherIteration != 'Y') && (anotherIteration != 'N'))
        printf("\nEnter a Y or a N.");
} while ((anotherIteration != 'Y') && (anotherIteration != 'N'));


You have a few bugs, but since you're learning, you should probably figure them out. The answer to your specific question on this program is that you probably want to be using fpurge() for stdin, not fflush().


One error:

while(toupper(anotherIteration == 'Y'))

should be

while(toupper(anotherIteration) == 'Y')
0

精彩评论

暂无评论...
验证码 换一张
取 消