problem with return

hi, i need your help ....
I want to coding find function in my class, that search in array of item and return 1 if it is found the item and return 0 if it is not found
here my code but alaways return 0 :(

1
2
3
4
5
6
7
8
9
10
11
12
13
14
//implementation file 
 bool bag::find(string item_name)
{
	
	
	for(int i=0;i<5;i++)
	 
	
		if(arr[i]==item_name)
		return 1;

		
	  else 
			return 0;



1
2
3
//main 
string a[5]={"pens","books","glasses","Ipad","Headphones"};
cout<<find(item_name);

Do you have somewhere in your code item_name = "name of one of the items"?
Also you called you string array a[5] but in the if look you are looking at arr[i], which is a different array?
Last edited on
mmmm , ok i want to show you my code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
//header file 
#ifndef BAG_H
#define BAG_H
#include<iostream>
#include<string>
using namespace std;

class bag
{
public:
	bag(string ,string,double,string[5]);
	~bag();
	void print();
	bool find(string);
	bool cheaper();
	void setbag(string,string,double,string[5]);
	void getbag(string& ,string&,double&,string[5]);

private :
	string name;
	string type;
	double price;
	string arr[5];

};
#endif 



--------------
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
//implementation file
#include"bag.h"


bag::bag(string n,string t,double p,string a[5])
{
	name=n;
	type=t;
	price=p;
	for(int i=0;i<5;i++)
		arr[i]=a[i];
}
bag::~bag()
{
	cout<<name<<endl;
}
void bag::print()
{
	cout<<"The Bag Brand is:  "<<name<<endl<<"The Type of the Bag is:   "<<type<<endl<<"The Price of the Bag is:   "<<price
		<<endl;
	cout<<"The item is :  ";
	for(int i=0;i<5;i++)

	{cout<<arr[i];
	cout<<endl;}
}
bool bag::find(string item_name)
{
	
	
	
	for(int i=0;i<5;i++)
		
	
		if(arr[i]==item_name)
		return 1;

		
	  else 
		  
			return 0;
		
		
	
	
}

void bag::setbag(string n,string t,double p,string a[5])
{
	name=n;
	type=t;
	price=p;
	for(int i=0;i<5;i++)
		arr[i]=a[i];

}
void bag::getbag(string &n ,string &t,double &p,string a[])
{
	n=name;
	t=type;
	p=price;
	for(int i=0;i<5;i++)
		a[i]=arr[i];
}


---------
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//main 
#include"bag.h"
#include<iostream>
using namespace std;


void main()
{
	
	string a[5]={"pens","books","glasses","Ipad","Headphones"};
		
	 bag c("adiddas" ,"back-bag",50,a);
	 c.print();
	 c.setbag("nike","brifcase",300,a);
	 c.print();
	 c.~bag();
	 
	cout<<c.find("pens");
	
	 

}
Mats he probably used a in his constructor when he created the object so that shouldn't matter. Also his error is probably with that else statement on line 13. He should only return false if none of them are true not if the first element isn't true. So remove line 13 and it should work. Also you should use constant numbers not magic numbers like the 5 on line 6. Is that the max size of the items in the bag or something?
giblit i do what you are saying to me , but same problem,, and this is max size of bag
please help me cuz I tried many time to fix this problem :(
const int max_size = 5;

Do not use void main(). It is always int main().

If I change void main() to int main() (so that I can compile this), that function does return 1 (pens found).
Your search function is broken:
1
2
3
4
5
6
7
8
bool bag::find(string item_name)
{
    for(int i=0;i<5;i++)
        if(arr[i]==item_name)
            return 1;
        else 
            return 0;
}

You see what happens? It only tests arr[0] and then returns either 1 or 0 depending on that first test. You need to search the entire array until either (a) the item is found or (b) the end of the array has been reached without finding a matching item.
Last edited on
That's what I said chervil :P He just needs to remove the else statement.
mmm giblit , i remove else statement but the output is random number like 123

Mats I change void with int , but same error :(

Did you do it like this?
1
2
3
4
5
6
7
8
bool bag::find(string item_name)
{
    for (int i=0; i<5; i++)
        if (arr[i] == item_name)
            return true;
       
    return false;
}
yes , the output alaways 0
Well, that means the item was not found. If the code is correct, the problem must be with the data. Check that the contents of the array, and the value being searched for make sense.
I change the items of array and search again , but it gives me 0 that means same problem . :(

ok, if we try to use while is that correct ?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

class bag {

    string arr[5];
    string type;
    string name;
    double price;
public:
    bag (){};
    bag (string a, string b, double num, string * c);

    bool find(string);
    void print();
};

bag::bag (string a, string b, double num, string * c) 
    : type(b), name(a), price(num)
{
    for (int i=0; i<5;i++)
    {
        arr[i] = c[i];
    }
}

void bag::print()
{
    cout << "The Bag Brand is:  "           << name
         << "\nThe Type of the Bag is:   "  << type
         << "\nThe Price of the Bag is:   " << price
         << endl;
    cout<<"The item is :  \n";
    for (int i=0; i<5; i++)
    {
        cout<<arr[i] << endl;
    }
}

bool bag::find(string item_name)
{
    for (int i=0; i<5; i++)
        if (arr[i] == item_name)
            return true;
       
    return false;
}

int main()
{
    string a[5]={"pens","books","glasses","Ipad","Headphones"};
    
    bag c("adiddas" ,"back-bag",50,a);
    c.print();
    
    cout << boolalpha << c.find("pens");
    
}

Output:
The Bag Brand is:  adiddas
The Type of the Bag is:   back-bag
The Price of the Bag is:   50
The item is :
pens
books
glasses
Ipad
Headphones
true
Thank yoooou so much Chervil
Actually Mohammed the problem you had was you had the unneeded else then in your main program you called the destructor before searching. How can you delete the object then find anything inside of it?
mmmm Thanks giblit my code is now working in a goodway
Topic archived. No new replies allowed.