【发布时间】:2020-12-27 08:24:24
【问题描述】:
尝试通过使用 if 语句更改其值来使用 int 创建一个依赖于输入的变量。可能犯了一个愚蠢的错误,但到目前为止,无法弄清楚(作为旁注,也尝试使用 Switch 语句,也无法使其工作)。
#include <iostream>
#include <string>
int main(){
int jobID;
std::cin >> jobID;
int setjobSalary (int jobSalary) {
if (jobID == 1) {
setjobSalary = 600;
}
else if (jobID == 2) {
setjobSalary = 1600;
}
return jobSalary;
}
int currentjobSalary = jobSalary - 300;
}
提前致谢。
【问题讨论】:
-
你看到编译器的错误信息了吗?
main.cpp: In function 'int main()': main.cpp:9:46: error: a function-definition is not allowed here before '{' token函数不能在其他函数内部定义(除非它们是 lambda 函数)。更不用说你从不调用setjobSalary()函数,它应该return值。setjobSalary = 600;是错误的。 c++ 与 Basic 不同。 -
你不能像这样在
main的主体内定义第二个函数setjobSalary。
标签: c++ if-statement return