#I need help help on this assignment desperately

16 messages · Page 1 of 1 (latest)

shrewd summit
#

CS 002 - Problem Solving and Program Design Using C++

Lab 7: Intro to Functions - Liters and MPG

Before tackling this lab, you should have completed:
Savitch Chapter 4 , and corresponding exercises

Collaboration policy:
Collaboration on these lab exercises is strongly ENCOURAGED.

Lab Objectives
understand the concepts of return type, parameter list, and argument list
be able to declare, define, and invoke basic functions

A liter is 0.264179 gallons. Write a program that will read in the number of liters of gasoline consumed by the user’s car and the number of miles traveled by the car and will then output the number of miles per gallon the car delivered. Your program should allow the user to repeat this calculation as often as the user wishes. Define a function (call the function mpgCalculator) to compute the number of miles per gallon. Your program should use a globally defined constant for the number of liters per gallon.

Next, modify your program so that it will take input data for two cars and output the number of miles per gallon delivered by each car. Your program will also announce which car has the best fuel efficiency (highest number of miles per gallon).

Note:
All of your cout statements should be in main, NOT inside the functions. You will receive a zero if there are any cout statements inside your function.
There should only be ONE global variable (the liter to gallon conversion constant). Global variables are defined prior to main.
You need to figure out what the return type is and how many arguments need to be passed to the function.
Make sure your program includes both a function prototype and a function definition.

fluid moonBOT
#

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.

shrewd summit
#

Sample Output:
Enter the number of liters of gas consumed: 18
Enter the number of miles traveled: 100
This car gets 21.0295 miles per gallon.
Calculate again? n

Enter the number of liters consumed for the first car: 18
Enter the number of miles traveled for the first car: 100
Enter the number of liters consumed for the second car: 20
Enter the number of miles traveled for the second car: 120

The first car gets 21.0295 miles per gallon.
The second car gets 22.7119 miles per gallon.
The second car gets better gas mileage.

north glacier
#

!hw

fluid moonBOT
# north glacier !hw
Zelis, Herald of Krill Issues
We Don't Do Your Homework

Welcome to Together C & C++ :wave:

We won't do your homework for you (#rules) but we are happy to help you learn and point you in the right direction.

Please send what you have so far and ask a specific question.

shrewd summit
#

currently i have this but i still get it right and its so annoying

#

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

const double LITERS_PER_GALLON = 0.264179;

double mpgCalculator(double liters, double miles);

int main() {
char repeat;
do {
int choice;
cout << "Choose calculation mode:\n";
cout << "1. Single car MPG calculation\n";
cout << "2. Compare two cars\n";
cout << "Enter your choice (1 or 2): ";
cin >> choice;

    if (choice == 1) {
        double liters, miles;
        cout << "Enter the number of liters of gas consumed: ";
        cin >> liters;
        cout << "Enter the number of miles traveled: ";
        cin >> miles;

        double mpg = mpgCalculator(liters, miles);

        cout << fixed << setprecision(4);
        cout << "This car gets " << mpg << " miles per gallon." << endl;

    } else if (choice == 2) {
        double liters1, miles1, liters2, miles2;

        cout << "Enter the number of liters consumed for the first car: ";
        cin >> liters1;
        cout << "Enter the number of miles traveled for the first car: ";
        cin >> miles1;

        cout << "Enter the number of liters consumed for the second car: ";
        cin >> liters2;
        cout << "Enter the number of miles traveled for the second car: ";
        cin >> miles2;

        double mpg1 = mpgCalculator(liters1, miles1);
        double mpg2 = mpgCalculator(liters2, miles2);
viscid flower
#

!format

fluid moonBOT
#

cout << fixed << setprecision(4);
cout << "The first car gets " << mpg1 << " miles per gallon." << endl;
cout << "The second car gets " << mpg2 << " miles per gallon." << endl;

        ```cpp

if (mpg1 > mpg2) {
cout << "The first car has better gas mileage." << endl;
} else if (mpg2 > mpg1) {
cout << "The second car has better gas mileage." << endl;
} else {
cout << "Both cars have the same gas mileage." << endl;
}
}
else {
cout << "Invalid choice! Please enter 1 or 2." << endl;
}

cout << "Calculate again? (y/n): ";
cin >> repeat;
}
while (repeat == 'y' || repeat == 'Y')
;

return 0;
}

double mpgCalculator(double liters, double miles) {
if (liters == 0) {
cout << "Error: Liters consumed cannot be zero!" << endl;
return 0;
}
double gallons = liters * LITERS_PER_GALLON;
return miles / gallons;
}

Prince💉💉💉💉
#
How to Format Code on Discord
Markup

```cpp
int main() {}
```

Result
int main() {}
viscid flower
#

@shrewd summit what is your question

shrewd summit
#

what am i doing wrong

viscid flower
#

idk

shrewd summit
#

i just cant get the code right

marble lynx
stable jackal
#

does it give you an error? is there any test thing on your submission platform? The code looks ok, but without knowing what is happening on your side it's hard to give any useful input