Help with a program

This is a discussion on Help with a program within the C++ Programming forums, part of the General Programming Boards category; My teacher is asking me "Prompt the user if they'd like to do another cost of commute calculation. If they ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    2

    Help with a program

    My teacher is asking me "Prompt the user if they'd like to do another cost of commute calculation. If they enter 'Y' or 'y', then repeat; otherwise, print the following and exit the program. "

    Code:
    #include <iostream>
    using namespace std;
    
    int main(int, char**) {
        int milesPerDay;
        int daysPerWeek;
        int weeksPerYear;
        int milesPerGallon;
        double pricePerGallon;
        
            
        cout << "How many miles round-trip a day do you commute?";
        cin >> milesPerDay;
        cout << "How many days a week do you commute?";
        cin >> daysPerWeek;
        cout << "How many weeks a year do you commute?";
        cin >> weeksPerYear;
        cout << "How many miles per gallon does your vehicle get?";
        cin >> milesPerGallon;
        cout << "What is the current per gallon price of gas?";
        cin  >> pricePerGallon;
        
        int milesPerYear = milesPerDay * daysPerWeek * weeksPerYear;
        int gallonsPerYear = milesPerYear / milesPerGallon;
        double gasMoneyPerYear = gallonsPerYear * pricePerGallon;
        double monthlyGasExpense = gasMoneyPerYear / 12;
        cout << milesPerDay << " " << "miles a day" << endl;
        cout << daysPerWeek << " " << "days a week" << endl;
        cout << weeksPerYear << " " << "weeks a year" << endl;
        cout << milesPerGallon << " " << "miles per gallon" << endl;
        cout << "$" << pricePerGallon << " " << "per gallon" << endl;
        cout << "monthly gas expense:" << "  " << monthlyGasExpense << endl;
        system("pause");
        
        char loop = 'Y' || 'y';
        cout << "Would you like to do another cost of commute calculation?"
             << "Enter y for yes enter n for no" << endl;
        cin >> loop;
        if (loop = 'Y' || 'y') {
            
        
    }
    What do I do in order to get the program to loop?

  2. #2
    Registered User
    Join Date
    Jan 2010
    Posts
    34
    Code:
    #include <iostream>
    using namespace std;
    
    int main(int, char**) {
        int milesPerDay;
        int daysPerWeek;
        int weeksPerYear;
        int milesPerGallon;
        double pricePerGallon;
        char loop = 'Y' ;
        
        do{
        
        cout << "How many miles round-trip a day do you commute?";
        cin >> milesPerDay;
        cout << "How many days a week do you commute?";
        cin >> daysPerWeek;
        cout << "How many weeks a year do you commute?";
        cin >> weeksPerYear;
        cout << "How many miles per gallon does your vehicle get?";
        cin >> milesPerGallon;
        cout << "What is the current per gallon price of gas?";
        cin  >> pricePerGallon;
        
        int milesPerYear = milesPerDay * daysPerWeek * weeksPerYear;
        int gallonsPerYear = milesPerYear / milesPerGallon;
        double gasMoneyPerYear = gallonsPerYear * pricePerGallon;
        double monthlyGasExpense = gasMoneyPerYear / 12;
        cout << milesPerDay << " " << "miles a day" << endl;
        cout << daysPerWeek << " " << "days a week" << endl;
        cout << weeksPerYear << " " << "weeks a year" << endl;
        cout << milesPerGallon << " " << "miles per gallon" << endl;
        cout << "$" << pricePerGallon << " " << "per gallon" << endl;
        cout << "monthly gas expense:" << "  " << monthlyGasExpense << endl;
        system("pause");
        
        cout << "Would you like to do another cost of commute calculation?"
             << "Enter y for yes enter n for no" << endl;
        cin >> loop;
        }while (loop=='Y' || loop=='y');
            
        
    }

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    2

    lol

    Thanks, I thought it might be that but it seemed wrong wasn't thinking ><

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 12:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 12:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 09:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21