Convert if else to switch case in this example


Hi
In this code I have tried to substitute the if else statement between line 80 and 107 which I commented out, for a switch case statement. It is however not working the way it should when I compile it. I would greatly appreciate any advice. Thank you.

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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
  // HomeWorkRoem1.cpp : Defines the entry point for the console application.
// start.cpp : Defines the entry point for the console application.


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

using namespace std;

int readRoman(void);

int main()
{
	int number1, number2, result, aktiv;
	char aktion;

	aktiv = 1;
	while (aktiv == 1)
	{
		cout << "Please type in a roman number: ";
		number1 = readRoman();

		cout << "Please type in another roman number: ";
		number2 = readRoman();

		cout << "What do you want to do with that?" << endl << "(+-*/)" << endl;
		cin >> aktion;

		switch (aktion)
		{
		case '+':
			result = number1 + number2;
			break;
		case '-':
			result = number1 - number2;
			break;
		case '*':
			result = number1 * number2;
			break;
		case '/':
			result = number1 / number2;
			break;
		default:
			cout << "What do you mean '" << aktion << endl << "'?" << endl;
		}

		cout << "This equals: " << result << endl;
		while (result > 0) {
			switch (true)
			{
			case 1:  result >= 1000;
				result -= 1000;
				cout << "m";
				break;
			case 2: result >= 500;
				result -= 500;
				cout << "d";
				break;
			case 3: result >= 100;
				result -= 100;
				cout << "c";
				break;
			case 4: result >=50;
				result -= 50;
				cout << "l";
				break;
			case 5: result >= 10;
				result -= 10;
				cout << "x";
				break;
			case 6: result >= 5;
				result -= 5;
				cout << "v";
				break;
			case 7: result >= 1;
				result -= 1;
				cout << "i";
				break;
			}
			/*if (result >= 1000) {
				result -= 1000;
				cout << "m";
			}
			else if (result >= 500) {
				result -= 500;
				cout << "d";
			}
			else if (result >= 100) {
				result -= 100;
				cout << "c";
			}
			else if (result >= 50) {
				result -= 50;
				cout << "l";
			}
			else if (result >= 10) {
				result -= 10;
				cout << "x";
			}
			else if (result >= 5) {
				result -= 5;
				cout << "v";
			}
			else if (result >= 1) {
				result -= 1;
				cout << "i";
			}*/
		}
		cout << endl;
		if (number1 == 0) aktiv = 0;
	}

	return 0;
}

int readRoman(void)
{
	char roman[10];
	char romanDigits[8] = "ivxlcdm";
	int digitValue[7] = { 1, 5, 10, 50, 100, 500, 1000 };
	int arabic[10], figures, result;
	int j;

	cin >> roman;
	if (roman[0] == '0') return 0;

	for (figures = 0; roman[figures] != '\0'; figures++)
	{
		for (j = 0; j < 7; j++)
			if (roman[figures] == romanDigits[j])
			{
				arabic[figures] = digitValue[j];
				break;
			}
		if (j == 7)
			cout << "Mistake!" << endl <<

			"In your input " << roman << " " << roman[figures] <<
			" is not a roman digit.";
	}

	for (j = 1; j < figures; j++)
		if (arabic[j] > arabic[j - 1])
		{
			arabic[j] -= arabic[j - 1];
			arabic[j - 1] = 0;
		}

	result = 0;
	for (j = 0; j < figures; j++) result += arabic[j];

	return result;
}
Last edited on
Line 49:
 
switch(true)

true is a constant expression and is always 1... So it will always fall under your case 1 label. This is why your code did not work.
However, I don't think in this case you can convert the if and else (especially with the else ifs) to a switch. Reason being that switch checks for an exact match (==), while in your ifs you have >=. You might have to look for another way to break the number down if you want to use a switch.
Look at the syntax of the switch statement between lines 29 and 45:
1
2
3
4
switch( /*char or int type variable*/ ) {
case /* char or int RValue */ : // Do stuff if the variable being switched equals the RValue specified
default: // Do this if no case is met
}


The conditional is built into the switch statement and you are trying to specify your own, which you cannot do.
OK
I get why its not working.
Great, thanks for the help guys!
Topic archived. No new replies allowed.