tags:

views:

140

answers:

7

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;
}
+2  A: 

You probably meant...

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

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

Amber
Thank you Mr. Dav
A: 

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().

Carl Norum
Thanks Mr. Carl. I guess I better pay more attention to the Standard Library. I'll go and read about the differences between fpurge and fflush.
@mbpluvr64, `man fflush` should tell you everything you need to know.
Carl Norum
@mbpluvr64: Since you're printing a prompt before taking input, you *do* want to call `fflush(stdout)` after the `printf` calls. Furthermore, you won't need to "flush" `stdin` at all if you replaced `scanf` with a combination of `fgets` and `sscanf` (which is generally a good idea anyway).
jamesdlin
+1  A: 

Well,

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

looks like you meant to say

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

.. but there may be other issues.

JustJeff
While that code is odd, I don't see how that would create the result he sees. anotherIteration=='Y' should return true (1), and toupper(1) should be 1. Resulting in a true-loop. Instead, he sees the loop end.
abelenky
Thanks Mr. Jeff.
@abelenky: yeah, that's what i thought, it should never terminate the way it looked. hence the punt about 'other issues' =)
JustJeff
+1  A: 

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;
}
Ishpeck
+1  A: 

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'));
Billy ONeal
toupper(1) is documented to return 1: "If no such conversion is possible, the value returned is c unchanged." Why do you think toupper will return 0?
abelenky
@abelenky: Because if it was not returning 0, then the program would not be terminating.
Billy ONeal
Thanks Mr. Billy. I appreciate your time.
A: 

One error:

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

should be

while(toupper(anotherIteration) == 'Y')
Betamoo
Thanks Mr. Betamoo.
+2  A: 

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?

AndreyT
Thank you Mr. Andrey