我是个C++编程新手,我正在尝试书中的一段代码。当我尝试构建并运行它时,我得到的错误和警告使我无法运行该程序。不知道为什么。我的代码是逐字编写的。我也在PC上使用codeBlocks。
#include <stdio.h>
int main()
{
char choice;
printf("Are you filing a single, joint or ");
printf("Married return (s, j, m)? ");
do
{
scanf(" %c ", &choice);
switch (choice)
{
case ('s') : printf("You get a $1,000 deduction.\n");
break;
case ('j') : printf("You geta 1 $3,000 deduction.\n");
break;
case ('m') : printf("You geta $5,000 deduction.\n");
break;
default : printf("I don't know the ");
printf("option %c.\n, choice");
printf("Try again.\n");
break;
}
}while ((choice != 's') && (choice != 'j') && (choice != 'm');
return 0;
}发布于 2013-07-22 22:49:12
该错误是因为While语句中缺少)。
目前是:while ((choice != 's') && (choice != 'j') && (choice != 'm');
它应该是
while ((choice != 's') && (choice != 'j') && (choice != 'm'));
除此之外,您的scanf和printf语句也存在问题。
目前它们是:scanf(" %c, &choice");
和
printf("option %c.\n, choice");
应将这些更改为:scanf(" %c", &choice);
和
printf("option %c.\n", choice);
如果在编写代码时小心,这些类型的问题可以很容易地避免。
发布于 2013-07-22 22:50:03
您有几个语法问题。
在您的scanf行中,右双引号放在错误的位置,它应该在%c之后。
scanf(" %c", &choice);在you while行中,行尾缺少一个右括号。
} while ((choice != 's') && (choice != 'j') && (choice != 'm'));修复这两个错误会使程序对我来说编译和运行得很好。
发布于 2013-07-22 22:51:10
In function 'main':
Line 25: error: expected ')' before ';' token
Line 27: error: expected ';' before '}' token
Line 27: error: expected declaration or statement at end of inputhttp://codepad.org/tXK1DlsJ
首先,不要关闭do-while循环的大括号。您需要在末尾添加大括号。
while ((choice != 's') && (choice != 'j') && (choice != 'm'));此外,正如其他人所提到的,您需要将scanf语句更改为
scanf(" %c", &choice);https://stackoverflow.com/questions/17790503
复制相似问题