I am writing a code for university. We should use enum and switch case.
The user gives a number from 1-7. The result is {doe=1, re, mi, sol, fa, la, si, default= different number}
.
However, I thought the default should come every time something different from 1-7 appears, but when the user types any letter or text, the program starts an infinite loop. How can I correct that?
I have tried the !isdigit()
, but I still have the same problem.
Below you can see the code:
#include <stdio.h>
#include <ctype.h>
int main (){
enum tast{doe = 1, re, mi, fa, sol, la, si}taste;
int opt;
do{
printf("Geben Sie ein Zahl zwischen 1 und 7 ein: ");
fflush(stdout);
scanf("%u", &taste);
/*
*
* if(!isdigit(taste)){
goto exit_loop;
}
exit_loop:
*
*/
switch(taste){
case doe:
printf("doe");
break;
case re:
printf("re");
break;
case mi:
printf("mi");
break;
case fa:
printf("fa");
break;
case sol:
printf("sol");
break;
case la:
printf("la");
break;
case si:
printf("si");
break;
default:
printf("--");
break;
}
printf("\n\nPressen Sie '0', um das Programm zu schliessen: ");
fflush(stdout);
scanf("%d", &opt);
printf("\n");
}while( opt!=0);
return 0;
}