布尔函数不给正确的答案 [英] Boolean function not giving right answer
本文介绍了布尔函数不给正确的答案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有我的布尔函数check_gift一个问题。它给人的值false,而礼物是在店里...
我是什么做错了吗?
的#include<&iostream的GT;
#包括LT&;&的fstream GT;
#包括LT&;矢量>
#包括LT&;串GT;
#包括LT&; cstdlib>
#包括LT&;串GT;
#包括LT&;&了cassert GT;
使用命名空间std;typedef的矢量<字符串>愿望;INT尺寸(愿望和放大器; W){返回的static_cast< INT>(w.size()); }
结构心愿
{
双预算;
希望愿望;
};
结构礼物
{
双价;
字符串名称;
};typedef的矢量<礼品GT; Giftstore;INT尺寸(Giftstore&安培; G){返回的static_cast< INT>(g.size()); }无效read_wishlist_into_struct(ifstream的&安培; INFILE,收藏和放大器;心愿)
{
双B:
INFILE>> B;
wishlist.budget = B; INT I = 0;
字符串名称;
函数getline(INFILE,名); 而(INFILE)
{
wishlist.wishes.push_back(名);
我++;
函数getline(INFILE,名);
}
infile.close();
}
无效show_wishlist(收藏心愿)
{
COUT<<预算:<< wishlist.budget<<&ENDL LT;< ENDL;
COUT<<祝愿:<< ENDL;
的for(int i = 0; I<尺寸(wishlist.wishes);我++)
{
COUT&所述;&下; wishlist.wishes [1] - ;&下; ENDL;
}
COUT<< ENDL;
}无效read_giftstore_into_vector(ifstream的&安培; INFILE,礼品,放大器;礼品,Giftstore&安培; giftstore)
{
双磷;
字符串名称;
INT I = 0;
INFILE>指p; 而(INFILE)
{
gift.price = P;
函数getline(INFILE,名);
gift.name =名称; giftstore.push_back(礼品);
我++;
INFILE>指p;
}
infile.close();
}无效show_giftstore(Giftstore giftstore)
{
COUT<<在giftstore所有可能的礼物:<<&ENDL LT;< ENDL; 的for(int i = 0; I<尺寸(giftstore);我++)
{
COUT<< giftstore [I]。价格<<\\ t的<< giftstore [我]。名称<< ENDL;
}
COUT<< ENDL;
}布尔check_gift(Giftstore giftstore,串giftname)
{
INT I = 0; 而(I<尺寸(giftstore))
{
如果(giftstore [我]。名称== giftname)
{
返回true;
}
其他
{
我++;
}
}
返回false;
}
void清除(收藏和b)
{
b.budget = 0; 而(!b.wishes.empty())
{
b.wishes.pop_back();
}
}无效副本(愿望清单中的,收藏和b)
{
b.budget = a.budget; 的for(int i = 0; I<尺寸(b.wishes);我++)
{
b.wishes.push_back(a.wishes [I]);
}
}诠释的main()
{ ifstream的infile2(giftstore.txt);
馈赠;
Giftstore giftstore; read_giftstore_into_vector(infile2,礼品,giftstore);
show_giftstore(giftstore); 串giftname;
giftname =(DVD最多面包车屋环游记);
布尔X; X = check_gift(giftstore,giftname);
COUT<<在商店?:<< X<< ENDL;
返回0;
}
解决方案
有没有返回false;
在函数的结尾;因此,如果没有找到礼物,程序会脱落结束,并给未定义的行为。
编译器应就此发出警告,如果启用了警告。
I have a problem with my boolean function check_gift. It gives the value false while the gift is in the store...
What am I doing wrong?
#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;
}
解决方案
There is no return false;
at the end of the function; so if the gift is not found, the program will fall off the end and give undefined behaviour.
The compiler should warn you about this, if you enable warnings.
这篇关于布尔函数不给正确的答案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文