1

我遇到了一个问题,我似乎无法弄清楚我做错了什么。我必须编写这个程序,由于某种原因,它不允许我将 char 变量与循环中的常量 char 变量进行比较,也不允许我使用 % 两个双变量?谁能帮忙?

    // 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;
}
4

2 回答 2

2
choice != "Q "

您正在char与 a进行比较string,因此类型不匹配,您必须执行以下操作:

choice != 'Q' //this is char literal

%仅适用于整数,因为“余数”的正常数学概念仅适用于整数除法。即生成整数商所需的除法。所以不能申请%double

于 2013-04-09T23:58:59.507 回答
0

关于 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'
    
于 2013-04-10T00:25:58.717 回答