C++ 布尔函数没有给出正确答案

C++ 布尔函数没有给出正确答案,c++,vector,boolean,C++,Vector,Boolean,我的布尔函数check_礼物有问题。当礼物在商店里时,它给出的值为假 我做错了什么 #include <iostream> #include <fstream> #include <vector> #include <string> #include <cstdlib> #include <string> #include <cassert> using namespace std; typedef vecto

我的布尔函数check_礼物有问题。当礼物在商店里时,它给出的值为假

我做错了什么

#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <cstdlib>
#include <string>
#include <cassert>
using namespace std;

typedef vector<string> Wishes;

int size(Wishes& w){ return static_cast<int>(w.size()); }


struct Wishlist
{
     double budget;
     Wishes wishes;
};


struct Gift
{
    double price;
    string name;
};

typedef vector<Gift> Giftstore;

int size(Giftstore& g) { return static_cast<int>(g.size()); }



void read_wishlist_into_struct(ifstream& infile, Wishlist& wishlist)
{
    double b;
    infile>>b;
    wishlist.budget=b;

    int i=0;
    string name;
    getline(infile,name);

    while(infile)
    {
        wishlist.wishes.push_back(name);
        i++;
        getline(infile,name);
    }
    infile.close();
}


void show_wishlist(Wishlist wishlist)
{
    cout<<"Budget: "<<wishlist.budget<<endl<<endl;
    cout<<"Wishes: "<<endl;
    for(int i=0; i<size(wishlist.wishes); i++)
    {
        cout<<wishlist.wishes[i]<<endl;
    }
    cout<<endl;
}



void read_giftstore_into_vector(ifstream& infile, Gift& gift, Giftstore& giftstore)
{
    double p;
    string name;
    int i=0;
    infile>>p;

    while(infile)
    {
        gift.price=p;
        getline(infile,name);
        gift.name=name;

        giftstore.push_back(gift);
        i++;
        infile>>p;
    }
    infile.close();
}

void show_giftstore(Giftstore giftstore)
{
    cout<<"All possible gifts in giftstore: "<<endl<<endl;

    for(int i=0; i<size(giftstore); i++)
    {
        cout<<giftstore[i].price<<"\t"<<giftstore[i].name<<endl;
    }
    cout<<endl;
}

bool check_gift(Giftstore giftstore, string giftname)
{
    int i=0;

    while(i<size(giftstore))
    {
        if(giftstore[i].name==giftname)
        {
            return true;
        }
        else
        {
            i++;
        }
    }
   return false;
}


void clear(Wishlist& b)
{
    b.budget=0;

    while(!b.wishes.empty())
    {
        b.wishes.pop_back();
    }
}

void copy(Wishlist a, Wishlist& b)
{
    b.budget=a.budget;

    for (int i=0; i<size(b.wishes); i++)
    {
        b.wishes.push_back(a.wishes[i]);
    }
}



int main ()
{

    ifstream infile2("giftstore.txt"); 


    Gift gift;
    Giftstore giftstore;

    read_giftstore_into_vector(infile2, gift, giftstore);
    show_giftstore(giftstore);



    string giftname;
    giftname=("dvd Up van Pixar");
    bool x;

    x=check_gift(giftstore, giftname);
    cout<<"in store?: "<<x<<endl;


return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
类型定义向量愿望;
int size(Wishes&w){return static_cast(w.size());}
结构愿望列表
{
双预算;
愿望;
};
结构礼物
{
双倍价格;
字符串名;
};
typedef矢量礼品店;
int size(Giftstore&g){return static_cast(g.size());}
void read_wishlist_进入结构(ifstream&infle,wishlist&wishlist)
{
双b;
填充>>b;
愿望清单.预算=b;
int i=0;
字符串名;
getline(填充,名称);
while(填充)
{
愿望列表。愿望。推回(姓名);
i++;
getline(填充,名称);
}
infle.close();
}
无效显示愿望列表(愿望列表愿望列表)
{

您是否忘记在
check\u gift
功能结束时返回
false

bool check_gift(Giftstore giftstore, string giftname)
{
    int i=0;

    while(i<size(giftstore))
    {
        if(giftstore[i].name==giftname)
        {
            return true;
        }
        else
        {
            i++;
        }
    }
    return false;
}
bool check\u礼品(礼品店礼品店,字符串礼品名)
{
int i=0;

而(i您需要在
check_gift()
的末尾添加
return false
,函数末尾没有
return false;
;因此,如果没有找到礼物,程序将从末尾掉下来并给出未定义的行为


如果启用了警告,编译器应该对此发出警告。

请编写一个简单的示例。除了@DavidHammen所说的之外,请避免使用常见问题解答中提到的称呼和标语。此外,如果您是指另一个问题,请发布一个指向该问题的链接。(尽管在这种情况下,该参考没有用处。)