程序运行而不会崩溃,但无论该人将什么作为变量,答案都是 0

Program runs without crashing but the answers are 0 no matter what the person puts as a variable

本文关键字:什么 变量 答案 运行 崩溃 程序      更新时间:2023-10-16

好的,所以它会运行,但它不进行计算。当窗口出现时,我输入 A B 或 C,然后输入单词的长度它只是说支付=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;
}

它现在卡在每个的第一个固定工资上作为输出,它将完成第一部分<=7500,但上面的任何数字都会将答案设置为第一个固定工资。我编辑了上面的问题以显示新代码

这是新代码:

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

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

....
cout << "Pay is: $" << payTotal << endl; 
int payTotal;
// code to actually compute the value of payTotal.

另请注意,您有一个全局和本地支付总计。在这种情况下,这并不重要,但是当你编写更复杂的代码时,这真的会咬你。

正如@Andrew指出的那样,int/float 转换还有另一个问题。此代码:

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

如果payTotal小于 13,则将numberOfWords设置为 0。原因是微妙的,所以 thunb 的一个好规则是,如果你正在处理浮点数,那么让计算中的每个变量都成为浮点数。

这是因为你有返回 0; 最后,它应该是返回 payTotal;