【发布时间】: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 值,您也将得到零。
-
天哪,非常感谢你!现在对我来说似乎有点明显,但我整天都遇到麻烦。
-
时间意识。这是由于缺乏