Hi, I am somewhat new to C++. I am testing switch statements to better understand how they work. I created a simple program, but my program is not functioning the way it should. I don't have any compiling errors, but the program exits right away. I'm not sure if I'm using the switch statements correctly:
int option = 0;
switch(option) {
case = '1':
Input(); //this is part of a class
case = '2':
Display(); //this is part of a class
case = '0':
cout <<"Good Bye" << endl;
break;
}
I want the program to display the functions when the user has entered either 1 or 2 and exit when 0 is entered. Also, is a switch statement similar to if/else? Or is my thought process completely off.
You have a couple of problems here
First have a break after every case unless you know what youre doing
And second dont use ' ' around the numbers. Instead just put
using the 'break;' keyword will break the loop, otherwise the switch will carry on running. So if you value is 2, and you don't have a break, it will play everything under 2.
Also switch can ony be used with 'char' and 'int', as far as I'm aware.
Yea I realized it after. If I was using char it would have been okay. I realized that single quotes are used for characters and double quotes are used for string.