2016-07-06 128 views
-1

這是我在stackoverflow上的第一個問題,所以我希望它都出來格式正確。我正在編寫我的編程課程,我們必須編寫一個程序,它接受兩個輸入,然後產生結果讀數。我必須創建兩個功能來詢問一個人想要參加哪個音樂會,然後他們想要爲該音樂會購買多少票。我已經創建了這些函數,但是現在我無法調用函數來打印我的結果。調用C++函數

#include <iostream> 
using namespace std; 

char GetConcert() 
{ 
    char Concert; 
    cout << "The following concerts are available:\n"; 
    cout << "  B for Beyonce\n"; 
    cout << "  L for Lady Gaga\n"; 
    cout << "  T for Taylor Swift\n"; 
    cout << "Enter the letter for the concert you want:\n"; 
    cin >> Concert; 
    return Concert; 
} 

int GetNumTickets() 
{ 
    int NumTickets; 
    cout << "Enter the number of tickets you want:\n"; 
    cin >> NumTickets; 
    while ((NumTickets < 0) || (NumTickets > 10)) 
    { 
     if (NumTickets < 0) 
     cout << "You can not sell tickets here.\n"; 
     else if (NumTickets > 10) 
     cout << "You may not purchase more than 10 tickets.\n"; 
     cout << "Enter the number oftickets you want:\n"; 
     cin >> NumTickets; 
    } 
    return NumTickets; 
} 

int main() 
{ 
    // Declare Variables 
    char Concert; 
    int NumTickets; 

    // Call function to find out the concert they want to attend 


    // Call function to find out how many tickets they want 

    // Print out the information you have collected. 
    cout << "\nThe customer has placed the following order:\n"; 
    cout << "Concert: " << Concert << endl; 
    cout << "Number of Tickets: " << NumTickets << endl; 
    return 0; 
} 

我宣佈我的主段中的變量,但是當我嘗試調用的函數,它說,我不能從整數轉換爲字符。

+0

顯示觸發錯誤的代碼。顯示錯誤信息的確切完整文本;不要複述。 –

+0

main.cpp | 40 |錯誤:從'char(*)()'無效轉換爲'int'[-fpermissive] | – Lumberjacked216

+0

第40行main.cpp中的代碼是什麼? –

回答

1

爲了在C++中調用函數,你需要在函數名的末尾加上()。在這個例子中,GetNumTickets()GetConcert()。由於這些函數也是返回值,因此保存返回值以便稍後使用或立即使用它們非常重要。您可以嘗試:

char Concert = GetConcert(); 
int NumTickets = GetNumTickets();