#Loops

18 messages · Page 1 of 1 (latest)

safe timber
#

#include <iostream>
#include <string>
using namespace std;

int main() {
char ans = 'Y';

while (ans == 'Y' || ans == 'y') {

    std::cout << "\n";
    std::cout << "[Enter game Information as followed]\n";
    std::cout << "------------------------------------\n";

    std::cout << "Duration of the game in minutes (between 40 and 90 and intervals of 10): ";

    int game_duration = 0;

    while (true) {
        cin >> game_duration;

        if (game_duration >= 40 && game_duration <= 90 && game_duration % 10 == 0) {
            break;
        }
        else {
            cout << "\nERROR: Your input for duration of the game is NOT valid. Please enter again. \n";
        }
        cout << " \nDuration of the game in minutes (between 40 and 90 and intervals of 10): ";
    }


    std::cout << "\n";

    double price_per_min = 0;
    double total_dollars = 0;
    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(2);

    std::cout << "Enter Price per minute, choose between ($1.50 and $5.00): ";

    while (true) {
        cin >> price_per_min;

        if (price_per_min >= 1.50 && price_per_min <= 5.00) {
            break;
        }
        else {
            cout << "\nInvalid input! Please enter a price between $1.50 and $5.00.\n";
        }
        cout << "Enter Price per minute, choose between ($1.50 and $5.00): ";
    }

    
    std::cout << "\n\nReferee Payment Breakdown...\n\n";

    std::cout << "Game Duration: " << game_duration << " Minutes.\n";

    std::cout << "Price per minute: $" << price_per_min << "\n";

    double round_payment;
    double payment = 0;
    payment = (game_duration * price_per_min);
    round_payment = static_cast<int>(payment + 0.5);
    cout << "Total Payment: $" << round_payment << "\n";

  

    int center_payment = (round_payment * 0.40); 
    int assistant_payment = (round_payment * 0.30);

    int total_distributed = center_payment + 2 * assistant_payment;
    int remaining = round_payment - total_distributed;

    
    if (remaining >= 1) {
        center_payment += 1; 
        remaining -= 1;
    }
    if (remaining >= 2) {
        assistant_payment += 1; 
        assistant_payment += 1;
    }

    cout << "Center Ref's Payment: $" << center_payment << "\n";

    cout << "Assistant Red's Payment: $" << assistant_payment << "\n";


    std::cout << "\n";

    cout << "Do Another Calculation?(Y/N): ";

    cin >> ans;

}
cout << "Thank you for using the program!" << endl;


    return 0;

}

grave oracleBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

safe timber
#

I have a question, so for my loop

#

it works but if I insert a letter instead of number it crashes

#

if (game_duration >= 40 && game_duration <= 90 && game_duration % 10 == 0) {
break;
}
else {
cout << "\nERROR: Your input for duration of the game is NOT valid. Please enter again. \n";
}

#

is there a way to make it so if the user accidently inserts a letter it doesn't crash and start pasting like crazy?

austere badge
#

you have to handle the input error

#

when you ask for a number and the user types in something that can't be a number, the stream goes into an error state and doesn't do any more IO until you .clear() the error

#

that's why it then loops forever without waiting for new input

#

you can do like:

if (cin >> price_per_min) {
    // input ok, do stuff with price_per_min
} else {
    // input failed :(
}
#

and use clear to reset the error so you can try again, and ignore to throw away the line of input, so you don't try reading the same bad input again

grave oracleBOT
#

@safe timber Has your question been resolved? If so, type !solved :)

safe timber
#

!solved +

grave oracleBOT
safe timber
#

!solved