令牌之前'{'预期的非限定 id
Expected unqualified-id before '{' token
我正在尝试编写一个程序,该程序将根据劳动率生成劳动时间,但我遇到了Expected unqualified-id before '{' token
错误。
include <iostream>
using namespace std;
double S516 = 5.5; // 516-70 Alloy welds at 5.5 inches per minute
double A304 = 4.3; // A304 Steel welds at 4.3 inches per minute
double x; // This value is a placeholder for total linear inches to be welded
int main()
{
cout << "Please enter the total number of inches to be welded:n ";
cin >> x;
}
{
if S516, then multiplies[(S516 * x *1.1 / 60];
else if A304, then multiplies[(A304 * x * 1.1 / 60];
}
我甚至走在这一切的正确轨道上吗?
试试这个:
#include <iostream>
using namespace std;
double S516 = 5.5; // 516-70 Alloy welds at 5.5 inches per minute
double A304 = 4.3; // A304 Steel welds at 4.3 inches per minute
enum material {
material_S516 = 1,
material_A304 = 2
};
int main()
{
double x; // This value is a placeholder for total linear inches to be welded
cout << "Please enter the total number of inches to be welded:" << endl;
cin >> x;
int material_to_weld;
cout << "Please indicate the material you have to weld (enter 1 for S516 or 2 for A304):" << endl;
cin >> material_to_weld;
double time_to_weld, corrected_time_to_weld;
if (material_to_weld == material_S516) {
time_to_weld = S516 * x * 1.1 / 60;
corrected_time_to_weld = x * 1.1 / S516 / 60;
} else if (material_to_weld == material_A304) {
time_to_weld = A304 * x * 1.1 / 60;
corrected_time_to_weld = x * 1.1 / A304 / 60;
} else {
cout << "Invalid material! Please choose either 1 or 2!" << endl;
return(1);
}
cout << "With the original formula, welding " << x << " inches of this material will take " << time_to_weld << " hours." << endl;
cout << "With the corrected formula, welding " << x << " inches of this material will take " << corrected_time_to_weld << " hours." << endl;
return 0;
}
这不是我真正写它的方式,但我试图保持简单。您应该真正注意到的一件事是您的配方看起来不对 - 焊接所需的时间随着速度的增加而增加。时间=空间/速度,所以我认为你想用x * 1.1 / S516 / 60
.这样,如果速度增加,时间就会减少,正如预期的那样。但无论如何,我保留了这两个计算。
如您所见,所有代码都在 main()
函数中。在您的代码中,是的,它在大括号内,但这些大括号没有任何意义,而且它们在main()
之外。你得到的错误很神秘,但这就是它的意思。
更多评论:
- 这是
#include
,不是include
! - 您的 S516 和 A304 变量被声明为全局变量,因为它们在
main()
之外。我把它们留在那里,但你实际上可以把它们移到main()
.在这种情况下,没有实际区别,但最好仅在真正需要时才使用全局变量(这种情况可能发生,但很少发生)。 - 正如我在一些评论中告诉您的那样,您需要一个变量来选择材料,以及相应的
cout
和cin
.请注意,当您读取用户的输入时,您通常应该验证它,即检查它是否有效。例如,在这里,接受的值是1
和2
,如果用户输入 3,程序将显示"无效材料!请选择 1 或 2!"并终止。另一种方法是将其插入到一个循环中,该循环不断要求用户选择 1 或 2,并且只有在用户提供有效输入后,循环才会停止 - 我为材料使用了
enum
。枚举通常比整数更安全,但我还没有以非常好的方式使用它。不过,你可能有很多东西需要你学习,我决定保持简单,所以我像这样使用它。
我希望这能帮助你。
你在这里有几个问题:
在你的主函数中,你需要 if/else-if 语句与 cout/cin 位于相同的大括号内。
以下两行在代码C++无效:
if S516, then multiplies [(S516 * x *1.1 / 60];
else if A304, then multiplies [(A304 * x * 1.1 / 60];
相反,请尝试:
double answer = 0.0;
if (x==S516){
answer = (S516 * x * 1.1 / 60);
}
else if (x==A304){
answer = (A304 * x * 1.1 / 60);
}
我不知道你的算术在做什么,所以我不理会它,但我觉得这不是你想要计算的。但我会把那部分留给你。
此外,作为大多数人部分的偏好问题,尽量避免使用全局变量。对于"双 x;"我会把它移到主要。
看起来您可能是编程/C++的新手并且正在跳枪。您应该从基本教程开始。
使用另一个答案来获得完整的解决方案。
相关文章:
- C++:"("令牌"之前有预期的非限定 id 指向类中成员函数的指针
- 错误:令牌 { '{' 之前应存在非限定 ID
- 令牌 C++ 错误之前'['非限定 ID
- 修复令牌错误之前';'预期的非限定 ID
- C 错误:令牌之前'['预期为非限定 id
- "["令牌和 + 之前应为非限定 ID
- 屏幕显示脚本中'.'令牌之前的预期非限定 id
- “OR”令牌之前的预期非限定 ID
- 声明中的限定 ID 在"="令牌/对象计数器变量之前
- 需要调试"在')'令牌之前预期的非限定id",第一次使用c ++的用户
- 在"." 令牌之前应为非限定 ID(C++ 错误)
- 在“.”令牌之前应为非限定 ID
- 令牌之前应'{'非限定 ID
- 函数概率:令牌之前'['预期的非限定 id
- 预期在令牌之前的未合格ID
- 错误:预期的非限定 ID befo 重新'{'令牌
- Arduino 错误在令牌之前预期为非限定 id '/'
- 错误:在 Arduino 中'{'令牌之前预期未限定 ID
- 令牌之前')'预期的非限定 id?
- 令牌之前应为非限定 ID ')'