2016-08-31 2 views
-4

listSelectedDVD()은 제목을 입력 할 때 세부 사항을 표시해야합니다. 하지만 내 코드에서 제목을 입력 할 수 있지만 세부 사항을 표시하지 못했습니다. 인수에서 전달할 수 없습니다. 여기 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 

무효 초기화() & 무효 listSelectedDVD (문자열 enterTitle)의 내 무효 기능입니다;

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' 또는'Money Just Enough'입니까? – LP496

+0

'cin >> enterTitle;'을 사용할 때 이름에 공백이있는 제목이 없습니다. 이러한 종류의 문제가 아니라 디버거를 사용 stackoverflow – grek40

답변

0

먼저) (listSelectedDVD를 호출하기 전에 주()에서 초기화를 호출해야합니다. 이 줄에 사용자 입력 찍을 때

int main() 
{ 
    initialize(); 

    //rest of your code 

}//end of main 
0

주요 문제는, "CIN >> enterTitle를,"당신이 공백으로 구분하여 입력 한 첫 번째 단어를 얻을 수 있습니다. 당신이

충분히

돈을 입력 할 때 그럼 enterTitle의 값은 ""가된다.

이 때문에 프로그램에서 일치하는 항목을 찾을 수 없습니다. ("Money"는 "Money Just Enough"와 동일하지 않습니다.)

이 문제를 해결하는 한 가지 방법은 전체 줄을 문자열 입력으로받을 수 있도록 코드를 변경하는 것입니다.

+0

@ user5478656 제안대로 main 함수 내부에서 initialize 함수를 호출해야합니다. 이 함수를 정의하고 프로토 타입을 제공했지만 코드에서 아무 것도 호출하지 않았습니다. –

관련 문제