1

I am running into a problem and i cant seem to figure out what i am doing wrong. I have to write this program and for some reason it's not letting me compare a char variable to a constant char variable in the loop and it's not letting me % two double variables? can anyone help?

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

2 Answers2

2
choice != "Q "

you are comparing char with a string, so type mismatch, you have to do the following:

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

% only works with integers since the normal mathematical notion of "remainder" is only applicable to integer division. i.e. division that required to generate integer quotient. So you cannot apply % on double.

taocp
  • 23,276
  • 10
  • 49
  • 62
0

There is one more logical error about the while condition. Your initial line was
while (choice != "Q "|| choice != "q") which corrected according to tacp looks like
while (choice != 'Q' || choice != 'q').

This remains logically incorrect:

  • assuming you want to know when the user entered 'q' OR 'Q' so you would end the loop. the following expression is true if any condition is met

    choice == 'Q' || choice == 'q'
    
  • but that not good because the while loop goes on as long as the expression is true. you need it going as long as the user DIDN'T quit. so you negate the initial expression

    !(choice == 'Q' || choice == 'q')
    
  • with a bit of boolean logic you get to your correct version (note the &&):

    choice != 'Q' && choice != 'q'
    
dmind
  • 815
  • 1
  • 6
  • 9