Hello iiAlex,
When testing the program the first problem I found is
int vehicle;
. When you get to the formatted input of
std::cin >> vehicle;
"cin" is expecting to put a number into this variable and when it can not "std::cin" is put in a failed state and is unusable the rest of the program until it is fixed. That is the main reason the program returns because you are not able to enter anything else using "std::cin".
So when you get to the if statement to check the vehicle type you are checking an "int" against a "char" and this does not work.
Since you are expecting a letter for "vehicle" define it as a "char".
Next is the if statement to verify miles. It is backwards. With what you wrote:
if(miles > 1 && miles < 812)
and say I enter 200, this would tell me that I entered an invalid number. As you will see shortly what you want here is
miles < 1 || miles > 812
.
The next thing I noticed is that you are verifying vehicle type to late. And even if you should print an error message you continue on with the program. The while loop I used will allow you to correct the problem before continuing with program.
Since "miles" is defined as an "int" the while loop checks both for a failed "std::cin" and the proper range.
Here is a possible solution for your program. This may be improved upon, but for now I just worked with what you have started with.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
|
#include <cctype> // <--- std::tolower() and std::toupper().
#include <iostream>
#include <iomanip>
#include <limits>
//using namespace std; // <--- Best not to use.
// A recent post that is worth reading. http://www.cplusplus.com/forum/beginner/258335/
int main()
{
int miles{};
char vehicle{}; // <--- Changed.
char again = 'Y';
std::cout << std::fixed << std::showpoint << std::setprecision(2);
std::cout << "\n Welcome to the rental vehicle daily cost estimator!" << std::endl;
//while (again == 'y' || again == 'Y')
while (std::toupper(again) == 'Y')
{
std::cout << "\n Which vehicle are you interested in? (enter E, S, L, V or T): ";
std::cin >> vehicle;
while (!(vehicle == 'E' || vehicle == 'e' || vehicle == 'S' || vehicle == 's' || vehicle == 'L' || vehicle == 'l' || vehicle == 'V' || vehicle == 'v' || vehicle == 'T' || vehicle == 't'))
{
std::cout << "\n Enter only E, S, L, V or T.\n" << std::endl;
std::cout << "\n Which vehicle are you interested in? (enter E, S, L, V or T): ";
std::cin >> vehicle;
}
std::cout << "\n How many miles do you estimate you'll drive? (1-812): ";
std::cin >> miles;
while (!std::cin || (miles < 1 || miles > 812))
{
std::cout << "\n Please enter a number between 1 and 812, inclusive.\n";
std::cout << "\n How many miles do you estimate you'll drive? (1-812): ";
std::cin >> miles;
}
switch (vehicle)
{
case 'E':
case 'e':
if (miles >= 40)
{
std::cout << "\n Your charges are $45.90" << std::endl;
}
else if (miles < 40)
{
std::cout << "Your charges are $" << (miles - 40) * .25 + 45.90 << std::endl;
}
break;
case 'S':
case 's':
if (miles >= 120)
{
std::cout << "\n Your charges are $57.75" << std::endl;
}
else if (miles < 120)
{
std::cout << "\n Your charges are $" << (miles - 120) * .40 + 57.75 << std::endl;
}
break;
case 'L':
case 'l':
if (miles >= 200)
{
std::cout << "\n Your charges are $85.50" << std::endl;
}
else if (miles < 200)
{
std::cout << "\n Your charges are $" << (miles - 200) * .35 + 85.50 << std::endl;
}
break;
case 'V':
case 'v':
if (miles >= 150)
{
std::cout << "\n Your charges are $55.00" << std::endl;
}
else if (miles < 150)
{
std::cout << "\n Your charges are $" << (miles - 150) * .50 + 55.00 << std::endl;
}
break;
case 'T':
case 't':
if (miles >= 150)
{
std::cout << "\n Your charges are $55.00" << std::endl;
}
else if (miles < 150)
{
std::cout << "\n Your charges are $" << (miles - 150) * .50 + 55.00 << std::endl;
}
break;
default:
std::cout << "\n Invalid entry!\n";
} // End switch.
std::cout << "\n\n Do you want to go again (y/n)? ";
std::cin >> again;
} // End while.
std::cout << "\n\n Thank you for using this program. Goodbye." << std::endl;
// The next line may not be needed. If you have to press enter to see the prompt it is not needed.
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
std::cout << "\n\n Press Enter to continue: ";
std::cin.get();
return 0;
}
|
If yo look close yo will see that I have changed the prompts and error messages. I think it works better when the output does not all run together line aftre line.
Hope that helps,
Andy