2015-03-25 110 views
-1

我想要一個名爲「userPrompt」的函數,並要求用戶輸入一個名爲「choose」的整數值,以便我可以使用switch語句。Switch Statement - 嵌套函數 - C++

但它沒有工作它說:「選擇」未申報。

我想它會首先啓動主函數,並在其中第一個命令將初始化userPrompt函數。那麼感謝userPrompt我會有一個選擇值,以便交換機可以工作。

那麼這段代碼有什麼問題?

如何使用嵌套函數?(我希望它被稱爲像)

是我的代碼的順序錯了嗎?

任何幫助將不勝感激。

userPrompt(){ 

    int choose; 

    cout << " Please Choose An Option : " << endl; 
    cout << " Type 1 to Add new grades : " << endl; 
    cout << " Type 2 to Calculate the average grades : " << endl; 
    cout << " Type 3 to Calculate the total grades : " << endl; 
    cout << " Type 4 to Exit : " << endl;  

    cin >> choose; 

} 

    int main() 
    { 

     userPrompt(); 

     switch(choose){ 

     case 1 
      getGrade(); 
      userPrompt(); 
      break; 

     case 2 

      int average; 
      getGrade(); 

      average = total/counter; 

      cout << average; 

      break;  

     case 3 

      getGrade(); 
      cout << total; 
      break; 

     case 4 

      cout << "Thanks for Trying" << endl; 

      return 0; 

      system("pause"); 

      break; 

     default 

      cout << "Please Choose A Valid Option ! : " << endl; 
      validOption(); 

     } 
    } 
+0

'choose'僅宣佈在本地'userPrompt()'你需要一個'返回'語句或輸出參數,將值傳遞給'main()'中的另一個變量。代碼中有更多的語法錯誤。 – 2015-03-25 07:59:35

+0

謝謝你的回答,我在C++上也是新手,在編程方面也是新的。它會越來越好,我希望:) 我會解決它的。謝謝。 – Recomer 2015-03-25 08:12:45

回答

0

你忘了coloncase,你也需要return choose

case 1: 

試試這個:

int userPrompt(){ 

int choose; 

cout << " Please Choose An Option : " << endl; 
cout << " Type 1 to Add new grades : " << endl; 
cout << " Type 2 to Calculate the average grades : " << endl; 
cout << " Type 3 to Calculate the total grades : " << endl; 
cout << " Type 4 to Exit : " << endl;  

cin >> choose; 
return choose; 
} 
int main() 
{ 

    int choose = userPrompt(); 

    switch(choose){ 

    case 1: 
     getGrade(); 
     userPrompt(); 
     break; 

    case 2: 

     int average; 
     getGrade(); 

     average = total/counter; 

     cout << average; 

     break;  

    case 3: 

     getGrade(); 
     cout << total; 
     break; 

    case 4: 

     cout << "Thanks for Trying" << endl; 

     return 0; 

     system("pause"); 

     break; 

    default: 

     cout << "Please Choose A Valid Option ! : " << endl; 
     validOption(); 

    } 
} 
+0

謝謝你解決了我的問題。儘管我現在有新的問題,但我知道如何解決這些問題,謝謝你! – Recomer 2015-03-25 08:14:29

0

變化的代碼如下:

int userPrompt(){ //--> changed into a function returning the choice 

    int choose; 

    cout << " Please Choose An Option : " << endl; 
    cout << " Type 1 to Add new grades : " << endl; 
    cout << " Type 2 to Calculate the average grades : " << endl; 
    cout << " Type 3 to Calculate the total grades : " << endl; 
    cout << " Type 4 to Exit : " << endl;  

    cin >> choose; 
    return choose; 
} 

int main() 
{ 
    //--> declare choose in main and assign a value using the function call 
    int choose = userPrompt(); 

    switch(choose){ 

    case 1: 
     getGrade(); 
     userPrompt(); 
     break; 

    case 2: 

     int average; 
     getGrade(); 

     average = total/counter; 

     cout << average; 

     break;  

    case 3: 

     getGrade(); 
     cout << total; 
     break; 

    case 4: 

     cout << "Thanks for Trying" << endl; 

     return 0; 

     system("pause"); 

     break; 

    default 

     cout << "Please Choose A Valid Option ! : " << endl; 
     validOption(); 

    } 
} 
+0

我也會嘗試這種方式。它看起來像Rahul Tripathi發送的前一個代碼一樣:) – Recomer 2015-03-25 08:15:18

+0

這是一樣的,我們同時發佈了我們的代碼。但是請幫我一個忙,並且upvote ;-) – 2015-03-25 08:20:09

0

簡單的錯誤。把冒號放在case 1:那樣的情況下

`初始化選擇並嘗試它。

int choose = o;

1

C++使用的 「範圍」,其種類的轉化的變量 「可視性」。 userPrompt()函數的「choose」變量只在userPrompt()函數的作用域內「可見」(觸手可及)。

所以,你可以聲明userPrompt()函數

int userPrompt() // Returns the user choice 
{ 
    ... // your existing code here 
    return choose; 
} 

然後裏面的main(),你會做這樣的事情:

int main() 
{ 
    int choice = userPrompt(); 
    switch(choice) 
    ... 
+0

感謝您的技術解釋,我會記住它。 – Recomer 2015-03-25 08:17:12

0

在C++中每個函數都有一個返回類型。這意味着它會返回一些東西或返回void(即不返回)。在你的程序中,userPrompt沒有返回類型,既沒有void也沒有任何其他返回類型,因此這部分是程序中的第一個錯誤。 下一個錯誤是,在開關聲明任何情況下標籤後標籤後面必須跟一個冒號「:」

+0

非常感謝! – Recomer 2015-03-26 09:05:14