PDA

View Full Version : Need help in C programming. About while loop.


nodeffect
09-12-2004, 09:37 AM
#include <stdio.h>

int main()
{
char choose = 'a';
float celsius, fahrenheit;

printf( "***Temperature Conversion Calculator***\n" );
printf( "Enter a character[c for convert to Celsius, f for convert to Fahrenheit]: " );
scanf( "%c", &choose );

while ( choose != 'c' && choose != 'C' && choose != 'f' && choose != 'F' ){
scanf( "%c", &choose ); /* <-- WHY CAN'T I REMOVE THIS ? */
printf( "\n!!Error Message: Wrong input, please try again.\n" );
printf( "\nEnter a character[c for convert to Celsius, f for convert to Fahrenheit]: " );
scanf( "%c", &choose );
}

switch ( choose ) {

case 'c':
case 'C':
printf( "Enter the Fahrenheit value[in integer]: " );
scanf( "%f", &fahrenheit );
celsius = 5.0/9.0 * ( fahrenheit - 32 );
break;

case 'f':
case 'F':
printf( "Enter the Celsius value[in integer]: " );
scanf( "%f", &celsius );
fahrenheit = ( celsius / (5.0/9.0) ) + 32;
break;
}

printf( "\n***Temperature Conversion Calculator***\n" );
printf( "The result is:\n" );
printf( "%10s%10s\n", "Fahrenheit", "Celsius" );
printf( "%10.3f%+10.3f\n", fahrenheit, celsius );

return 0;
}


see "/* <-- WHY CAN'T I REMOVE THIS ? */" that line...

so why..? if i remove that scanf line, the while loop will repeat itselfs 2 times. I really don't understand !....Please help guys :(

Jason
09-13-2004, 07:57 PM
try taking out that line and making that while statement into a do while, that might help...


Jason