2013-06-06 66 views
0

因此,在我的代碼中,無論我改變了多少,我都無法讓它正常工作 它應該成爲問題。包括掃描對應那麼它應該調用與聲明的選項現在導航和與它 但不管你選擇什麼樣的選擇,它只是說工作的一個選項 一個INT 對不起C/C++函數沒有正確調用開關結構

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

#define OPENWINDOW "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n" 

void question(int option) 

{ 
     printf("What Would You Like To Do?\n"); 
     printf("\t1.Add A Reminder\n\t2.View Reminders\n\t3.Manage Current Reminders\n\t4.Settings\n"); 
     scanf("%i", &option); 

} 

void navigate(int option) 
{ 
    switch(option) 
    { 
     case 1: 
     printf(OPENWINDOW); 
     break; 
     case 2: 
     printf(OPENWINDOW); 
     break; 
     case 3: 
     printf(OPENWINDOW); 
     break; 
     case 4: 
     printf(OPENWINDOW); 
     break; 
     default : 
     printf("sorry"); 
     question(option); 


    } 
} 

int main() 
{ 
    int option;  

    question(option); 
    navigate(option);  

    return 0; 
} 

回答

2

參數按值傳遞,而不是引用。所以,你的「選項」arg在函數結束後很快就會「消失」。

如果您將「參考」傳遞給var,那麼您可以使用它來填充調用者變量。以下代碼和示例修復了它。

void question(int *option) 
{ 
     printf("What Would You Like To Do?\n"); 
     printf("\t1.Add A Reminder\n\t2.View Reminders\n\t3.Manage Current Reminders\n\t4.Settings\n"); 
     scanf("%i", option); 
} 

然後調用它像這樣:

int option; 
question(&option); 
// now you can use option... 

由於函數可以返回值,你還可以:

int question(void) 
{ 
     int option; 
     printf("What Would You Like To Do?\n"); 
     printf("\t1.Add A Reminder\n\t2.View Reminders\n\t3.Manage Current Reminders\n\t4.Settings\n"); 
     scanf("%i", &option); 
     return option; 
} 

// ... 
int option = question(); 
// ... 

的導航和主要使用引用(指針):

void navigate(int *option) 
{ 
    switch(*option) 
    { 
     case 1: 
      printf(OPENWINDOW); 
      break; 
     case 2: 
      printf(OPENWINDOW); 
      break; 
     case 3: 
      printf(OPENWINDOW); 
      break; 
     case 4: 
      printf(OPENWINDOW); 
      break; 
     default: 
      printf("sorry"); 
      question(option);  
    } 
} 

int main(void) 
{ 
    int option;  

    question(&option); 
    navigate(&option);  

    return 0; 
} 
+0

(同樣的「修復」應該應用於功能導航) – ShinTakezou

+0

謝謝。我犯了錯誤的學習Java作爲第一語言,並完全困惑指針謝謝 –

+0

該程序崩潰,如果我應用修復導航爲什麼? @shin takezou –

1

您需要通過option作爲傳遞參考。將選項的地址傳遞給問題()並在那裏更新。

請參閱修改後的代碼。

void question(int *option) 
{ 
     printf("What Would You Like To Do?\n"); 
     printf("\t1.Add A Reminder\n\t2.View Reminders\n\t3.Manage Current Reminders\n\t4.Settings\n"); 
     scanf("%i", option); 
} 

呼叫的問題()如,

question(&option); 
0

你被值傳遞變量optionquestion(option)

你應該參考

void question(int *option) 
{ 
     printf("What Would You Like To Do?\n"); 
     printf("\t1.Add A Reminder\n\t2.View Reminders\n\t3.Manage Current Reminders\n\t4.Settings\n"); 
     scanf("%i", option); 

} 

void navigate(int *option) 
{ 
    switch(*option) 
    { 
     case 1: 
     printf(OPENWINDOW); 
     break; 
     case 2: 
     printf(OPENWINDOW); 
     break; 
     case 3: 
     printf(OPENWINDOW); 
     break; 
     case 4: 
     printf(OPENWINDOW); 
     break; 
     default : 
     printf("sorry"); 
     question(option); 

} 

int main() 
{ 
    int option;  

    question(&option); 
    navigate(&option);  

    return 0; 
} 

通過option varible更多有關這方面的信息看看這個鏈接Difference between call by reference and call by value

0

您正在傳遞「option」作爲按值調用。因此無論你傳遞給問題()。會丟失。

要麼您從問題()返回「選項」並將其傳遞給navigate()。

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

#define OPENWINDOW "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n" 

int question() 
{  int option; 
     printf("What Would You Like To Do?\n"); 
     printf("\t1.Add A Reminder\n\t2.View Reminders\n\t3.Manage Current Reminders\n\t4.Settings\n"); 
     scanf("%i", &option); 
     return option; 

} 

void navigate(int option) 
{ 
    switch(option) 
    { 
     case 1: 
     printf(OPENWINDOW); 
     break; 
     case 2: 
     printf(OPENWINDOW); 
     break; 
     case 3: 
     printf(OPENWINDOW); 
     break; 
     case 4: 
     printf(OPENWINDOW); 
     break; 
     default : 
     printf("sorry"); 
     question(option); 


    } 
} 

int main() 
{ 
int option; 

option = question(); 
navigate(option); 

return 0; 
} 
~ 
0

你需要ei然後將option的指針傳遞給question或從函數question返回。

在您的案例中option的值main()question()中讀取時不會改變。更新您的代碼爲

int question() 

{ 
     int option; 

     printf("What Would You Like To Do?\n"); 
     printf("\t1.Add A Reminder\n\t2.View Reminders\n\t3.Manage Current Reminders\n\t4.Settings\n"); 
     scanf("%i", &option); 
     return option; 
} 

int main() 
{ 
    int option; 
    option = question(option); 
    navigate(option); 
    return 0; 
} 
0

如果您不想使用傳遞引用,則可以使用您在代碼中使用的傳遞值。它只需要正確實施。您可以通過將void更改爲「int」並在問題函數結束前發出return語句來更改「無效問題」以返回值。檢查下面的代碼:

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

#define OPENWINDOW "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n" 

int question() 

{ 
     printf("What Would You Like To Do?\n"); 
     printf("\t1.Add A Reminder\n\t2.View Reminders\n\t3.Manage Current Reminders\n\t4.Settings\n"); 
     scanf("%i", &option); 
     return i; 
} 

void navigate(int option) 
{ 
    switch(option) 
    { 
     case 1: 
     printf(OPENWINDOW); 
     break; 
     case 2: 
     printf(OPENWINDOW); 
     break; 
     case 3: 
     printf(OPENWINDOW); 
     break; 
     case 4: 
     printf(OPENWINDOW); 
     break; 
     default : 
     printf("sorry"); 
     question(option); 
    } 
} 

int main() 
{ 
int option; 
option = question(option); 
navigate(option); 

return 0; 
} 
0

因爲變量選項只通過它的值寫入功能的問題(),變量期權的價值確實是不變的,那麼,也許你應該返回的功能問題選項的值()