I was given this question.
Input program only with numbers 0-9. The first input must include the code {1,0,3,7,0,3,1,1}. Input must be equal to 15 digits. If it is wrong it will ask for user input again.
I have a problem in accepting only the numbers 0-9. When I input a wrong command like 10370311abcdefg, it will asks the user to input again. But if I input a correct one like 10370311456789, it also will ask the user to input again which should be stopped. The printf for the correct answer does not come out.
Here this code:
int main() { char nim[16]; char kode[8] = {'1', '0', '3', '7', '0', '3', '1', '1'}; do { printf("NIM = "); gets(nim); int panjang = strlen(nim); int benar = 0; bool cekang =false; for (int i = 0; i < 8; i++) { if(nim[i] != kode[i]){ benar = 1; break; } } if (panjang < 15) { printf("Kurang dari 15!\n"); }else if(panjang > 15){ printf("Lebih dari 15\n"); }else if(panjang == 15 && benar == 1){ printf("All Required Characters not present\n"); }else if(panjang == 15 && benar == 0){ for(int i = 0;i < panjang;i++){ if(!(nim[i] >= '0' && nim[i] <= '9')){ printf("hanya0-9\n"); cekang=false;break; } } if(cekang==true){ printf("sucsess!\n");break; } } } while (true); } https://stackoverflow.com/questions/66512714/program-only-accept-number-in-c March 07, 2021 at 10:40AM
没有评论:
发表评论