2011-10-07 45 views
0

我不理解如何使它在testSelection中輸入我的選擇時指向功能測試。我如何去做這件事?它不應該去那裏嗎?Cin和指向功能選擇

#include <iostream> 
using namespace std; 

int test (int testSelection); 

int main() 
{ 
int testSelection; 

cout << "Welcome to the pizza place!" << endl; 
cout << "Choose 1 for pizza or 2 for drinks: "; 

cin >> testSelection; 


return 0; 

} 

int test (int testSelection) 
{ 
if (testSelection== 1) 
{ 
    cout << "select your Pizza" << endl; 

} 
if (testSelection== 2) 
{ 
    cout << "Please select your drink" << endl; 

} 
else 
    cout << "test"; 

return 0; 
} 

回答

5

你需要調用函數...

 
cin >> testSelection; 
test(testSelection); 

基本上,你寫一個函數定義INT測試(INT testSelection){...代碼...}但是,它的只是休眠代碼,直到你通過調用它來調用它。

2

我不確定你在問什麼。 testSelection是一個int,而不是返回int的函數(其中test是)。請詳細說明你在這裏試圖完成的任務,如果我不在線,你甚至不會打電話給test。據我所知,你真正想要的是:

int test (int testSelection); 

int main() 
{ 
    int testSelection; 

    cout << "Welcome to the pizza place!" << endl; 
    cout << "Choose 1 for pizza or 2 for drinks: "; 

    cin >> testSelection; 

    // you actually have to call the function... 
    test(testSelection); 

    return 0; 
} 

我沒加任何輸入驗證(你應該檢查cin居然搶到了有效的整數)。