2013-10-02 24 views
1

好吧,我正在編寫一些代碼,並且在測試開關函數時遇到了問題。它會從所選的所有案例中增加(我的解釋)。有人能幫我解釋這是爲什麼嗎?C中的開關函數做所有的情況

#include <stdio.h> 
#include <stdlib.h> 
#include "ratedzfunctions.h" 

int main() 
{ 

    int selection, loop=1; 
    FILE* fajl; 

    //Opening the participants file 
    fajl=fopen("participants.txt","r+"); 
    if (fajl==NULL) 
    { 
     printf("The file cannot be opened.\n"); 
    } 

    //MENU 
    do 
    { 
     printf("\nMENU: \n------------\n1. RATEDZ\n\n2. STATISTICS\n\n3. EXIT\n\n==>"); 
     scanf("%d", &selection); 

     switch (selection) 
     { 
     case 1: 
      ratedz(fajl); 
     case 2: 
      stats(fajl); 
     case 3: 
      loop=0; 
     } 
    } 

    while (loop==1); 

    fclose(fajl); 

    return 0; 
} 

//THIS IS FROM RATEDZFUNCTIONS.H 

void ratedz(FILE *fajl) 
{ 
    printf("\nTEST RATEDZ"); 
} 

void stats(FILE *fajl) 
{ 
    //Printing all participants 
    char *buffer=(char*) malloc(50); 

    while(fscanf(fajl,"%s %s %s", buffer)!=EOF) 
    { 
     printf("\n%s %s %s", buffer); 
    } 

    free(buffer); 
} 

回答

3

你應該每個case後放break;

switch/case規則很簡單,一個mached案件後,所有的下列情況下,直到break;switch結束時執行:

switch (selection) 
{ 
case 1: 
     ... 
     break; 
case 2: 
     ... 
     break; 
case 3: 
     ... 
     break; // Last break is not necessary 
       // but it's good practice to put it. 
} 

有很好的情況下,這消除break;是合理的:

switch(letter) 
{ 
case 'i': 
case 'a': 
case 'o': 
case 'u': 
case 'e': 
      printf ("Vowel!"); 
      break; 
default : 
      printf ("Consonant!"); 
      break; 
} 
+0

非常感謝您! – Narraxus

+0

你能告訴我爲什麼它只打印一次案例2中的.txt文件?當我再次選擇它時,它不會再執行一次嗎? – Narraxus

+0

哦,這是因爲我沒有倒帶文件:P傻我。 – Narraxus

5

你忘了每個case後添加break;聲明。

case 2: 
    stats(fajl); 
    break; /* <---- */ 
2

如果在每種情況下都不添加break它會將ju ST回落到下一個案例:

switch (selection) 
{ 
    case 1: 
     ratedz(fajl); 
     break ; 
    case 2: 
     stats(fajl); 
     break ; 
    /* ... */ 
} 
1

語法switch staement用C

switch(expression) 
{ 
    case (constant-expression) : staements 
    .... 
    case (constant-expression) : staements 
    default : statements 
} 

當您該組聲明中最後一條語句必須是break特定的情況下工作。 如果沒有break聲明,當執行case中的最後一條語句時,控件將「落入」下面的case中的第一條語句;下一個case的案例標籤(const-expression)將被忽略。沒有break(或某些跳轉聲明),控制將從一個case流入下一個。

+0

實際上,switch的「語法」可能比這更復雜。可以有任何語句或塊作爲依賴語句,'case'只是該依賴語句中的標籤。你所描述的只是一個(非常常見的)'switch'的用例。 –

2

A caseswitch聲明中被視爲標籤(參見C.11 § 6.8.1)。 。實際上根本不需要任何case(參見C.11 § 6.8.4)。

switch (0) { /* do nothing */ } 

上面的代碼會編譯得很好。

由於case就像一個標籤,因此沒有隱含的語義附加到它會導致它自動跳到switch之外。正如break用於提前離開循環塊,break也被用於早期離開switch塊。

0

一些修改,

#include <stdio.h> 
#include <stdlib.h> 
#include "ratedzfunctions.h" 

int main() 
{ 
    int selection, loop=1; 
    FILE* fajl; 

    //Opening the participants file 
    fajl=fopen("participants.txt","r+"); 
    if (fajl==NULL) 
    { 
     printf("The file cannot be opened.\n"); 
     exit(1); //handle error when file cannot be opened... 
    } 

    //MENU 
    do 
    { 
     printf("\nMENU: \n------------\n1. RATEDZ\n\n2. STATISTICS\n\n3. EXIT\n\n==>"); 
     scanf("%d", &selection); 

     switch (selection) 
     { 
     case 1: 
      ratedz(fajl); 
break; 
     case 2: 
      stats(fajl); 
break; 
     case 3: 
      loop=0; 
break; 
     default: 
break; 
     } 
    } 

    while (loop==1) 
    { 
    //do stuff here 
    } 
    fclose(fajl); 

    return 0; 
}