以双精度数除以双精度数来获得循环中的余数和char参数不起作用
dividing a double by a double to get the remainder and char arguments in loop not working
我遇到了一个问题,我似乎不知道我做错了什么。我必须写这个程序,由于某些原因它不允许我在循环中比较一个字符变量和一个常量字符变量它不允许我%两个双变量?有人能帮忙吗?
// Nathan Brown
// Nathan Owen Brown's Space Travle Company
// CSCI 1010 PASS9
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
void mostPowerful (double &power1, double &power2, double &power3)
{
if (power1 > power2 && power1 > power3)
cout << "The largest power output is " << power1 << " and is Jetpack number 1" << endl;
else if (power2 > power1 && power2 > power3)
cout << "The largest power output is " << power2 << " and is Jetpack number 2" << endl;
else if (power3 > power1 && power3 > power2)
cout << "The largest power output is " << power3 << " and is Jetpack number 3" << endl;
}
void discountResults (double &price, double &disAmount)
{
disAmount = price * disAmount;
price = price - disAmount;
}
void howMany (double &moneyAvail, double &cost)
{
int howMany;
double leftOver;
howMany = moneyAvail / cost;
leftOver = moneyAvail % cost;
moneyAvail = howMany;
cost = leftOver;
}
char menu ()
{
char choice;
cout << "Welcom to Nathan Owen Brown's Space Travel Company" << endl;
cout << "(M)ost Powerful Calculation" << endl;
cout << "(D)iscount Calculation" << endl;
cout << "(H)ow Many Calculation" << endl;
cout << "(Q)uit" << endl;
cout << endl;
cout << endl;
cout << "Please enter the option (M, D, H, or Q) ";
cin >> choice;
return choice;
}
int main ()
{
double power1, power2, power3, price, disAmount, moneyAvail, cost;
char choice;
menu ();
while (choice != "Q "|| choice != "q")
{
if (choice == "M" || choice == "m" )
{
cout << "Please enter 3 power output measurements in MW: " << endl;
cin >> power1 >> power2 >> power3;
mostPowerful (power1, power2, power3);
}
else if (choice == "D" || choice == "d")
{
cout << "Please enter a price and a discount amount: " << endl;
cin >> price >> disAmount;
discountResults (price, disAmount);
cout << "The discount amount is " << disAmount << " and the dicounted price is " << price << "." << endl;
}
else if (choice == "H" || choice == "h")
{
cout << "Please enter amount available and cost of each: " << endl;
cin >> moneyAvail >> cost;
howMany (moneyAvail, cost);
cout << "You can buy " << moneyAvail << " and have " << cost << " left over." << endl;
}
menu ();
}
system ("PAUSE");
return 0;
}
choice != "Q "
您正在比较char
与string
,因此类型不匹配,您必须执行以下操作:
choice != 'Q' //this is char literal
%
只适用于整数,因为一般的数学概念"余数"只适用于整数除法。即需要生成整数商的除法。所以你不能在double
上应用%
还有一个关于while条件的逻辑错误。您的初始行是while (choice != "Q "|| choice != "q")
根据tacp修正后看起来像while (choice != 'Q' || choice != 'q')
。
这在逻辑上仍然是不正确的:
-
假设你想知道用户何时输入'q'或'q',这样你就可以结束循环。如果满足任何条件,以下表达式为真
choice == 'Q' || choice == 'q'
-
但这并不好,因为只要表达式为真,while循环就会继续。只要用户没有退出,你就需要它继续运行。你否定了初始表达式
!(choice == 'Q' || choice == 'q')
-
用一点布尔逻辑你得到你的正确版本(注意&&):
choice != 'Q' && choice != 'q'
相关文章:
- 如何防止 c++ 在从浮点型转换为双精度型(不适用于 IO)时添加额外的小数?
- 正在将csv文件读取为双精度矢量
- 我可以信任表示整数的浮点或双精度来保持精度吗
- 如何在C++中的同一函数中使用字符串和双精度
- 特征::矩阵<双精度,1,3> 结构类型函数中的返回类型函数
- 检查是否以特定精度给出双精度
- 转换函数,将 std::数组的双精度作为参数或双精度作为参数单独转换
- C 字符串返回字符串的整数/双精度/长整型值
- 为什么将双精度转换为 int 似乎在第 16 位数字之后将其四舍五入?
- 如何使双精度值的 C++ 和 C# 中的结果相同
- 使用浮点数和双精度数的非常小数字的数学
- 使用 Xcode 将双精度存储在数组C++中
- 在 C++ 中将双精度变量写入二进制文件
- 如何从字符串转换为双精度*
- 为什么我的数组双精度函数不起作用?
- 高精度双精度的 Sprintf 格式化问题
- C++ cout 将双精度对齐到精度 2 并正确对齐
- 将指针数组分配给双精度
- 为什么在C和C++中没有浮点/双精度的除法余数运算
- 以双精度数除以双精度数来获得循环中的余数和char参数不起作用