2013-04-21 35 views
-3

这主要是出于好奇,为什么会发生这种情况,因为它对我而言并不重要。如果我输入了一个无效的数字,它会正确地转到重复标签并要求我再次输入一个数字,但是如果我输入了像'f'这样的字符,它将无休止地循环而不停止。为什么是这样?当在C中输入字符时,代码循环

这里的数组和所有变量都是int类型的。

repeat: 
    printf("Enter number of available space, you are %c: ", userXO); 
    scanf("%d", user); 

    switch (*user) 
    { 
     case 1: if (spaces[0][0] == 49){ spaces[0][0] = userXO;}else goto repeat; break; 
     case 2: if (spaces[0][1] == 50){ spaces[0][1] = userXO;}else goto repeat; break; 
     case 3: if (spaces[0][2] == 51){ spaces[0][2] = userXO;}else goto repeat; break; 
     case 4: if (spaces[1][0] == 52){ spaces[1][0] = userXO;}else goto repeat; break; 
     case 5: if (spaces[1][1] == 53){ spaces[1][1] = userXO;}else goto repeat; break; 
     case 6: if (spaces[1][2] == 54){ spaces[1][2] = userXO;}else goto repeat; break; 
     case 7: if (spaces[2][0] == 55){ spaces[2][0] = userXO;}else goto repeat; break; 
     case 8: if (spaces[2][1] == 56){ spaces[2][1] = userXO;}else goto repeat; break; 
     case 9: if (spaces[2][2] == 57){ spaces[2][2] = userXO;}else goto repeat; break; 
     default: goto repeat; break; 
} 
+0

无论发生什么缩进?该代码几乎不可读。 – sapi 2013-04-21 01:14:01

+8

你为什么要使用[goto](http://stackoverflow.com/questions/3517726/what-is-wrong-with-using-goto)?为什么?! WHYYYY? – timss 2013-04-21 01:15:18

+1

@sapi它是缩进的。如果你正在谈论if语句,它不是很复杂,我没有缩进,所以它可以放在屏幕上。 – 0x41414141 2013-04-21 01:16:07

回答

5

scanf("%d", user);尝试读取的数,发现一个char(f),叶它在缓冲器并结束。循环然后循环并再次执行scanf("%d", user);。并再次...

+0

“缓冲区溢出”是什么? – 0x41414141 2013-04-21 01:22:36

+2

@ bh3244不,当您尝试访问超过数组末尾的元素时,会发生“缓冲区溢出”。 – Kevin 2013-04-21 01:25:10

2

这里是我会写你做了什么:

int rc, user; 
char buf[100]; 

for (;;) // repeat until explicitly broken out of 
{ 
    printf ("Enter number of available space; you are %c: ", userXO); 
    if (!fgets (buf, sizeof buf, stdin)) /* end of file or i/o error? */ 
     break; 

    rc = sscanf(buf, "%d", &user); 
    if (rc != 1) /* other than one parsed input item is an error */ 
    { 
     printf ("invalid number; try again\n"); 
     continue; 
    } 

    /* 
    * this switch has the odd property of potentially 
    * doing all 9 cases for case 1, 8 cases for case 2, etc. 
    * Maybe explicit breaks for success are needed? 
    */ 
    switch (user) 
    { 
    case 1: if (spaces[0][0] == 49) spaces[0][0] = userX0; else continue; 
    case 2: if (spaces[0][1] == 50) spaces[0][1] = userX0; else continue; 
    case 3: if (spaces[0][2] == 51) spaces[0][2] = userX0; else continue; 
    case 4: if (spaces[1][0] == 52) spaces[1][0] = userX0; else continue; 
    case 5: if (spaces[1][1] == 53) spaces[1][1] = userX0; else continue; 
    case 6: if (spaces[1][2] == 54) spaces[1][2] = userX0; else continue; 
    case 7: if (spaces[2][0] == 55) spaces[2][0] = userX0; else continue; 
    case 8: if (spaces[2][1] == 56) spaces[2][1] = userX0; else continue; 
    case 9: if (spaces[2][2] == 57) spaces[2][2] = userX0; else continue; 
    default: continue; 
    } 
    break; /* if valid case(s) taken, exits loop */ 
} 

正如你所看到的,也没有必要为一个标签或goto语句。代码也更紧凑。