Program returns after I prompt the user for vehicle type, why?

Program returns after I prompt the user for vehicle type, why?

#include <iostream>

using namespace std;

int main()

{
int miles;
int vehicle;
char again = 'Y';

cout << "Welcome to the rental vehicle daily cost estimator!" << endl;

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

cout << "Which vehicle are you interested in? (enter E, S, L, V or T): " << endl;
cin >> vehicle;

cout << "How many miles do you estimate you'll drive? (1-812): " << endl;
cin >> miles;

if(vehicle != 'E', 'e', 'S', 's', 'L', 'l', 'V', 'v', 'T', 't')
{
cout << "Enter only E, S, L, V or T." << endl;
}

else if(miles > 1 && miles < 812)
cout << "Please enter a number between 1 and 812, inclusive" << endl;

switch(vehicle)
{
case 'E':'e';
if(miles >= 40)
{
cout << "Your charges are $45.90" << endl;
}

else if(miles < 40)
{
cout << "Your charges are $" << (miles - 40) * .25 + 45.90 << endl;
}
break;

case 'S':'s';
if(miles >= 120)
{
cout << "Your charges are $57.75" << endl;
}

else if(miles < 120)
{
cout << "Your charges are $" << (miles - 120) * .40 + 57.75 << endl;
}
break;

case 'L':'l';
if(miles >= 200)
{
cout << "Your charges are $85.50" << endl;
}

else if(miles < 200)
{
cout << "Your charges are $" << (miles - 200) * .35 + 85.50 << endl;
}
break;

case 'V':'v';
if(miles >= 150)
{
cout << "Your charges are $55.00" << endl;
}

else if(miles < 150)
{
cout << "Your charges are $" << (miles - 150) * .50 + 55.00 << endl;
}
break;

case 'T':'t';
if(miles >= 150)
{
cout << "Your charges are $55.00" << endl;
}

else if(miles < 150)
{
cout << "Your charges are $" << (miles - 150) * .50 + 55.00 << endl;
}
break;
}

cout << "Do you want to go again (y/n)?" << endl;
cin >> again;
}

cout << "Thank you for using this program. Goodbye." << endl;
}
Put the code you need help with here.
[/code]
Hello iiAlex,



PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.



Without testing the first thing I see is if(vehicle != 'E', 'e', 'S', 's', 'L', 'l', 'V', 'v', 'T', 't'). Using the comma operator it is most likely to actually look like if(vehicle != 't'). What you would need to do is if (vehicle != 'E' || vehicle != 'e' || ....

And your case statements should be:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
switch(vehicle)
{
    case 'E':
    case 'e':
        if(miles >= 40)
       {
            cout << "Your charges are $45.90" << endl;
       }
       break;
    case 'S':
    case 's':
        if(miles >= 120)
        {
            cout << "Your charges are $57.75" << endl;
        }
}

Try fixing those two things while I give what you have a test to see anything else I might find.

Hope that helps,

Andy
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
Thanks so much, why does it matter to have the "!" point, instead of using !=?

(!(vehicle == 'E' || vehicle == 'e' || vehicle == 'S' || vehicle == 's' || vehicle == 'L' || vehicle == 'l' || vehicle == 'V' || vehicle == 'v' || vehicle == 'T' || vehicle == 't'))
Last edited on
Hello iiAlex,

That line of code may work using "!=", but I was having problems getting it to work until I tried that which did work. I do agree that the "!=" should work and if used "&&" tends to be what is needed between the statements. I will get back to working on that part.

I started the while loop that follows the input of the "miles", but looked at something else and forgot to finish it. It should be:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
while (!std::cin || (miles < 1 || miles > 812))
{
	if (!std::cin)
	{
		std::cout << "\n    Invalid entry! Enter numbers only.\n";
		std::cin.clear();
		std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.
	}
	else if (miles < 1 || miles > 812)
	{
		std::cout << "\n    Invalid miles!\n";
	}

	std::cout << "\n How many miles do you estimate you'll drive? (1-812): ";
	std::cin >> miles;
}

Sorry about that.

Hope that helps,

Andy
Hello iiAlex,

The first time I tried this with an if statement
(vehicle != 'E' && vehicle != 'e' && vehicle != 'S' && vehicle != 's' && vehicle != 'L' && vehicle != 'l' && vehicle != 'V' && vehicle != 'v' && vehicle != 'T' && vehicle != 't') it did not work. I am not sure what I did wrong. I may have missed changing a "||" to "&&".

Both of the conditions will work and you are free to choose whichever works best for you.

Andy
Topic archived. No new replies allowed.