【问题标题】:My current code isn’t running properly in MS Visual Studio?我当前的代码在 MS Visual Studio 中运行不正常?
【发布时间】:2021-01-29 12:39:50
【问题描述】:

我今天正在为办公室做一个项目,但我今天遇到了一些意外。我的代码已正确调试,但它在运行时会忽略我设置的所有参数,并在一行可执行文件中一起粉碎(如果这有意义的话)。这是提示以及我目前所拥有的:

“企业需要计算销售代表的奖励积分。奖励积分基于每位销售代表当年的销售额。

编写一个程序,提示用户输入销售代表的销售额。为每组销售代表包括不同的职能。

将奖励积分显示为整数。

请参阅显示组、销售和奖励积分的表格:

团体销售奖励积分:

0 美元 - 100 美元,000 500 点

$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;
    }

【问题讨论】:

  • edit您的问题并格式化您的代码。您可以在“编辑问题”页面上找到格式帮助:(?) =&gt; 'Code'。格式化使其他人更容易阅读和提供建议。
  • if (salesamounts &lt;= -1); 等同于if (salesamounts &lt;= -1) { /* empty body of if statement */ } 去掉末尾的分号。
  • 注意:无论您输入什么,您的报价都会受到影响。
  • when ran 贴出的代码距离编译还有几步之遥,更别说运行了。
  • 我将代码扔进格式化程序以快速缩进它,它发现了一些错误。使用常规缩进方案,我使用Allman style,它会在你犯错时向你显示错误。因为其中一些错误可能是您正在寻找的错误,所以我不会更新代码,但这里有一个提示:确保您确实需要您正在使用的所有分号。

标签: c++ visual-studio visual-c++ compiler-errors


【解决方案1】:

根据您的要求,您可以使用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 来执行此操作。

【讨论】:

    【解决方案2】:

    您输入部分几乎正确,但之后您将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 声明。

    请注意我没有将第二次检查写为if (salesamounts&gt;=100001 &amp;&amp; salesamounts&lt;=1000000),而且我将最后一次检查写为else,而不是else if

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-21
      • 2014-11-28
      • 1970-01-01
      • 1970-01-01
      • 2018-09-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多