程序运行而不会崩溃,但无论该人将什么作为变量,答案都是 0
Program runs without crashing but the answers are 0 no matter what the person puts as a variable
好的,所以它会运行,但它不进行计算。当窗口出现时,我输入 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;
相关文章:
- 有没有什么方法可以使用一个函数中定义的常量变量,也可以由c++中同一程序中的其他函数使用
- 在 .h 文件中的类中声明静态变量和在.cpp文件中声明"global"变量有什么区别
- 未初始化的变量有什么危险
- 在C/C++中将变量名定义为__00000001有什么好处吗
- 我可以在这里替换什么,因为我不能在 C# 中使用隐式变量的 lambda 函数?
- Visual C++: MSVC vs. GCC+CLANG: 处理 lambda 捕获类成员变量,正确的方法是什么?
- 存储变量的更有效方法是什么?
- 变量 BitMask 在函数 CeilLog2 中的实际效果是什么?
- 这个变量在 C++ 中的范围是什么?
- 从二进制流中读取时,将双精度变量的地址转换为 char* 意味着什么?
- 变量按什么顺序相乘
- 与普通变量相比,仅仅读取原子变量的性能有什么不同吗
- 使用 gtest 框架在单元测试代码中检查目标对象的私有变量的最佳实践是什么?
- C++在变量的内存地址上做什么来"deallocate"它?
- 有什么方法可以使用 int 变量来完成组件名称吗?
- 从"LLONG_MAX 秒"构造 std::chrono::毫秒变量时发生了什么?
- 将共享指针传递给函数参数 - 将其分配给局部变量的正确方法是什么
- 正在连接的等待条件变量的线程会发生什么情况?
- 当我不确定输入是什么时,使用什么变量?
- 对于非常大的整数,什么变量类型