在IF语句C ++中调用void函数

时间:2016-08-31 06:25:44

标签: c++

输入标题时,

listSelectedDVD()应显示详细信息。但在我的代码中,我能够输入标题,但没有显示details.unable传入参数。

#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 initialize()&amp;的无效函数 void listSelectedDVD(string 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//
       }
    }
}

2 个答案:

答案 0 :(得分:0)

在调用listSelectedDVD()之前,您应首先从main调用initialize()。

$ awk '{ORS=(/},|\]/?RS:""); gsub(/[[:blank:]]+/,""); sub(/}$/,"},")}1' file
[{"server":"servename1","i.p":127.0.0.1,"domain":"generic","OS":"RHEL","Version":7.0},
{"server":"servename2","i.p":127.0.0.1,"domain":"generic","OS":"RHEL","Version":7.0},
{"server":"servename3","i.p":127.0.0.1,"domain":"generic","OS":"RHEL","Version":7.0},]

答案 1 :(得分:0)

主要问题是,当您在此行“cin&gt;&gt; enterTitle;”上获取用户输入时,它会获得您按空格分隔的第一个单词。所以当你输入

钱够了

然后enterTitle的值变为“ Money ”。

这就是您的程序无法找到任何匹配项的原因。 (“金钱”与“金钱足够”不一样)

解决此问题的一种方法是更改​​代码,以便您可以将整行作为字符串输入接收。