【问题标题】:Program runs without crashing but the answers are 0 no matter what the person puts as a variable程序运行时不会崩溃,但无论该人将什么作为变量,答案都是 0
【发布时间】:2014-03-05 03:27:55
【问题描述】:

好的,这样就可以运行了,但它不进行计算。 当窗口出现时,我输入 A B 或 C 然后输入单词的长度 它只是说pay=0。我也尝试了几种不同的方法,但我不知道如何让它重置,以便人们输入新的作者级别和字数

代码如下:

#include <iostream>




using namespace std;


char authorLevel;
int numberOfWords, payTotal;
int fixedPayAmount;



int main()
{


cout << "Enter Author Level(A,B,or C):";
char authorLevel;
cin >>authorLevel;

cout << "Enter Length(in words):";
int numberOfWords;
cin >>numberOfWords;

cout << "Pay is: $" << payTotal << endl; 
int payTotal;


cout << "Fixed Pay is:$" << fixedPayAmount << endl;
int fixedPayAmount;

//Calculations for C Level Author


if (authorLevel == 'C')
{

         //If the Number of words is <7500 multiply by 0.08     
              if (numberOfWords <=7500)
                  {
                    payTotal= numberOfWords * 0.08;                     
                  }

        //If the numberOfWords is >7500 to <=8000 pay is fixed 600
                if (numberOfWords >7500 || numberOfWords <= 8000)
                    {
                        fixedPayAmount= 600;

                    }
        //If the numberOfWords is >8000 to <=17500 multiply by 0.075
              if (numberOfWords >8000 || numberOfWords <=17500)     
                    {
                    payTotal= numberOfWords * 0.075;
                    }
        //If the numberOfWords is >17500 to <= 19000 fixed $1313
             if (numberOfWords >17500 || numberOfWords <= 19000)
                    {
                        fixedPayAmount=1313;
                    }
        //If the numberOfWords is >=19000 multiply 0.07
            if (numberOfWords >=19000)
            {
                payTotal= numberOfWords * 0.07;
            }
}

else if (authorLevel== 'A')
    {


         //If the Number of words is <7500 multiply by 0.14     
              if (numberOfWords <=7500)
                  {
                    payTotal= numberOfWords * 0.14;
                    }

        //If the numberOfWords is >7500 to <=8000 pay is fixed $1050
                if (numberOfWords >7500 || numberOfWords <= 8000)
                    {
                        fixedPayAmount= 1050;

                    }
        //If the numberOfWords is >8000 to <=17500 multiply by 0.13125
              if (numberOfWords >8000 || numberOfWords <=17500)     
                    {                                               
                    payTotal= numberOfWords * 0.13125;
                    }
        //If the numberOfWords is >17500 to <= 19000 fixed $2297.75
             if (numberOfWords >17500 || numberOfWords <= 19000)
                    {
                        fixedPayAmount=2297.75;
                    }
        //If the numberOfWords is >=19000 multiply 0.1225
            if (numberOfWords >=19000)
            {
                payTotal= numberOfWords * 0.1225;
            }
    }



    else if (authorLevel== 'B')
    {

         //If the Number of words is <7500 multiply by 0.1  
              if (numberOfWords <=7500)
                  {
                        payTotal= numberOfWords * 0.1;
                    }

        //If the numberOfWords is >7500 to <=8000 pay is fixed $750
                if (numberOfWords >7500 || numberOfWords <= 8000)
                    {
                        fixedPayAmount= 750;

                    }
        //If the numberOfWords is >8000 to <=17500 multiply by 0.09375
              if (numberOfWords >8000 || numberOfWords <=17500)     
                    {
                    payTotal= numberOfWords * 0.09375;
                    }
        //If the numberOfWords is >17500 to <= 19000 fixed $1641.25
             if (numberOfWords >17500 || numberOfWords <= 19000)
                    {
                        fixedPayAmount=1641.25;
                    }
        //If the numberOfWords is >=19000 multiply 0.0875
            if (numberOfWords >=19000)
            {
                payTotal= numberOfWords * 0.0875;
            }
    }


            return 0;
}

它现在停留在每个人的第一个固定工资作为输出,它将执行第一部分

这是新代码:

#include <iostream>




using namespace std;


float authorLevel;
float numberOfWords, payTotal;
float fixedPayAmount;



int main()
{



cout << "Enter Author Level(A,B, or C):";
char authorLevel;
cin >>authorLevel;

cout << "Enter Length(in words):";
int numberOfWords;
cin >>numberOfWords;






//Calculations for C Level Author


if (authorLevel == 'C')
{

         //If the Number of words is <7500 multiply by 0.08     
              if (numberOfWords <=7500)
                  {
                        payTotal= (numberOfWords * 0.08);
                        cout << "Pay is: $" << payTotal << endl; 


                  }

        //If the numberOfWords is >7500 to <=8000 pay is fixed 600
                else if (numberOfWords >7500 || numberOfWords <= 8000)
                    {
                        fixedPayAmount= 600;
                    cout << "Fixed Pay is:$" << fixedPayAmount << endl;

                    }
        //If the numberOfWords is >8000 to <=17500 multiply by 0.075
              else if (numberOfWords >8000 || numberOfWords <=17500)        
                    {
                        payTotal= numberOfWords * 0.075;
                        cout << "Pay is: $" << payTotal << endl;
                    }
        //If the numberOfWords is >17500 to < 19000 fixed $1313
             else if (numberOfWords >17500 || numberOfWords < 19000)
                    {
                        fixedPayAmount=1313;
                    cout << "Fixed Pay is:$" << fixedPayAmount << endl;

                        }
        //If the numberOfWords is >=19000 multiply 0.07
        else if (numberOfWords >=19000)
            {
                payTotal= numberOfWords * 0.07;
                    cout << "Pay is: $" << payTotal << endl;
            }

}

else if (authorLevel== 'A')
    {


         //If the Number of words is <7500 multiply by 0.14     
             if (numberOfWords <=7500)
                  {
                        payTotal= numberOfWords * 0.14;
                        cout << "Pay is: $" << payTotal << endl;
                    }

        //If the numberOfWords is >7500 to <=8000 pay is fixed $1050
            else if (numberOfWords >7500 || numberOfWords < 8000)
                    {
                    fixedPayAmount= 1050;
                    cout << "Fixed Pay is:$" << fixedPayAmount << endl;                     
                    }
        //If the numberOfWords is >8000 to <=17500 multiply by 0.13125
              else if (numberOfWords >=8000 || numberOfWords <=17500)       
                    {                                               
                        payTotal= numberOfWords * 0.13125;
                        cout << "Pay is: $" << payTotal << endl;
                    }
        //If the numberOfWords is >17500 to <= 19000 fixed $2297.75
             else if (numberOfWords >17500 || numberOfWords <19000)
                    {
                        fixedPayAmount=2297.75;
                    cout << "Fixed Pay is:$" << fixedPayAmount << endl;
                    }
        //If the numberOfWords is >=19000 multiply 0.1225
            else if (numberOfWords >=19000)
            {
                payTotal= numberOfWords * 0.1225;
                    cout << "Pay is: $" << payTotal << endl;
            }
    }



    else if (authorLevel== 'B')
    {

         //If the Number of words is <7500 multiply by 0.1  
              if (numberOfWords <=7500)
                  {
                        payTotal= numberOfWords * 0.1;
                        cout << "Pay is: $" << payTotal << endl;
                    }

        //If the numberOfWords is >7500 to <=8000 pay is fixed $750
                else if (numberOfWords >7500 || numberOfWords <= 8000)
                    {
                        fixedPayAmount= 750;
                    cout << "Fixed Pay is:$" << fixedPayAmount << endl;                     
                    }
        //If the numberOfWords is >8000 to <=17500 multiply by 0.09375
              else if (numberOfWords >8000 || numberOfWords <=17500)        
                    {
                        payTotal= numberOfWords * 0.09375;
                        cout << "Pay is: $" << payTotal << endl;
                    }
        //If the numberOfWords is >17500 to <= 19000 fixed $1641.25
             else if (numberOfWords >17500 || numberOfWords < 19000)
                    {
                        fixedPayAmount=1641.25;
                    cout << "Fixed Pay is:$" << fixedPayAmount<< endl;
                    }
        //If the numberOfWords is >=19000 multiply 0.0875
            else if (numberOfWords >=19000)
            {
                payTotal= numberOfWords * 0.0875;
                cout << "Pay is: $" << payTotal << endl;
            }
    }

            return 0;

}

【问题讨论】:

  • 这段代码中有两个payTotal变量。您还可以在计算其值之前打印payTotal
  • 另外,作为前助教和评分员,请正确缩进代码。
  • 您还将一个 int 乘以一个浮点数并将结果分配回一个 int,您可能应该将 payTotal 设为浮点数。否则,对于较小的 numberOfWords 值,您也将得到零。
  • 天哪,非常感谢你!现在对我来说似乎有点明显,但我整天都遇到麻烦。
  • 时间意识。这是由于缺乏

标签: c++ variables output


【解决方案1】:

您在计算之前打印出该值。 int payTotal;

....
cout << "Pay is: $" << payTotal << endl; 
int payTotal;

// code to actually compute the value of payTotal.

另请注意,您有一个全局和一个本地 payTotal。在这种情况下,这无关紧要,但是当您编写更复杂的代码时,这真的会咬到您。

正如@Andrew 指出的那样,int/float 转换存在另一个问题。这段代码:

          if (numberOfWords <=7500)
              {
                payTotal= numberOfWords * 0.08;                     
              }

如果 numberOfWords 小于 13,则将 payTotal 设置为 0。原因很微妙,因此 thunb 的一个好的规则是,如果您正在处理浮点数,则将计算中的每个变量都设为浮点数.

【讨论】:

  • 它现在停留在每个人的第一个固定工资作为输出,它会执行第一部分
【解决方案2】:

因为你有 return 0;最后应该是 return payTotal;

【讨论】:

    猜你喜欢
    • 2016-05-09
    • 1970-01-01
    • 2015-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-11
    相关资源
    最近更新 更多