Hello miguelminaj,
I have been going over your latest code and I do have some suggestions.
To start with you have written this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <cctype>
#include <cstdlib>
using namespace std;
int main()
{
constexpr int WIDTH{ 13 };
| |
The blank lines are good, but to many of them does not help. Consider this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
#include <iostream>
#include <iomanip>
//#include <limits> // <--- Not needed yet, but will have a futute use.
#include <string>
#include <vector>
#include <cctype>
#include <cstdlib>
using namespace std;
int main()
{
constexpr int WIDTH{ 13 };
char playAgain{};
double milesPerYear{}, priceOfGas{};
| |
I find this much easier to read. The first code makes me think that something is missing or should be added.
I realize that error checking may not have been covered yet, but there is no time like the present to learn.
1 2 3 4 5 6 7 8 9 10
|
const vector<double> mpg{ 0.0, 21.0, 28.0, 22.0, 19.0, 16.0 };
if (vehicles.size() != mpg.size())
{
std::cerr << "\n Vehicles contain : " << vehicles.size() << " items and mpgs contain: " << mpg.size() << " items.\n";
std::cerr << "\n Vehicle names do not match the mpgs!\n\n";
return 1;
}
| |
If you do not understand this let me know.
I will just mention this for now. The "cout" statement that prints the menu is fine and I just revised what you originally started with to show you a better way, but in the end a for loop would work better than all those individual lines. I will show you later.
The program is good down to:
1 2 3 4 5 6 7
|
//Calculations for vehicle 1
//Gallons of fuel for vehicle one
float gallonsPerYear;
gallonsPerYear = milesPerYear / mpg.at(vec1Choice - 1);
cout << "Gallons per year for vehicle 1 is: " << gallonsPerYear << endl;
| |
The problem I see here is that both variables on the rhs of = are defined as "double"s, so after the calculation you are trying to but a larger "double" into a smaller "float". There will be data loss and I have seen where the last decimal number of a 'float" be rounded up B4 it chops of the extra digits.
Running all those lines together makes it hard to see the variable definition.
Unless you have a good reason the preferred floating point type is a "double". It is best to make all your variables match. Defining your variables as "float"s should produce a warning of possible data loss. This will not keep the program from running, but the result may not be what you expected.
What you have done after this point appears to be correct, although I have not completely checked the calculations.
The last line of the do/while loop is:
cout << "\n\n Goodbye!\n\n";
. I changed that a little. See what you think, but this line should be after the do/while loop just B4 the program ends.
Now here is a little trick you can use to make the code easier to write and use.
1 2 3 4 5 6 7 8 9 10 11 12 13
|
//Vectors with vehicle names
const vector<string> vehicles
{
"",
"Ford Fusion",
"Honda Fit",
"Jeep Renegade",
"Chrysler 300",
"Ram"
};
//Vectors with mpg
const vector<double> mpg{ 0.0, 21.0, 28.0, 22.0, 19.0, 16.0 };
| |
Notice line 4 in "vehicles" This is just a place holder to give element (0) something. And in the "mps" vector I used (0.0).
Now you can change the first "cout" in the do/while loop to:
1 2 3 4 5 6 7 8
|
std::cout << std::left << '\n';
for (size_t idx = 1; idx < mpg.size(); idx++)
{
std::cout << ' ' << idx << ". Is a " << std::setw(WIDTH) << vehicles[idx] << " with an mpg of " << mpg[idx] << '\n';
}
std::cout << '\n';
| |
Now it will not matter how many vehicles are in the vector it will print all of them and the same for the mpg vector.
The "\n" at the end of line 1 is optional, but I think it makes for a nicer output. Line 8 is also optional.
You will notice that "idx" starts at (1) and not (0)zero. Since element (0) is unused everything comes out correctly. It does not matter if you use "vehicles" or "mpg" with the .size function because by the time you get here it would have been checked and they will be the same size.
Later in your code:
1 2 3 4 5 6 7 8 9 10
|
//Ask the user to “Select vehicle one : ” and “Select vehicle two : ”
std::cout << "Please enter your first vehicle choice. (1-5): ";
std::cin >> vec1Choice;
std::cout << "Please enter your second vehicle choice. (1-5): ";
std::cin >> vec2Choice;
//Show the user their vehicle choices
std::cout << "\nVehicle 1 is: " << vehicles.at(vec1Choice) << '\n';
std::cout << "Vehicle 2 is: " << vehicles.at(vec2Choice) << "\n\n";
| |
Notice lines 9 and 10 do not need the (- 1) to correctly access the vector because you are not using element (0) of the 2 vectors.
This is what your program output B4 I made some changes:
Vehicle 1 is a Ford Fusion with an mpg of 21.00
Vehicle 2 is a Honda Fit with an mpg of 28.00
Vehicle 3 is a Jeep Renegade with an mpg of 22.00
Vehicle 4 is a Chrysler 300 with an mpg of 19.00
Vehicle 5 is a Ram with an mpg of 16.00
Please enter your first vehicle choice. (1-5): 2
Please enter your second vehicle choice. (1-5): 4
Vehicle 1 is: Honda Fit
Vehicle 2 is: Chrysler 300
How much miles do you anticipate on driving per year?: 15000
What's the price of gas?: 2.55
Gallons per year for vehicle 1 is: 535.71
Cost of yearly fuel for vehicle 1 is: $1366.07
Gallons per year for vehicle 2 is: 789.47
Cost of yearly fuel for vehicle 2 is: $2013.16
Difference in cost of both vehicles is: $647.09
Try another combination (Y/N): y
Goodbye!
Vehicle 1 is a Ford Fusion with an mpg of 21.00
Vehicle 2 is a Honda Fit with an mpg of 28.00
Vehicle 3 is a Jeep Renegade with an mpg of 22.00
Vehicle 4 is a Chrysler 300 with an mpg of 19.00
Vehicle 5 is a Ram with an mpg of 16.00
Please enter your first vehicle choice. (1-5):
|
Notice that the "Goodbye!" displays even if you choose "y". Not what you want.
Now consider this for your output:
1. Is a Ford Fusion with an mpg of 21.00
2. Is a Honda Fit with an mpg of 28.00
3. Is a Jeep Renegade with an mpg of 22.00
4. Is a Chrysler 300 with an mpg of 19.00
5. Is a Ram with an mpg of 16.00
Please enter your first vehicle choice. (1-5): 2
Please enter your second vehicle choice. (1-5): 5
Vehicle 1 is: Honda Fit
Vehicle 2 is: Ram
How much miles do you anticipate on driving per year?: 15000
What's the price of gas?: 2.55
Gallons per year for vehicle 1 is: 714.29
Cost of yearly fuel for vehicle 1 is: $1821.43
Gallons per year for vehicle 2 is: 789.47
Cost of yearly fuel for vehicle 2 is: $2013.16
Difference in cost of both vehicles is: $191.73
Try another combination (Y/N): y
1. Is a Ford Fusion with an mpg of 21.00
2. Is a Honda Fit with an mpg of 28.00
3. Is a Jeep Renegade with an mpg of 22.00
4. Is a Chrysler 300 with an mpg of 19.00
5. Is a Ram with an mpg of 16.00
Please enter your first vehicle choice. (1-5):
|
A few well placed "\n"s will make a big difference. And I know that you are probably just happy to get output that is correct.
Andy