-5

我的布尔函数 check_gift 无法正常工作。

我将一个 txt 文件复制到矢量礼品店中。现在我想检查给定的商品是否在商店中。为了测试函数 check_gift,我从实际的 txt 文件中获取了一个项目,但该函数给出了错误的答案。它返回 false 而不是 true。

我究竟做错了什么?

#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<giftstore.size(); 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)
        {
            cout<<"Yes"<<endl;
            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;
}
4

1 回答 1

0

了解如何调试。如果您无法通过代码逐行跟踪,请尝试保留某种日志。

现在至少将其输出到控制台。

在您的情况下 1. 验证输入文件已成功打开 2. 在您阅读时打印出每个礼物。

将是一个很好的开始方式。

如果您希望能够放入多个日志语句然后稍后将其删除,您可以使用可以在一个地方关闭的宏。

对于继续投入生产的大型项目,日志记录是一项相当棘手的技能,但是您应该在短期内学习如何使用它来调试您的程序。

我们在这里甚至看不到您的输入文件中的内容。这就是人们对您的问题投反对票的原因。

好的:现在你告诉我你的问题是你需要从你读入的每个字符串的前面修剪空格。

有多种方法可以做到这一点,但

trimmed = s.substr( s.find_first_not_of(" \n\r\t" ) );

现在可能会工作。

但是我原来的答案仍然成立:请学习调试。如果您在读入字符串时输出了这些字符串,您会看到这些前导空格。

于 2012-12-11T15:25:51.390 回答