C语言 如何解决错误:预期标识符或“(”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14011644/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to solve error: expected identifier or '('
提问by Johanna
I've got a problem with something I'm programming. I get this error over and over;
我在编程时遇到了问题。我一遍又一遍地得到这个错误;
jharvard@appliance (~/Dropbox/pset1): make mario
clang -ggdb3 -O0 -std=c99 -Wall -Werror mario.c -lcs50 -lm -o mario
mario.c:23:5: error: expected identifier or '('
do
^
mario.c:32:1: error: expected identifier or '('
do
^
2 errors generated.
I searched all over the internet but couldn't find the problem..
removing the ; after int main(void)didn't help
我搜索了整个互联网,但找不到问题.. 删除; 在int main(void)没有帮助之后
This is my code:
这是我的代码:
#include <stdio.h>
#include <cs50.h>
#include <math.h>
int main(void);
//Ask User for Height, and check
int a, b, rows, height;
int a = 0;
int b = 0;
int rows = 1;
do
{
printf ("Height: ");
height = GetInt();
}
while (height <=0 || height > 23);
//build half pyramid
do
{
do
{
printf("r");
a++;
}
while (a < height - rows);
do
{
printf("#");
b++;
}
while (b < rows + 1);
printf("\n");
rows++;
while (rows <= height);
}
I've been trying to solve this problem for a few days, but i just can't figure it out!
我已经尝试解决这个问题几天了,但我就是想不通!
Thank you so much in advance!
非常感谢您!
回答by Alok Save
int main(void);
You have just declared the main. You need to define it and add the code inside that definition.
您刚刚声明了main. 您需要定义它并在该定义中添加代码。
int main()
{
.....
}
回答by dreadlock
You got nested loop with do/while. Make sure that each start with do end with while.
你得到了 do/while 的嵌套循环。确保每个都以 do 开头,以 while 结尾。
Look like at the end of file, the "while" is not correct.
看起来像在文件末尾,“while”不正确。
printf("\n");
rows++;
while (rows <= height);
}
That could be you missing the close '}' before 'while (rows <= height);'
那可能是您在 'while (rows <= height);' 之前错过了关闭的 '}'
Correct code could be:
正确的代码可能是:
int main(void)
{
//Ask User for Height, and check
int a, b, rows, height;
a = 0; // <- removed int
b = 0; // <- removed int
rows = 1; // <- removed int
do
{
printf ("Height: ");
height = GetInt();
}
while (height <=0 || height > 23);
//build half pyramid
do
{
do
{
printf("r");
a++;
}
while (a < height - rows);
do
{
printf("#");
b++;
}
while (b < rows + 1);
printf("\n");
rows++;
} // <- add }
while (rows <= height);
}
回答by Blastcore
Main post is edited, so clear answer.
主要帖子已编辑,所以答案很清楚。
All your code is outside of a function because you're doing int main(); you're declaring a function. Use {} brackets instead.
你所有的代码都在函数之外,因为你在做 int main(); 你在声明一个函数。请改用 {} 括号。
int main() {
//Code here.
}
回答by Sunil Bojanapally
int a, b, rows, height;
int a = 0;
int b = 0;
Here in your above statements aand bare re-declared more than once causing compilation error.
在您的上述语句中,a并b多次重新声明导致编译错误。
回答by Voicu
If you don't indent your code, which you (by all means) should do, at leastwrite the starting and the ending curly brackets at once when you write the loop statement, before putting any code into that loop's body (which goes between the curly brackets). It will save you from troubles like these in the future.
如果你不缩进你的代码,你(无论如何)应该这样做,至少在你编写循环语句时,在将任何代码放入该循环的主体之前(它介于大括号)。它将使您免于将来遇到此类麻烦。
回答by Konstantin Dinev
Take the last while outside of the do scope:
采取 do 范围之外的最后一段时间:
while (rows <= height);

