在 C 中不断收到错误消息,它们是什么意思?
Keep getting error messages in C what do they mean?
有人可以向我解释这些错误消息是什么意思我不断收到“错误:预期';'在顶级声明之后”和“预期标识符或'('”。我对编程很陌生,不理解任何建议。我的程序编码是否正确?它的目的是检查用户输入的内容是否正确结果是一个三角形。这是我的代码:
#include <cs50.h>
#include <stdio.h>
bool check_triangle(float x, float y, float z);
int main(void)
{
printf("what is the length of the first side of the triangle: ");
float x = get_float();
printf("what is the length of the second side of the triangle: ");
float y = get_float();
printf("what is the length of the third side of the triangle: ");
float z = get_float();
bool check_triangle(float x, float y, float z);
if
{
(x <= 0 || y <= 0 || z <= 0)
return false;
}
if
{
( x+ y <= z || x + <= y || y + z <= x)
return false;
}
return true
}
在学习 C 语言的过程中,您在语法的几个方面遇到困难。首先,C 语言不允许嵌套函数。虽然缺少 main()
的结束 '}'
可能会解释部分问题——看起来你试图在 main()
内部定义 check_triangle()
——那是行不通的.
下一步,除非函数太长以至于不能简单地放在 main()
上面而不完全弄乱你的文件,否则忘记尝试在 main()
上面提供函数原型和下面的定义 - - 你可以稍后再做。
在您的测试中,x + <= y
是一个不完整的表达式。看来您只是不小心遗漏了 z
。然后,在您尝试 definition of check_triangle()
之后包含一个杂散的 ';'
-- 一个函数是 return type function_name (arguments) { /* body */ }
而在关闭之后没有 ';'
')'
定义参数。
如果您修复这些问题,您的函数将如下所示:
bool check_triangle (float x, float y, float z) {
if (x <= 0. || y <= 0. || z <= 0.) {
return false;
}
if ( x + y <= z || x + z <= y || y + z <= x) {
return false;
}
return true;
}
在 CS50 中,您的 get_float()
函数需要一个字符串作为提示。您不需要单独的 printf
语句。相反,您需要,例如:
float x = get_float("Length of the first side of the triangle: ");
float y = get_float("Length of the second side of the triangle: ");
float z = get_float("Length of the third side of the triangle: ");
最后,您需要调用函数check_triangle()
来检查输入的是不是三角形。在那里你只需使用函数名和参数列表。 (您需要检查 return 以了解您是否有三角形)。所以在你获得x
、y
和z
之后,你需要做这样的事情:
if (check_triangle (x, y, z)) {
puts ("\nyou have a triangle");
}
else {
puts ("\nnot a triangle");
}
总而言之,您将拥有:
#include <cs50.h>
#include <stdio.h>
bool check_triangle (float x, float y, float z) {
if (x <= 0. || y <= 0. || z <= 0.) {
return false;
}
if ( x + y <= z || x + z <= y || y + z <= x) {
return false;
}
return true;
}
int main (void) {
float x = get_float("Length of the first side of the triangle: ");
float y = get_float("Length of the second side of the triangle: ");
float z = get_float("Length of the third side of the triangle: ");
if (check_triangle (x, y, z)) {
puts ("\nyou have a triangle");
}
else {
puts ("\nnot a triangle");
}
}
要编译,请始终启用警告(对于gcc/clang -Wall -Wextra -pedantic
,我建议-Wshadow
)并且直到它会在没有警告的情况下编译。如果你调用你的源文件 checktriangle.c
,你可以使用:
gcc -Wall -Wextra -pedantic -Wshadow -std=c11 -Ofast -o checktriangle checktriangle.c -lcs50
那么您应该可以测试您的代码了。检查一下,如果您还有其他问题,请告诉我。
以下是我在代码中发现的几个错误
- 缺少
main
的右大括号 }
- 在函数减速中
bool check_triangle(float x, float y, float z);
不需要分号并且缺少左大括号 {
。
- 在
if
中,条件应该是这种格式的if(your condition)
,而不是if{}
。
x + <= y
是错误的左值,缺少 x+something <= y
- 遵循正确的格式以提高可读性。
请参阅下面的代码并更正这些。
假设 #include <cs50.h>
& get_float()
您正在照顾
#include <cs50.h>
#include <stdio.h>
bool check_triangle(float x, float y, float z);
int main(void)
{
float x, y, z;
printf("what is the length of the first side of the triangle: \n");
x = get_float();
printf("what is the length of the second side of the triangle: \n");
y = get_float();
printf("what is the length of the third side of the triangle: \n");
z = get_float();
if(check_triangle(x,y,z))
{
printf("Check triangle is success\n ");
}
else
{
printf("Check triangle failed\n ");
}
return 0;
}
bool check_triangle(float x, float y, float z)
{
if(x <= 0 || y <= 0 || z <= 0)
{
return false;
}
if( x + y <= z || x + z <= y || y + z <= x)
{
return false;
}
return true;
}
有人可以向我解释这些错误消息是什么意思我不断收到“错误:预期';'在顶级声明之后”和“预期标识符或'('”。我对编程很陌生,不理解任何建议。我的程序编码是否正确?它的目的是检查用户输入的内容是否正确结果是一个三角形。这是我的代码:
#include <cs50.h>
#include <stdio.h>
bool check_triangle(float x, float y, float z);
int main(void)
{
printf("what is the length of the first side of the triangle: ");
float x = get_float();
printf("what is the length of the second side of the triangle: ");
float y = get_float();
printf("what is the length of the third side of the triangle: ");
float z = get_float();
bool check_triangle(float x, float y, float z);
if
{
(x <= 0 || y <= 0 || z <= 0)
return false;
}
if
{
( x+ y <= z || x + <= y || y + z <= x)
return false;
}
return true
}
在学习 C 语言的过程中,您在语法的几个方面遇到困难。首先,C 语言不允许嵌套函数。虽然缺少 main()
的结束 '}'
可能会解释部分问题——看起来你试图在 main()
内部定义 check_triangle()
——那是行不通的.
下一步,除非函数太长以至于不能简单地放在 main()
上面而不完全弄乱你的文件,否则忘记尝试在 main()
上面提供函数原型和下面的定义 - - 你可以稍后再做。
在您的测试中,x + <= y
是一个不完整的表达式。看来您只是不小心遗漏了 z
。然后,在您尝试 definition of check_triangle()
之后包含一个杂散的 ';'
-- 一个函数是 return type function_name (arguments) { /* body */ }
而在关闭之后没有 ';'
')'
定义参数。
如果您修复这些问题,您的函数将如下所示:
bool check_triangle (float x, float y, float z) {
if (x <= 0. || y <= 0. || z <= 0.) {
return false;
}
if ( x + y <= z || x + z <= y || y + z <= x) {
return false;
}
return true;
}
在 CS50 中,您的 get_float()
函数需要一个字符串作为提示。您不需要单独的 printf
语句。相反,您需要,例如:
float x = get_float("Length of the first side of the triangle: ");
float y = get_float("Length of the second side of the triangle: ");
float z = get_float("Length of the third side of the triangle: ");
最后,您需要调用函数check_triangle()
来检查输入的是不是三角形。在那里你只需使用函数名和参数列表。 (您需要检查 return 以了解您是否有三角形)。所以在你获得x
、y
和z
之后,你需要做这样的事情:
if (check_triangle (x, y, z)) {
puts ("\nyou have a triangle");
}
else {
puts ("\nnot a triangle");
}
总而言之,您将拥有:
#include <cs50.h>
#include <stdio.h>
bool check_triangle (float x, float y, float z) {
if (x <= 0. || y <= 0. || z <= 0.) {
return false;
}
if ( x + y <= z || x + z <= y || y + z <= x) {
return false;
}
return true;
}
int main (void) {
float x = get_float("Length of the first side of the triangle: ");
float y = get_float("Length of the second side of the triangle: ");
float z = get_float("Length of the third side of the triangle: ");
if (check_triangle (x, y, z)) {
puts ("\nyou have a triangle");
}
else {
puts ("\nnot a triangle");
}
}
要编译,请始终启用警告(对于gcc/clang -Wall -Wextra -pedantic
,我建议-Wshadow
)并且直到它会在没有警告的情况下编译。如果你调用你的源文件 checktriangle.c
,你可以使用:
gcc -Wall -Wextra -pedantic -Wshadow -std=c11 -Ofast -o checktriangle checktriangle.c -lcs50
那么您应该可以测试您的代码了。检查一下,如果您还有其他问题,请告诉我。
以下是我在代码中发现的几个错误
- 缺少
main
的右大括号}
- 在函数减速中
bool check_triangle(float x, float y, float z);
不需要分号并且缺少左大括号{
。 - 在
if
中,条件应该是这种格式的if(your condition)
,而不是if{}
。 x + <= y
是错误的左值,缺少x+something <= y
- 遵循正确的格式以提高可读性。
请参阅下面的代码并更正这些。
假设
#include <cs50.h>
&get_float()
您正在照顾#include <cs50.h> #include <stdio.h> bool check_triangle(float x, float y, float z); int main(void) { float x, y, z; printf("what is the length of the first side of the triangle: \n"); x = get_float(); printf("what is the length of the second side of the triangle: \n"); y = get_float(); printf("what is the length of the third side of the triangle: \n"); z = get_float(); if(check_triangle(x,y,z)) { printf("Check triangle is success\n "); } else { printf("Check triangle failed\n "); } return 0; } bool check_triangle(float x, float y, float z) { if(x <= 0 || y <= 0 || z <= 0) { return false; } if( x + y <= z || x + z <= y || y + z <= x) { return false; } return true; }