0

My code only outputs 0.00 when 'y' is entered. Clearly, I'm a beginner.

#include <iostream>
#include <iomanip> 
#include <string>

const double ADULTFEE = 18.00, CHILDFEE = 7.50, TRAINFEE = 6.00, BIRDFEE = 5.00;

using namespace std;

int main ()
{
    char reply = ' ';
    double adultNum = 0, childNum = 0, guestAdult = 0, guestChild = 0, trainNum = 0, birdNum = 0;
    double totalFee = 0;

    cout << "Welcome to Jake's Fee Calculator" << endl; 
    cout << "How many adults (age 14 or over) are in your party? ";
    cin >> adultNum;
    cout << "How many children (age 5 to 13) are in your party? ";
    cin >> childNum;
    cout << "Do you have a family membership? (y/n) ";
    cin >> reply;
    if (tolower(reply) == 'y')
    {
        cout << "How many adults in your party are guests? ";
        cin >> guestAdult;
        cout << "How many children (age 5 to 13) in your party are guests? ";
        cin >> guestChild;
        cout << "How many train tickets would you like? ";
        cin >> trainNum;
        cout << "How many bird show tickets would you like? ";
        cin >> birdNum;
    }
    else
    {
        cout << "How many train tickets would you like? ";
        cin >> trainNum;
        cout << "How many bird show tickets would you like? ";
        cin >> birdNum;
    }
    if (reply == 'y' && guestAdult > 2)
        totalFee = ((guestAdult - 2) * ADULTFEE) + (guestChild * CHILDFEE) + (((trainNum * TRAINFEE) + (birdNum * BIRDFEE))/2);

    else if (reply == 'y' && guestAdult == 2)
        totalFee = (guestChild * CHILDFEE) + (((trainNum * TRAINFEE) + (birdNum * BIRDFEE))/2);

    else if (reply == 'y' && (guestAdult == 1 && guestChild == 0))
        totalFee = (((trainNum * TRAINFEE) + (birdNum * BIRDFEE))/2);

    else if (reply == 'y' && (guestAdult == 1 && guestChild == 1))
        totalFee = (((trainNum * TRAINFEE) + (birdNum * BIRDFEE))/2);

    else if (reply == 'y' && (guestAdult == 0 && guestChild == 2))
        totalFee = (((trainNum * TRAINFEE) + (birdNum * BIRDFEE))/2);

    else if (reply == 'y' && (guestAdult == 0 && guestChild > 2))
        totalFee = ((guestChild - 2) * CHILDFEE) + (((trainNum * TRAINFEE) + (birdNum * BIRDFEE))/2);

    else if (reply == 'y' && (guestAdult == 0 && guestChild == 1))
        totalFee = (((trainNum * TRAINFEE) + (birdNum * BIRDFEE))/2);

    else if (reply == 'n')
        totalFee = (adultNum * ADULTFEE) + (childNum * CHILDFEE) + (trainNum * TRAINFEE) + (birdNum * BIRDFEE);

    cout << fixed << showpoint;
    cout << setprecision(2);
    cout << "Your total fee is $" << totalFee << endl;
    cout << "Thank you for using Jakes's Fee Calculator" << endl;

    system("pause");
    }

Edited by Tony.Z

4
Contributors
3
Replies
26
Views
4 Years
Discussion Span
Last Post by RonalBertogi
0

There are many opportunities in your code to enter the value 'y'; please tell us the complete input you typed in, what the output was, and what you expected the output to be.

0

First, have a cleanup of your code. You may (must) replace your code from line 40 up to line 62 with the code below. That way, it could be cleaner to read (and debug) your code and it might help us—including yourself—solve your problem.

if (reply == 'y')
{
    switch (guestAdult)
    {
        case 0:
            if (guestChild == 1)
                totalFee = (((trainNum * TRAINFEE) + (birdNum * BIRDFEE))/2);
            else if (guestChild == 2)
                totalFee = (((trainNum * TRAINFEE) + (birdNum * BIRDFEE))/2);
            else if (guestChild > 2)
                totalFee = ((guestChild - 2) * CHILDFEE) + (((trainNum * TRAINFEE) + (birdNum * BIRDFEE))/2);
            break;
        case 1:
            if (guestChild == 0)
                totalFee = (((trainNum * TRAINFEE) + (birdNum * BIRDFEE))/2);
            else if (guestChild == 1)
                totalFee = (((trainNum * TRAINFEE) + (birdNum * BIRDFEE))/2);
            break;
        case 2:
            totalFee = (guestChild * CHILDFEE) + (((trainNum * TRAINFEE) + (birdNum * BIRDFEE))/2);
            break;
        default:
            if( guestAdult > 2)
                totalFee = ((guestAdult - 2) * ADULTFEE) + (guestChild * CHILDFEE) + (((trainNum * TRAINFEE) + (birdNum * BIRDFEE))/2);
    }
}
else
    totalFee = (adultNum * ADULTFEE) + (childNum * CHILDFEE) + (trainNum * TRAINFEE) + (birdNum * BIRDFEE);
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.