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!
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.
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