2010-05-03 154 views
0

以下代码编译得很好,但不允许用户选择是否要再次运行该程序。给用户答案后,程序自动终止。我将主代码放在“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; 
} 
+0

下次请格式化您的代码。 – 2010-05-03 22:59:59

回答

0

你有一些错误,但由于你正在学习,你应该找出它们。 这个程序的具体问题的答案是,您可能希望使用fpurge()代替stdin而不是fflush()

+0

谢谢卡尔先生。我想我最好更加关注标准库。我会去看看fpurge和fflush之间的区别。 – user331861 2010-05-03 23:29:46

+0

@ mbpluvr64,'man fflush'应该告诉你你需要知道的一切。 – 2010-05-04 00:19:38

+0

@ mbpluvr64:由于您在输入前正在打印提示,您希望在printf调用之后调用'fflush(stdout)'。此外,如果用'fgets'和'sscanf'组合替换'scanf'(无论如何,这通常都是个好主意),则不需要“刷新”stdin。 – jamesdlin 2010-05-04 03:27:22

2

你可能是指...

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

,因为你要的字符转换,并然后'Y'进行比较。

+0

谢谢戴尔先生 – user331861 2010-05-03 23:26:54

1

好,

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

看起来像你的意思是说

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

..但有可能是其他的问题。

+0

虽然这段代码很奇怪,但我不明白这将如何创造他看到的结果。 anotherIteration =='Y'应该返回true(1),toupper(1)应该是1.产生一个真正的循环。相反,他看到循环结束。 – abelenky 2010-05-03 23:06:07

+0

@abelenky:是的,这就是我的想法,它永远不应该终止它的样子。因此关于“其他问题”的平局=) – JustJeff 2010-05-03 23:27:55

1

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; 
} 
1

这里有两个大错误。第一个是你已经在你的 问题问什么:

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

anotherIteration ==“Y”将返回1或0,这都等于0之后 正在通过toupper通过。

你要代替的是:

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

其他错误就出在这里:

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! 
} 

你真正想要做的就是再次询问用户是否进入了错事, 这样:

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')); 
+0

toupper(1)被记录为返回1:“如果没有这样的转换是可能的,则返回的值不变。你为什么认为toupper会返回0? – abelenky 2010-05-03 23:08:20

+0

@abelenky:因为如果它没有返回0,那么程序不会终止。 – 2010-05-03 23:11:35

+0

谢谢比利先生。我感谢你的时间。 – user331861 2010-05-03 23:30:58

0

一个错误:

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

应该

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

谢谢贝塔莫先生。 – user331861 2010-05-03 23:30:06

2

条件

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

始终是真实的,所以你的程序,无论用户输入的终止。

此外,您将break放入代码中用于处理错误输入的每个ifbreak将终止循环和程序。这是一个相当奇怪的逻辑:要求用户再试一次,然后立即终止程序,而不让用户有机会再次尝试。你为什么终止程序,而不是让用户重新输入?

+0

谢谢安德烈先生 – user331861 2010-05-03 23:27:15