我今天正在为办公室的一个项目工作,而今天却遇到了一个绊脚石。我的代码已正确调试,但是在运行时它会忽略我设置的所有参数,并在一行可执行文件中将所有文件粉碎在一起(如果有意义)。这是提示信息,以及到目前为止的内容:
“企业需要为销售代表计算奖励积分。奖励积分基于每个销售代表每年销售多少。
编写一个程序,提示用户输入销售代表的销售额。为每组销售代表提供不同的功能。
将奖励积分显示为整数。
请参阅显示组,销售和奖励积分的表:
团体销售奖励积分:
$ 0-$ 100,000500点
$ 100,001-$ 1,000,000 1,500点
B $ 1,000,001-$ 2,000,000 2,000点
$ 2,000,001-$ 3,000,000 2,500点
C $ 3,000,001-$ 4,000,000 3,000点
$ 4,000,001和5,000点以上
输入验证:不接受负数的销售。”
这是到目前为止我尝试过的事情:
#include <iostream>
#include <iomanip>
using namespace std;
float salesamounts; //sales made
int username; // Sales rep name
int main()
{
cout << “Please enter your sales made for the year: $”
cin >> salesamounts;
cout << endl;
// exception on program
if (salesamounts <= -1);
{
if (salesamounts <= -1);
cout << “Value cannot be negative. Please input again.”
}
// Group A placement
for (;salesamount <= 100000;)
{
if ((salesamounts <= 100000)
cout << “Congratulations! You earned 500 Bonus Points!”
break;
{
for (; salesamounts > 100001 < 1000000;)
{
if ((salesamounts > 100001 < 1000000));
cout << “Congrats! 10000 Bonus Points have been credited to you!”
break;
}
答案 0 :(得分:0)
根据您的要求,您可以使用if-elseif-else
来判断每组销售代表,例如:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
double salesamounts; //sales made
int username; // Sales rep name
cout << "Please enter your sales made for the year : $" << endl;
while (cin >> salesamounts) {
// exception on program
if (salesamounts < 0.0) {
cout << "Value cannot be negative.Please input again." << endl;
}
//judge salesamounts
else if (salesamounts>0.0 && salesamounts <= 100000.0) {
cout << "Congratulations!You earned 500 Bonus Points!" << endl;
}
else if (salesamounts > 100000.0 && salesamounts <= 1000000.0) {
cout << "Congrats!1500 Bonus Points have been credited to you!" << endl;
}
else if (salesamounts > 1000000.0 && salesamounts <= 2000000.0) {
cout << "Congrats!2,000 Bonus Points have been credited to you!" << endl;
}
else if (salesamounts > 2000000.0 && salesamounts <= 3000000.0) {
cout << "Congrats!2,500 Bonus Points have been credited to you!" << endl;
}
else if (salesamounts > 3000000.0 && salesamounts <= 4000000.0) {
cout << "Congrats!3,000 Bonus Points have been credited to you!" << endl;
}
else if (salesamounts > 4000000.0 ) {
cout << "Congrats!5,000 Bonus Points have been credited to you!" << endl;
}
else {
break;
}
}
}
顺便说一句,您也可以使用switch statement进行此操作。
答案 1 :(得分:0)
您几乎正确地完成了输入部分,但是之后您将if-else-if
部分与for
循环混淆了。
接受用户输入时,可以使用while循环检查该值是否为负。如果输入的值不符合条件,这将使程序提示用户再次输入值:
float salesamounts;
cout<<"Enter the sales amounts for the year:"; //the program will prompt the user initially
cin>>salesamounts; //user inputs the value here
while(salesamounts<0) //this line checks if the value is negative, if yes, it'll continue running this block till the value is non-negative
{
cout<<"Negative numbers not allowed. Please enter again: "; //Suppose the value is negative. Then the program will display this line and prompt the user to enter again
cin>>salesamounts; //user enters the value again
}
程序的下一步是检查用户输入,并使程序采取相应的措施。 if-else
语句将完美地做到这一点:
if (salesamount<=100000) //checks the value. If this is true, then then the following line will run. Else, the program will move to the next check.
cout<<"Bonus point is 500"; //displays the output if the above condition is correct
else if (salesamount<=1000000) //second check
cout<<"Bonus point is 1500"; //displays if the second condition is true
.
.
.
else
cout<<"Bonus point is 5000";
以此类推。
仅说明一下,仅当您希望特定代码块重复运行时才使用循环,而要检查或比较两个值,则使用 if-else < / strong>声明。
请注意:我没有将第二张支票写成if (salesamounts>=100001 && salesamounts<=1000000)
,而且我给的最后一张支票也只有else
,而不是else if
。 / p>