我正在尝试编写一个基于人工费率生成人工时间的程序,而且我遇到了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];
}
我是否在所有这一切的正确轨道上?
答案 0 :(得分:0)
这里有一些问题:
在你的main函数中,你将if / else-if语句放在与cout / cin相同的大括号内。
以下两行不是有效的C ++代码:
f2("ta")
而是尝试:
if S516, then multiplies [(S516 * x *1.1 / 60];
else if A304, then multiplies [(A304 * x * 1.1 / 60];
我不知道你的算术是做什么的,所以我不管它,但我觉得这并不是你想要计算的。但是我会把那部分留给你。
此外,作为大多数人的部分优先考虑,尽量避免使用全局变量。对于&#34;双x;&#34;我会把它变成主要的。
看起来你可能不熟悉编程/ C ++而且正在开枪。你应该从基础教程开始。
使用其他答案获得完整解决方案。
答案 1 :(得分:0)
试试这个:
#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
!main()
之外。我把它们留在那里,但你实际上可以将它们移到main()
。在这种情况下,没有实际的区别,但只有在真正需要它们时才使用全局变量是一种很好的做法(这种情况可能发生,但很少发生)。cout
和cin
。请注意,当您阅读用户的输入时,您通常应对其进行验证,即检查它是否有效。例如,在此处,接受的值为1
和2
,如果用户输入3,则程序将显示&#34;无效的材料!请选择1或2!&#34;并终止。另一种方法可能是将此插入循环,不断要求用户选择1或2,并且循环仅在用户提供有效输入后停止enum
作为素材。枚举通常比整数更安全,但我还没有以一种非常好的方式使用它。尽管如此,你可能还有很多东西需要学习,而且我决定保持简单,所以我就这样使用它了。我希望这可以帮到你。