Switch and Case issues

I read the article here, and some stuff online but don't understand.

Is a switch block a smaller If...Else statement?

here is my code:
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
// CODE

#include "stdafx.h"
#include <iostream>


int _tmain(int argc, _TCHAR* argv[])
{
	return 0;
}

int main()	
{
	using namespace std;

	int a=1;
	int b, c;

	cin >> b;

	switch (b)
	{
	case 1:
		b==1;
		cout << "no\n";

	case 2:
		b!=1;
		cout << "yes\n";
		c=b;

	default:
		cout << "You did something wrong\n";
	}

        cout << c;
	return 0;
}



===============================
///////////////////////////////
===============================

1
2
3
4
5
6
//output

no
yes
You did something wrong
this is what i came up with. some correction on what i "think" your trying to do. there was errors, missing defaults, unused variable. so its sort of a simplification to hopefully be more clear.

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
#include <iostream>
#include <limits>


using namespace std;

// class to keep window open.
class pause
{
public:
	~pause()
	{
		cin.sync();
		cout << endl;
		cout << "Press ENTER to continue...";
		cin.ignore(numeric_limits<streamsize>::max(), '\n' );
    }
};


int main(int argc, char* argv[])	
{

	int result;

        cout << "enter a case num 1 or 2 : ";
	cin >> result;

	switch (result)
	{
	case 1:
		cout << "case 1" << endl;
		break; // need this
	case 2:
		cout << "case 2" << endl;
		break; // need this
	default:
		cout << "default" << endl;
	}

		pause ps; // keep window open
		return 0;
}
	
Last edited on
Topic archived. No new replies allowed.