以下代码编译得很好,但不允许用户选择是否要再次运行该程序。给用户答案后,程序自动终止。我将主代码放在“do while”循环中,以便能够在不止一次的情况下进行转换。我试图在命令行(Mac和Ubuntu机器)以及XCode中运行该程序的结果完全相同。任何援助将不胜感激。C编程逻辑错误?
- Ç初级
P.S.在运行Snow Leopard的MacBookPro上编译。
#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;
}
下次请格式化您的代码。 – 2010-05-03 22:59:59