Problem with switch case and while loop.

Hello everyone,

the problem I have is probably pretty obvious and simple to solve, but I really can't get behind it. For homework we had to write a program that messes with char arrays, and everything works alright, except the menu!

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
while (1) {
		cout << "Sie haben folgende Moeglichkeiten:\n\n";
		cout << "1.	Anzahl der Zeichen des Textes ermitteln\n";
		cout << "2.	Anzahl eines Zeichens ermitteln\n";
		cout << "3.	Ein bestimmtes Zeichen ersetzten\n";
		cout << "4.	Zeichen sortieren\n";
		cout << "5.	Text ausgeben\n";
		cout << "6.	Neuen Text eingeben\n";
		cout << "0.	Programm beenden\n";
		cout << endl;
		cout << "Ihre Wahl >>> ";
			cin >> ch;

		system("cls");

		switch (ch) {
			case 0: return 0;
			case 1: len(string);
				break;
			case 2: chr(string);
				break;
			case 3: repl(string);
				break;
			case 4: sort(string);
				break;
			case 5: print(string);
				break;
			case 6: enter(string);
				break;
			default: 
				system("cls");
				cout << "Falsche Eingabe!\n\n";
				break;
		}
		
	}


So what's supposed to happen is, that you enter a string and then you see the menu. You choose what should happen next, but if you enter an invalid character the menu should start again. And there is the problem, when I enter for example "f", it just goes crazy in an endless loop. I tried switching from switch case to if's but that also didn't work that well.
It's nothing major, since the rest works fine, but it really really bugs me.

Thanks in advance for the answers!
It is caused by fact that ch still exists and have "f" in it. You just have to do something to get rid of that "f" in ch variable but I'm not quite sure that it is cause.
EDIT: By the way whats the type of ch? is it int or char? If it's int everything should be working fine but if it is char you should but apostrophes around numbers
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
		switch (ch) {
			case '0': return 0;
			case '1': len(string);
				break;
			case '2': chr(string);
				break;
			case '3': repl(string);
				break;
			case '4': sort(string);
				break;
			case '5': print(string);
				break;
			case '6': enter(string);
				break;
			default: 
				system("cls");
				cout << "Falsche Eingabe!\n\n";
				break;
		}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
cin >> ch;
		system("cls");
		switch (ch) {
			case 0: return 0;
			case 1: len(string);
				break;
			case 2: chr(string);
				break;
			case 3: repl(string);
				break;
			case 4: sort(string);
				break;
			case 5: print(string);
				break;
			case 6: enter(string);
				break;
			default: 
				system("cls");
				cout << "Falsche Eingabe!\n\n";
				break;
		}


Numbers from 0-9 are system codes, not keyboard keys. Numbers you press from your keyboard are actually present as '0'-'9' in your computer.

So, your switch statement doesn't work. Your if-statements don't work. Change it to :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
cin >> ch;
		switch (ch)
 {
			case '0': return 0;
			case '1': len(string);
				break;
			case '2': chr(string);
				break;
			case '3': repl(string);
				break;
			case '4': sort(string);
				break;
			case '5': print(string);
				break;
			case '6': enter(string);
				break;
			default: 
				system("cls");
				cout << "Falsche Eingabe!\n\n";
				break;
		}

For your information : If you change the type of (ch) to an int, this may also work perfectly too.
Does that help you? :)
I actually had it as an int before, but now it works thanks :)

EDIT: It was an int before, but now that I put it in apostrophes and changed it to char it works.
Last edited on
Glad it helped :)
Topic archived. No new replies allowed.