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

时间:2012-12-11 11:46:34

标签: c++ vector boolean

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

我做错了什么?

#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;
}

3 个答案:

答案 0 :(得分:1)

您忘记在false功能

的末尾返回check_gift
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;
}

答案 1 :(得分:1)

函数末尾没有return false;;因此,如果没有找到礼物,程序就会掉线并给出不明确的行为。

如果启用警告,编译器应该警告您。

答案 2 :(得分:0)

您需要在return false的末尾添加check_gift()