Change Karnaugh Map simplifier from SOP to POS result - c++

It's my program to simplify Karnough map. I need to change it from SOP to POS for university project. I changed already signs from + to * and from * to +. All i have to do now is to make it read zero-s from text file instead of one's. i tried everything to solve that problem, but i have no idea. I have got two txt files, one with input and one with output. At this moment i need to put in input 0's instead of 1's and in the other side to, to make it work. Please help me, my life depends of that
#include <iostream>
#include <fstream>
#include <cmath>
#include <windows.h>
using namespace std;
//?????
int pow2(int n) // ??2?????,2^n
{
int result=1
;
while(n>0)
result*=2,n--;
return result;
}
int combination(int n,int r) // ?????C_n^r,? n! / ((n-r)!*(r)!)
{
int fm=1,fz=1;
for(int i=1;i<=r;i++,n--)
{
fm*=i;
fz*=n;
}
return fz/fm;
}
void d2b(int d,char* b,int n) // ???????,??????d???????????b?
{
for(n--;n>=0;n--)
{
b[n]='0'+d%2;
d/=2;
}
}
int ispair(char* a1,char* a2,int n) // ????????????????
{
int x,y=0;
for(int i=0;i<n;i++)
if(a1[i]!=a2[i])
x=i,y++;
if(y==1) return x;
else return-1;
}
bool issame(char* a1,char* a2,int n) // ????
{
for(int i=0;i<n;i++)
if(a1[i]!=a2[i])
return 0;
return 1;
}
int left1(char* a,int N) // ???????????,??????1,??????
{
for(int i=0;i<N;i++)
if(a[i]=='1')
return i;
return -1;
}
void copy(char* a1,char* a2,int n) // ??????
{
for(int i=0;i<n;i++)
a2[i]=a1[i];
}
bool isinside(int x,char* a,int n) // ???x????????a?
{
for(n--;n>=0;n--)
{
if(a[n]!='x' && a[n]!=(char)('0'+x%2))
return 0;
x/=2;
}
return 1;
}
void output(fstream& file,char* a,int n)
{
for(int i=0;i<n;i++)
{
if(i==0)file<<'(';
if(a[i]=='0'||a[i]=='1')
{
if(a[i]=='0') file<<'¬'<<(char)('a'+i);
if(a[i]=='1') file<<(char)('a'+i);
if(a[i+1]=='0'||a[i+1]=='1'||a[i+2]=='0'||a[i+2]=='1'||a[i+3]=='0'||a[i+3]=='1')
{
file<<'+';
}
else{
file<<')';
}
}
else{}
;
}
file<<'*';
}
int count(char* table,char* a,int n,int N) // ??,????????1???????????a?
{
int counter=0;
for(int i=0;i<N;i++)
{
if(table[i]!='1') continue;
if(isinside(i,a,n)) counter++;
}
return counter;
}
void clean(char* table,char* a,int n,int N) // ??????????????????1,???x
{
for(int j=0;j<N;j++)
if(isinside(j,a,n))
table[j]='x';
}
int main()
{
SetConsoleTitle("???-???????????");
system("color 06");
//????
fstream inputFile("input.txt",ios::in); //??????????input.txt
int valNum; //???
inputFile >> valNum; // ???????????
cout<<"\n ????????: "<<valNum;
cout<<"\n ?????????:\n";
int minTermLength=pow2(valNum); // ??????????2^valNum?
char* minTermExpression=new char[minTermLength]; // ???????????????
int lineOff = pow2(ceil(double(valNum)/2));
// ???????
if (inputFile.is_open())
{
for(int i=0;i<minTermLength;i++) // ?????
{
inputFile>>minTermExpression[i];
if(i%lineOff == 0&&(i!=0))
cout<<"\n";
cout<<"\t"<<minTermExpression[i];
}
inputFile.close();
}
// ??????
else{
cout<<"\n ??input.txt??,???????";
return 0;
}
// ?implication????????
char*** implication=new char**[valNum]; // ????
int nonZeroNum=1;
for(int i=0;i<minTermLength;i++)
if(minTermExpression[i]!='0') // ?????ON???DC?????????
nonZeroNum++;
for(int i=0;i<valNum;i++) // i-??,???????3?,i=0???????,i=1???,i=2???
{
if(pow2(i)>nonZeroNum)break;
int x=pow2(i-1)*combination(nonZeroNum,pow2(i));
implication[i]=new char*[x];
for(int j=0;j<x;j++)
implication[i][j]=new char[valNum];
}
//?????????
int* countNum=new int[valNum+1];
countNum[0]=0;
for(int i=0;i<minTermLength;i++)
if(minTermExpression[i]!='0') // ???0?
{
d2b(i,implication[0][countNum[0]],valNum); // ??????????implication[0]???
countNum[0]++;
}
int isOptimal=0;
while(countNum[isOptimal]>0) // ??????????
{
countNum[isOptimal+1]=0;
for(int i=0;i<countNum[isOptimal]-1;i++)
for(int j=i+1;j<countNum[isOptimal];j++)
{
int x=ispair(implication[isOptimal][i],implication[isOptimal][j],valNum); // ??????????
if(x==-1) continue;
copy(implication[isOptimal][i],implication[isOptimal+1][countNum[isOptimal+1]],valNum);// ???implication??
implication[isOptimal+1][countNum[isOptimal+1]][x]='x'; // ??????????,?????????x,???????????
countNum[isOptimal+1]++;
}
for(int i=0;i<countNum[isOptimal+1]-1;i++)
for(int j=i+1;j<countNum[isOptimal+1];j++)
if(issame(implication[isOptimal+1][i],implication[isOptimal+1][j],valNum)) // ??????
{
for(int k=j;k<countNum[isOptimal+1]-1;k++)
copy(implication[isOptimal+1][k+1],implication[isOptimal+1][k],valNum);
countNum[isOptimal+1]--;
}
isOptimal++;
}
isOptimal--;
//???????
fstream outputFile("output.txt",ios::out);
outputFile<<"F=";
while(left1(minTermExpression,minTermLength)>=0) //?minTermExpression???1
{
bool flag=0; // ???,????
for(int i=0;i<minTermLength&&flag==0;i++)
{
if(minTermExpression[i]!='1') continue;
int counter=0,recorder;
for(int j=0;j<countNum[isOptimal];j++)
if(isinside(i,implication[isOptimal][j],valNum))
counter++,recorder=j;
if(counter!=1) continue;
output(outputFile,implication[isOptimal][recorder],valNum);
clean(minTermExpression,implication[isOptimal][recorder],valNum,minTermLength);
flag=1;
}
if(flag==1) continue;
int termMaxInclude=0;
int recorder=0;
for(int i=0;i<countNum[isOptimal];i++) // ??????????,???????????1???,??????
if(count(minTermExpression,implication[isOptimal][i],valNum,minTermLength)>termMaxInclude)
termMaxInclude=count(minTermExpression,implication[isOptimal][i],valNum,minTermLength),recorder=i;
if(termMaxInclude==0) {isOptimal--; continue;}
output(outputFile,implication[isOptimal][recorder],valNum);
clean(minTermExpression,implication[isOptimal][recorder],valNum,minTermLength);
}
outputFile.close();
// ?????
outputFile.open("output.txt",ios::in);
char finalExpression[201];
outputFile.getline(finalExpression,200);
outputFile.close();
int termMaxInclude=0;
for(;finalExpression[termMaxInclude]!='\0';termMaxInclude++);
finalExpression[termMaxInclude-1]='\0';
outputFile.open("output.txt",ios::out);
outputFile<<finalExpression;
outputFile.close();
cout<<"\n??????????????";
cout<<"\n ??????,??????!";
cin.get();
return 0;
}

The zeros are simply those not in the ones list.

Related

Finding next palindrome of a number, getting SIGABRT error

Hi ,I am trying to solve the question PALIN on SPOJ where the idea is to find the next immediate palindrome of a number. I tested my code on the IDE with all possible test cases I found and the program is working fine but I can't figure out why I am getting SIGABRT error when I submit the solution.Please I request someone help me figure out the problem in the code For yor reference I am providing the link of the question:- https://www.spoj.com/problems/PALIN/
#include<iostream>
#include<stdio.h>
#include<vector>
#include<string>
using namespace std;
bool all_nines(string s)
{
for(int i=0;i<s.length();i++)
{
if(s[i]!='9')
return false;
}
return true;
}
int reverse(int a)
{
int rev=0;
while(a)
{
rev=(rev*10)+(a%10);
a/=10;
}
return rev;
}
void convert_and_compare(string &s,int low,int high)
{
int low_num,high_num;
string low_str="",high_str="";
for(int i=0,j=high;i<=low,j<s.size();i++,j++)
{
low_str+=s[i];
high_str+=s[j];
}
low_num=stoi(low_str);
high_num=stoi(high_str);
if(reverse(low_num)<=high_num)
{
low_num++;
low_str=to_string(low_num);
high_str="";
for(int i=low_str.length()-1;i>=0;i--)
high_str+=low_str[i];
}
else
{
high_str="";
for(int i=low_str.length()-1;i>=0;i--)
high_str+=low_str[i];
}
s="";
s+=low_str+high_str;
}
void convert_and_compare(string &s,int low,int high,int mid)
{
int low_num,high_num,mid_num;
string low_str="",high_str="";
char mid_str;
for(int i=0,j=high;i<=low,j<s.size();i++,j++)
{
low_str+=s[i];
high_str+=s[j];
}
low_num=stoi(low_str);
high_num=stoi(high_str);
mid_num=(s[mid]-'0');
if(reverse(low_num)>high_num)
{
low_str=to_string(low_num);
high_str="";
for(int i=low_str.length()-1;i>=0;i--)
high_str+=low_str[i];
mid_str=mid_num+'0';
}
else if(reverse(low_num)<=high_num && mid_num!=9)
{
mid_num++;
high_str="";
for(int i=low_str.length()-1;i>=0;i--)
high_str+=low_str[i];
mid_str=mid_num+'0';
}
else if(reverse(low_num)<=high_num && mid_num==9)
{
mid_num=0;
low_num++;
low_str=to_string(low_num);
high_str="";
for(int i=low_str.length()-1;i>=0;i--)
high_str+=low_str[i];
mid_str=mid_num+'0';
}
s="";
s=low_str+mid_str+high_str;
}
string find_next_palin(string s)
{
if(all_nines(s))
{
s[0]='1';
for(int i=1;i<s.length();i++)
s[i]='0';
s+='1';
return s;
}
else
{
if(s.length()%2==0)
{
int low,high;
low=s.length()/2-1;
high=s.length()/2;
convert_and_compare(s,low,high);
return s;
}
else
{
int low,high,mid;
mid=s.length()/2;
low=mid-1;
high=mid+1;
convert_and_compare(s,low,high,mid);
return s;
}
}
}
int main()
{
int t;
scanf("%d",&t);
vector <string> v(t);
for(int i=0;i<t;i++)
{
cin>>v[i];
}
for(int i=0;i<t;i++)
{
if(v[i].length()==1)
{
if(v[i]=="9")
{ printf("11");
printf("\n");
}
else
{
printf("%d",(v[i][0]-'0')+1);
printf("\n");
}
}
else
{
string temp;
temp=find_next_palin(v[i]);
for(int j=0;j<temp.length();j++)
cout<<temp[j];
printf("\n");
}
}
return 0;
}

Tried making a hash table, can't map all keys, also program crashes

I have to make a program with a hash table that maps single random characters into the table. The program kind of works but sometimes it crashes, also it doesn't map every element. Some of them just won't get inside the table and there are always spare spaces in the table. I don't know what to do to solve these 2 problems. I used 3 versions of open adressing and each of them causes the same 2 problems. Sorry for my bad English. Thank you in advance.
Edited. Of course, I forgot about dynamic allocation. But the problem isn't solved.
#include <time.h>
#include <string>
#include <cstdlib>
using namespace std;
int Liniowo(int i, int proby, int rozmiar) // (open adressing, Linear probing)
{
if(i+proby<rozmiar)
return i+proby;
else
{
return -1;
}
}
int Kwadratowo(int i, int proby, int rozmiar) // (open adressing, Quadratic probing)
{
if (i+proby*proby<rozmiar)
return i+proby*proby;
else
{
return -1;
}
}
int Podwojnie(int i, int proby, int rozmiar, char klucz) // (open adressing, Double hashing)
{
if (i*(klucz*(701%klucz)-klucz%13)<rozmiar&&i*(klucz*(701%klucz)-klucz%13)>0)
return i*(klucz*(701%klucz)-klucz%13);
else
{
return -1;
}
}
int modularnie(char c,int rozmiar) // modular
{
return c%rozmiar;
}
void dodaj(char *tab,int max, char c) // add an element
{
int i=modularnie(c, max);
if (tab[i]== '\0')
tab[i]=c;
else
{
int u=0;
int h;
while (tab[i]!= '\0'&&h!=-1)
{
u++;
// h=Kwadratowo(i, u, max);
h=Podwojnie(i,u,max,c);
}
if (h!=-1)
tab[h]=c;
else
cout << "no niestety, nie udalo sie wstawic " <<endl; //"I couldn't map the element"
}
}
int wyszukaj(char *tab,int max, char c) // search an element
{
int i=modularnie(c, max);
int j=i;
if (tab[i]== '\0')
return -1;
while (tab[i]==c)
{
i=(i+1)%max;
if((i==j)||(tab[i]== '\0'))
return -1;
}
return i;
}
int usun(char *tab,int max, char c) // remove an element
{
int r,j,i=wyszukaj(tab,max,c);
j=i;
if (i==-1)
return -1;
tab[i]= '\0';
while (tab[(++i)%max]!= '\0')
{
i%=max;
r=modularnie(tab[i],max);
if (((i<r)&&(r<=j)) || ((r<=j)&&(j<i)) || ((j<i)&&(i<r)))
{
tab[j]=tab[i];
tab[i]= '\0';
j=i;
continue;
}
}
return 0;
}
int main()
{
srand( time( NULL ) );
int ile;
cout << "podaj wielkosc tablicy: "; //"Type the size of the table"
cin >> ile;
char* tab; // EDITED
tab=new char(ile);
for (int n=0; n<ile; n++)
{
tab[n]= '\0';
}
char e;
for (int i=0; i<ile; i++)
{
e='!'+rand()%127;
dodaj(tab, ile, e);
}
for(int j=0; j<ile; j++)
{
cout << j << ", " << tab[j] << endl;
}
return 0;
}

why the below merge sort code doesnt work?

What should be changed in the below code? It shows the following error
ERROR: ld.so: object '/home/bot/funcs.so' from LD_PRELOAD cannot be preloaded (cannot open shared object file): ignored.
Error in `/home/bot/dcb7b4b8f54571a4467c2113b7856878': free(): invalid next size (fast): 0x0000000000c350b0
#include <iostream>
using namespace std;
int merge(int *left,int nl,int *right,int nr,int *a)
{
int i=0,j=0,k=0;
while(i<nl && j<nr)
{
if(left[i]<right[j])
{
a[k]=left[i];
i++;
}
else
{
a[k]=right[j];
j++;
}
k++;
}
while(i<nl)
{
a[k]=left[i];
i++;
}
while(j<nr)
{
a[k]=right[j];
j++;
}
}
int mergesort(int *a,int n)
{
if(n<2) return 0;
int mid=n/2;
int *left=new int[mid];
int *right=new int[n-mid];
for(int i=0;i<=mid-1;i++)
{
left[i]=a[i];
}
for(int i=mid;i<=n-1;i++)
{
right[i]=a[i];
}
mergesort(left,mid);
mergesort(right,n-mid);
merge(left,mid,right,n-mid,a);
delete[]left;
delete[]right;
}
int main() {
//code
int a[]={10,7,8,9,4,2,3,6,5,1};
int n=sizeof(a)/sizeof(a[0]);
mergesort(a,n);
for(int i=0;i<10;i++)
{
cout<<a[i]<<"\t";
}
return 0;
}
Fixes noted in comments, seems to be working now. A one time allocation of a temp array either in main or in a helper function would be faster. Using mutually recursive functions (one merges AtoA, the other merges AtoTemp, they call each other), eliminates having to copy data. I can post an example later if wanted. Bottom up merge sort would be slightly faster.
#include <iostream>
using namespace std;
// fix return type to void
void merge(int *left,int nl,int *right,int nr,int *a)
{
int i=0,j=0,k=0;
while(i<nl && j<nr)
{
if(left[i]<right[j])
{
a[k]=left[i];
i++;
}
else
{
a[k]=right[j];
j++;
}
k++;
}
while(i<nl)
{
a[k]=left[i];
i++;
k++; // fix
}
while(j<nr)
{
a[k]=right[j];
j++;
k++; // fix
}
}
// fix return type to void
void mergesort(int *a,int n)
{
if(n<2) return; // fix: change return type to void
int mid=n/2;
int *left=new int[mid];
int *right=new int[n-mid];
for(int i=0;i<=mid-1;i++)
{
left[i]=a[i];
}
for(int i=mid;i<=n-1;i++)
{
right[i-mid]=a[i]; // fix
}
mergesort(left,mid);
mergesort(right,n-mid);
merge(left,mid,right,n-mid,a);
delete[]left;
delete[]right;
}
int main() {
//code
int a[]={10,7,8,9,4,2,3,6,5,1};
int n=sizeof(a)/sizeof(a[0]);
mergesort(a,n);
for(int i=0;i<10;i++)
{
cout<<a[i]<<"\t";
}
cout << endl; // added this not needed
return 0;
}
Cleanup, changed output to not use tabs:
#include <iostream>
#include <iomanip> // for std::setw()
using namespace std;
void merge(int *left,int nl,int *right,int nr,int *a)
{
int i=0,j=0,k=0;
while(i<nl && j<nr)
{
if(left[i]<right[j])
a[k++]=left[i++];
else
a[k++]=right[j++];
}
while(i<nl)
a[k++]=left[i++];
while(j<nr)
a[k++]=right[j++];
}
void mergesort(int *a,int n)
{
if(n<2)
return;
int mid=n/2;
int *left=new int[mid];
int *right=new int[n-mid];
for(int i=0;i<=mid-1;i++)
left[i]=a[i];
for(int i=mid;i<=n-1;i++)
right[i-mid]=a[i];
mergesort(left,mid);
mergesort(right,n-mid);
merge(left,mid,right,n-mid,a);
delete[]left;
delete[]right;
}
int main() {
int a[]={10,7,8,9,4,2,3,6,5,1};
int n=sizeof(a)/sizeof(a[0]);
mergesort(a,n);
for(int i=0;i<10;i++)
cout<<setw(2)<<a[i]<<" "; // 2 digit field output
cout << endl;
return 0;
}
You should use std::vector instead of raw pointer:
typedef std::vector<int> ivector;
void merge( const ivector &l, const ivector &r, ivector &to )
{
ivector_const::iterator il = l.begin();
ivector_const::iterator ir = r.begin();
ivector::iterator ito = to.begin();
while( true ) {
if( il == l.end() ) {
if( ir == r.end() )
return;
*ito++ = *ir++;
} else {
if( ir == r.end() || *il < *ir )
*ito++ = *il++;
else
*ito++ = *ir++;
}
}
}
void mergesort( ivector &v )
{
if(v.size()<2) return;
ivector::iterator imid = v.begin() + v.size() / 2;
ivector left( v.begin(), imid );
ivector right( imid, v.end() );
mergesort(left);
mergesort(right);
merge( left, right, v );
}
int main() {
//code
ivector a ={10,7,8,9,4,2,3,6,5,1};
mergesort(a);
for(size_t i=0;i<a.size();i++)
{
cout<<a[i]<<"\t";
}
return 0;
}
Note: I did not validate your algorithm just rewrote it with std::vector instead of raw pointer. You can notice how simpler your function become when you use proper data type.

Wrong Answer http://www.spoj.com/problems/WORDS1/

I have written the following code for the above problem.
I have checked if Graph is connected by choosing the start node and doing DFS from it. And checking the conditions for Euler path and tour, in code that would be count= 2 or 0. It's working on all given test cases.But getting wrong answer on submission
#include<iostream>
#include<cmath>
#include<list>
#include<string>
#include<stack>
using namespace std;
class Graph
{
int V;
list<int> *adj;
int *in;
public:
Graph(int v)
{
this->V=v;
this->adj=new list<int>[V];
this->in=new int[V];
for(int i=0;i<V;i++)
{
in[i]=0;
}
}
void addEdge(int src,int des)
{
adj[src].push_back(des);
in[des]=in[des]+1;
}
void DFSUtil(bool visited[],int v)
{
stack<int> s;
s.push(v);
visited[v]=true; //mark v as visited;
while (!s.empty())
{
int top=s.top();
s.pop();
list<int> :: iterator it;
for(it=adj[top].begin();it!=adj[top].end();it++)
{
if(!visited[*it])
{
visited[*it]=true;
s.push(*it);
}
}
}
}
/*void DFSUtil(bool visited[],int v)
{
visited[v]=true;
list<int> :: iterator it;
for(it=adj[v].begin();it!=adj[v].end();it++)
{
if(!visited[*it])
{
DFSUtil(visited,*it);
}
}
}*/
// Graph reverseGraph()
// {
// Graph g(V);
// for(int i=0;i<V;i++)
// {
// list<int> :: iterator it;
// for(it=adj[i].begin();it!=adj[i].end();it++)
// {
// g.addEdge(*it,i);
// }
// }
// return g;
// }
bool isConnected()
{
//bool visited[V];
bool* visited=new bool[V];
for(int i=0;i<V;i++)
visited[i]=false;
int i=0;
int flag=0;
int n;
for(i=0;i<V;i++)
{
if(adj[i].size()>0)
{
n=i;
flag=1;
}
if(((int)adj[i].size()-in[i])==1 && in[i]==0) //selecting the start vertex i.e vertex with no incoming edges
{
n=i;
break;
}
}
if(i==V&&flag==0)
return 0;
DFSUtil(visited,n); //dfs to check if every node is reachable fro start vertex
for(int i=0;i<V;i++)
{
if(visited[i]==false && adj[i].size()>0)
return 0;
}
// Graph gr=reverseGraph();
// for(int i=0;i<V;i++)
// visited[i]=false;
// gr.DFSUtil(visited,n);
// for(int i=0;i<V;i++)
// {
// if(visited[i]==false && adj[i].size()>0)
// return 0;
// }
return 1;
}
bool isEuler()
{
int count=0;
int magnitude;
for(int i=0;i<V;i++) //check conditions on in and out edges, out edges=adj[].size
{
magnitude=in[i]-(int)adj[i].size();
if(magnitude<0)
magnitude=magnitude*-1;
if((magnitude)==1)
{
count=count+1;
}
else if(in[i]!=adj[i].size())
{
return 0;
}
}
if(count==1 || count>2)
return 0;
if(isConnected()==0) //check if graph is connected
return 0;
return 1;
}
};
int main()
{
int t;
//scanf("%d",&t);
cin>>t;
while(t--)
{
int n;
//scanf("%d",&n);
cin>>n;
string str;
if(n==1) //only one string entered
{
cin>>str;
cout<<"Ordering is possible.\n";
continue;
}
Graph g(26);
int src,des;
for(int i=0;i<n;i++)
{
cin>>str;
src=str[0]-'a';
des=str[str.length()-1]-'a';
g.addEdge(src,des);
}
if(g.isEuler())
{
cout<<"Ordering is possible.\n";
}
else
{
cout<<"The door cannot be opened.\n";
}
}
}
In your function isConnected, you attempt to find the start vertex with no incoming edges by breaking out of the for loop upon a certain condition. If this condition is not encountered, the loop variable i will contain the value 26, rather than an expected index in the range 0..25. When the value 26 is passed to the DFSUtil function it will caused an out of bounds array access. I get a crash on the line visited[v] = true;
A condition that will cause this to arise, is the test case from the linked site with the repeated word:
2
ok
ok
Your check to find the start vertex does not handle this case well.

Boolean function not giving right answer

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;
}
You forgot to return false at the end of your check_gift function
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;
}
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.
You need to add return false at the end of check_gift().