2016-08-31 32 views
-4

listSelectedDVD()當我輸入標題時應該顯示細節。但在我的代碼我能夠輸入標題,但沒有顯示details.unable傳入參數。在IF語句中調用void函數C++

#include <iostream> 
#include <string> 

using namespace std; 

struct myStock // declare myStock fields 
{ 
    string title; 
    double price; 
    int stockLevel; 
    bool award; 
};//end of strcut myStock 

myStock list[5]; 

void initialize(); 
void listSelectedDVD(string); 

int main() 
{ 
    int choice; 
    string enterTitle; 

    cout << "****** MAIN MENU ******" << endl; 
    cout << "1. List deatils of selected title" << endl; 
    cout << "4. Exit" << endl; 
    cout << endl; 
    cout << "enter your choice: " << endl; 
    cin >> choice; 

if (choice == 1) 
    {   
     cout << "Enter a Title: " << endl; 
     cin >> enterTitle; 
     listSelectedDVD(enterTitle); 
    } 
    else if (choice == 4) 
    { 
     return 0; 
    } 
    system("PAUSE");  
}//end of main 

這是我void初始化() & 無效listSelectedDVD(字符串enterTitle)的void函數;

void initialize() 
{ 
    list[0].title = "Ilo Ilo"; 
    list[0].price = 35.55; 
    list[0].stockLevel = 15; 
    list[0].award = true; 

    list[1].title = "Money Just Enough"; 
    list[1].price = 10.35; 
    list[1].stockLevel = 0; 
    list[1].award = false; 
} 

void listSelectedDVD(string enterTitle) 
{ 
for(int i=0;i<5;i++) 
    { 
     if (list[i].title.compare(enterTitle) == 0) //list[i].title == enterTitle 
     { 
      cout << "Title : " << list[i].title << endl; 
      cout << "Price : " << list[i].price << endl; 
      cout << "Stock : " << list[i].stockLevel << endl; 
      cout << "Award : " << list[i].award << endl; 
     } 
     else { 
      out<<"Invalid Title"<<endl; 
      //call back the main menu function// 
     } 
    } 
} 
+0

好像你必須提供事件序列。截至目前,答案可能只是標題不存在。 – ChiefTwoPencils

+0

你能提供你得到的輸出還是隻是空白?你也確定'enterTitle'是'Ilo Ilo'還是'Just Just Enough'? – LP496

+0

當使用'cin >> enterTitle;'時,找不到名字空格的標題。使用調試器來解決這類問題,而不是使用stackoverflow – grek40

回答

0

在調用listSelectedDVD()之前,您應該先從main調用initialize()。

int main() 
{ 
    initialize(); 

    //rest of your code 

}//end of main 
0

的主要問題是,當你走在這條線的用戶輸入「CIN >> enterTitle;」,它獲得的第一句話你輸入用空格隔開。所以,當你鍵入

錢只是夠

然後enterTitle的價值只是變成「」。

這就是爲什麼你的程序無法找到任何匹配的原因。 (「錢」不同於「錢夠用」)

解決此問題的一種方法是更改​​您的代碼,以便您可以接收整行作爲字符串輸入。

+0

作爲@ user5478656建議,您應該在主函數內調用初始化函數。你定義了這個函數並提供了它的原型,但是你永遠不會在你的代碼中調用它。 –