【问题标题】:wrong answer from boolean function [closed]布尔函数的错误答案[关闭]
【发布时间】:2012-11-29 02:52:57
【问题描述】:

我的布尔函数 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;
}

【问题讨论】:

  • 你试过什么?你很明显没有调试它,否则你会知道哪里出错了......
  • -1 请避免重复问同一个问题。你几个小时前就问过这个问题了。
  • 我改变了那个问题,之后我没有得到任何回应......我对c ++很陌生,为什么都是-1?我真的尽力了..
  • 归档在“请为我调试我的代码”下?

标签: c++ vector boolean


【解决方案1】:

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

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

在你的情况下 1.验证输入文件打开成功 2. 阅读时将每份礼物打印出来。

将是一个很好的开始。

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

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

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

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

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

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

现在可能会起作用。

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

【讨论】:

  • 再次感谢您!我得到你的教训。我会回到这里,当且仅当我可以调试 :-)
  • 我不想让你觉得你在 stackoverflow.com 上不受欢迎,只是你需要了解这个网站的用途,以及你应该学会自己做什么。
猜你喜欢
  • 1970-01-01
  • 2019-05-17
  • 1970-01-01
  • 1970-01-01
  • 2018-12-29
  • 1970-01-01
  • 1970-01-01
  • 2015-10-10
  • 1970-01-01
相关资源
最近更新 更多