Am I using "default" correctly in switch statements?

I am doing some practicing outside of class, and am teaching myself how to properly use "if then" statements, and now I am moving on to switch statements by using the beginner exercises on this site. Here is my code below:

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
/* Write a program that presents the user w/ a choice of your 5 favorite beverages (Coke, Water, Sprite, ... , Whatever).
Then allow the user to choose a beverage by entering a number 1 - 5.
Output which beverage they chose.
a) If you program uses if statements instead of a switch statement, modify it to use a switch statement.
If instead your program uses a switch statement, modify it to use if/else-if statements.

b) Modify the program so that if the user enters a choice other than 1-5 then it will output "Error. choice was not valid, here is your money back."
*/

#include <iostream>
using namespace std;

int main()
{
	int soda;
	
	cout << "Welcome to the computerized vending machine!\n";
	cout << "We have 5 different types of beverages.\n";
	cout << "| (Coke)  _  (Dr Pepper)  _  (Sprite)  _  (Water)  _  (Diet Coke) |\n";
	cout << "|___1_____________2______________3___________4_____________5______|\n";
	cout << "Enter a number, 1-5, in order to dispense your drink.\n";

	cin >> soda;

	switch (soda)
	{
	case 1: 
		cout << "Your coke has been dispensed. Enjoy!";
		break;
	case 2:
		cout << "Your dr pepper has been dispensed. Enjoy!";
		break;
	case 3:
		cout << "Your sprite has been dispensed. Enjoy!";
		break;
	case 4:
		cout << "Your water has been dispensed. Enjoy!";
		break;
	case 5:
		cout << "Your diet coke has been dispensed. Enjoy!";
		break;
	default:
		cout << "Error, choice was not valid. Here is your money back.";
		break;
	}
	return 0;
}


It correctly outputs the soda that is correlated with each number, but the issue is that whenever I try putting in an invalid input- like 6 or the letter "A", it just returns to a value of 0 and doesn't output an error like I want it to.

Any help would be appreciated.
Try cleaning your project or making a new project and pasting the code in. I think your code looks good enough for what you describe.

Enter a number, 1-5, in order to dispense your drink.
0
Error, choice was not valid. Here is your money back. 


Enter a number, 1-5, in order to dispense your drink.
A
Error, choice was not valid. Here is your money back. 

Note that when you type "A" that actually puts the input stream in a failed state, and the value of soda is filled with 0 (as of C++11).
Before C++11, the value of soda would not be changed if the input failed. So just in case, I would initialize your soda variable to 0. int soda = 0;
https://en.cppreference.com/w/cpp/io/basic_istream/operator_gtgt
Last edited on
You messed up big time, std::cin can fail especially because it is an int, strings are more lenient, but any errors on std::cin will still silently bleed into the next call, and ^Z can force an error.

Streams are really awful at explaining what you did wrong, std::stoi (exception) and std::from_chars (c++17, C-ish style error) are superior for error info, but for for simple terminal code it is OK to use streams and say a vague error hold enter and exit (or try to unset the error manually, ugly code sadly), getting a meaningful error is more useful in GUI code, since every prompt result is a string, unless you have a convenient API.

And reading complex data from a file using streams is a lost cause. Just reading a list of space separated numbers is fine, but getline on a txt file on windows, god no, only hacks can save you. Off topic, but a JSON 3rd party API at least tells you where the error occurs, and if it is a conversion error or not (they should, can't say for all API's).

You can error check by doing if(stream >> X) or if(stream) or if(getline(stream, X)), remember that all 3 of these examples return &(o/i)stream which overloads operator bool which returns stream.good() that includes all errors including EOF, but if you expect to read to EOF (reading std::ifstream), only treat it as an error if stream.fail() is true (there is also bad(), but don't use it since fail() == fail() || bad()), and remember to never mix std::cin << ... with getline, and always use an enum with switch statements over nothing (as I like to say "magic numbers").

Also you forgot newlines, if you tried your program in a terminal / cmd.exe you will see the result. The problem is that printf/std::cout require to flush to be seen, and by default on 99% of all OS's the newline will automatically flush.
Last edited on
I am really beginning to dislike poteto's regular language and mis-advice here on the forum.

There is no error in OP's program.

It behaves as designed: errors and all.

Ganado gave a well-reasoned response, enumerating relevant things to watch out for in the future, all without the alarmism or side issues and, most importantly, without the pessimism and incorrect advice.
@Ganado thanks for reference link, I'm already fixing my code :)
Huh, I thought side issues are important (/shade_off).

But in seriousness, I never really thought about any of my recent posts being mis-advicefull in any way, if you want to have a discussion about it, shoot me a PM.
Thanks Ganado! I copied and pasted the code into a new project file and it ran successfully after that.
Topic archived. No new replies allowed.