My search functions are not working.

Hello, I am creating a c++ program for my parents and this code will compile, bring in the data file but my search function isn't working. here is the 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
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <stdlib.h>
#include <cstdlib>

const int maxitem = 400; //setting maxitem number for each catergory
char search_char;


void getdata(char sortletter[maxitem],int number[maxitem], std::string itemname[maxitem],std::string unit[maxitem],double price[maxitem]);
// Import data and store into each parameter. Sortletter refers to a letter that is sorting different types of item
// Number refers to itme number, name refers to name, unit refers to which you unit the items can be bought in
// and price refers to the unit price of each item.
// The importing process is following, 

void search_number(char sortletter[maxitem],int number[maxitem], std::string itemname[maxitem],std::string unit[maxitem],double price[maxitem],char inputletter, int inputnumber,std::string itemoutname, std::string itemoutunit, double itemoutprice);
// uses all the first parameter to confirm, later the additional parameter for outputs


void search_name(char sortletter[maxitem],int number[maxitem], std::string itemname[maxitem],std::string unit[maxitem],double price[maxitem],char inputletter, int inputnumber, std::string inputname, std::string itemoutname, std::string itemoutunit, double itemoutprice);
// basically same as search_number but uses name this time

void search_name_string_change(std::string namesearch, std::string ioname);
//takes the search term and converts it back to capitalized characters to make the search simpler.

char search_name_char_change(char search_char,char new_char);
// changes item sort letter to capital



int main()
{	using namespace std;
    int response;           //Variables
    char itemletter;
    char first_char;
    int  itemnumber;
    string itemname_search;
    string itemoutname; 
    string itemoutunit; 
    double itemoutprice;
    char sortletter[maxitem];
    int number[maxitem]; 
    string itemname[maxitem];
    string unit[maxitem];
    string input_itemname;
    double price[maxitem];
    char shoppinganswer;
    char searchanswer='Y';
    
   
    cout<< "Canamarine Supply Corp, Shopping List/Calculator" << endl;
    cout<<"Welocome!" <<endl;
    cout<<" "<<endl;
    cout<<"Enter '1' to search by name or enter '2' to search by the product number"<<endl;
    cout<<"1) Search By Name. "<<endl;
    cout<<"2) Search By Number. "<<endl; //User Interface
    cin >> response;
    
    
    if(response ==1)
    {cout<<"Enter The Product Name. ";  //Search By name
        cin>>itemname_search;
        search_name_string_change(itemname_search,input_itemname);
    }else if (response ==2)
    {cout <<"Enter the Product Sorting Letter. "; //Search By Number, need capitalization function
            cin >> first_char;
            search_name_char_change(first_char,itemletter);
     cout <<"Enter the Product Number. ";
     cout << itemletter;
        cin >> itemnumber;
    }else do {if (response !=1||response!=2)
    {cout<<"Please enter a valid Integer Value. "; // loop when wrong value is entered
        cin>>response;
    } }while (response == 1 || response == 2);
     // still needs a fix!
    
    getdata(sortletter,number,itemname,unit,price);
    //function call
    

    do
    {search_name(sortletter, number, itemname, unit, price, itemletter,
    itemnumber, input_itemname, itemoutname, itemoutunit, itemoutprice);
        
        cout<< "The Search Result. "<< endl;
        cout<< itemletter << itemnumber << endl;
        cout<<itemoutname<< "  "<<itemoutunit<<"  "<<itemoutprice<<endl; //resutl price
        cout<< "Add to the shopping cart? Y/N";
        cin>> shoppinganswer;
        //need a function call for shopping cart
        cout<< "Search Again? Y/N"<< endl;
        cin >> searchanswer;
            if(searchanswer == 'y' || searchanswer == 'Y')
                {searchanswer = 'Y';}
            else;
    } while (response == 1 && searchanswer == 'Y');

    
    do 
    {search_number(sortletter,number,itemname,unit,price,itemletter,itemnumber,itemoutname,itemoutunit,itemoutprice);
        cout<< "The Search Result. "<< endl;
        cout<< itemletter << itemnumber << endl;
        cout<<itemoutname<< "  "<<itemoutunit<<"  "<<itemoutprice<<endl; //result print
        cout<< "Add to the shopping cart? Y/N";
        cin>> shoppinganswer;
        //need a function call for shopping cart
        cout<< "Search Again? Y/N"<< endl;
        cin >> searchanswer;
            if(searchanswer == 'y' || searchanswer == 'Y')
                {searchanswer = 'Y';}
            else;
    } while (response == 2 && searchanswer == 'Y');
   
  
    
        system("pause");
    return 0;
    }

void getdata(char sortletter[maxitem],int number[maxitem], std::string itemname[maxitem],std::string unit[maxitem],double price[maxitem])
{   using namespace std;
    int i =0;
    ifstream infile1("H:\\CSC\\final project\\datafile.dat");
    do { 
        infile1 >> sortletter[i];
        infile1 >> number[i];
        infile1 >> itemname [i];
        infile1 >> unit [i];
        infile1 >> price [i];
        i++;
        
    } while (i<400);
   

}   

void search_number(char sortletter[maxitem],int number[maxitem], std::string itemname[maxitem],
std::string unit[maxitem],double price[maxitem],char inputletter, int inputnumber,
 std::string itemoutname, std::string itemoutunit, double itemoutprice)

{   using namespace std;
    int i=0;
    do
    {i++;}
    while (inputletter != sortletter[i]);
    inputletter = sortletter [i];
    i=0;
    do
    {i++;}
    while (inputnumber != number [i]);
    inputnumber = number [i];
    itemoutname = itemname [i];
    itemoutunit = unit [i];
    itemoutprice = price [i];
}

void search_name(char sortletter[maxitem],int number[maxitem], std::string itemname[maxitem],
std::string unit[maxitem],double price[maxitem],char inputletter, int inputnumber,
std::string inputname, std::string itemoutname, std::string itemoutunit, double itemoutprice)
{   using namespace std;
    int i=0;
for (i=0; inputname != itemname [i]; i++)
{itemoutname = itemname [i];}
i++;
itemoutname = itemname [i];
inputletter = sortletter [i];
inputnumber = number [i];
itemoutname = itemname [i];
itemoutunit = unit [i];
itemoutprice = price [i];

}

void search_name_string_change(std::string namesearch,std::string ioname) //name conversion
{ using namespace std;
  for (int i=0; i<(namesearch.length()); i++)
	{
		ioname = static_cast<char>(toupper(namesearch[i]));
	}

}

char search_name_char_change(char search_char,char new_char) //letter conversion
{ using namespace std;

  if (search_char == 'v'|| search_char == 'V')
  {new_char = 'V';
  }else   if (search_char == 'm' || search_char == 'M')
  {new_char = 'M';
  }else   if (search_char == 'f' || search_char == 'F')
  {new_char = 'F';
  }else   if (search_char == 's' || search_char == 'S')
  {new_char = 'S';
  }else   if (search_char == 'g' || search_char == 'G')
  {new_char = 'G';
  }else;
  return (new_char);
}

the function names are "search_name" and "search_number"
Last edited on
Please wrap your code [code]inside code tags[/code] to give it syntax highlighting. It's is very hard to read it without that.
sorry, I saw that and wrapped in in code tag. I apologize I am a fairly new member and didn't really know how to until 5 minutes after posting. Now it's fixed!
cstdlib is the c++ equal to stdlib.h so you don't need stdlib.h
Okay, thanks for that tip, but I don't its really the library problem here I think it's the syntax of the search function.... GAH!
go to void search_name_string_change you don't need using namespace std there it goes with the header files outside of any function
okay you actually have it at the start of everything just use it once (once again outside of everything at the beginning) and you don't have to use std:: try that
there are many logical error in the function search_name
Can I recommend that instead of returning all the individual values from this function that you simply return the index of the one you found?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void search_name(char sortletter[maxitem],int number[maxitem], std::string itemname[maxitem],
std::string unit[maxitem],double price[maxitem],char inputletter, int inputnumber,
std::string inputname, std::string itemoutname, std::string itemoutunit, double itemoutprice)
{   using namespace std;
    int i=0;
for (i=0; inputname != itemname [i]; i++)
{itemoutname = itemname [i];}
i++;
itemoutname = itemname [i];
inputletter = sortletter [i];
inputnumber = number [i];
itemoutname = itemname [i];
itemoutunit = unit [i];
itemoutprice = price [i];

}

As you have it, you are passing everything but the kitchen sink in as parameters. But you only really need to pass in the array to search and the value to search for:
1
2
3
4
5
6
7
8
9
10
11
12
13
/**
 * This function will search for the inputname in the array itemname[].
 *
 * It will return the index of the found name, otherwise it will return
 * the value maxitem if the name is not found.
 **/
int search_name(std::string itemname[maxitem], std::string inputname)
{
	using namespace std;

	for(int i = 0; i < maxitem && inputname != itemname[i]; ++i); // ; means no body
	return i;
}

Then you can use that function like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
do
{
	int i = search_name(itemname, input_itemname);
	if(i != maxitem)
	{
		cout << "The Search Result. " << endl;
		cout << sortletter[i] << number[i] << endl;
		cout << itemname[i] << "  " << unit[i] << "  " << price[i] << endl; //resutl price
		cout << "Add to the shopping cart? Y/N";
		
		// ... etc
	}
} while (response == 1 && searchanswer == 'Y');

Also, instead of holding your product data in several different arrays you might want to keep them together in a struct. And you might also consider using a std::vector rather than using an array:
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
#include <string>
#include <vector>
#include <fstream>

struct product
{
	char sortletter;
	int number;
	std::string itemname;
	std::string unit;
	double price;
};

std::vector<product> data;

void getdata(std::vector<product>& data)
{
	data.clear();
	std::ifstream infile("H:\\CSC\\final project\\datafile.dat");
	product p;
	while(infile >> p.sortletter >> p.number >> p.itemname >> p.unit >> p.price)
	{
		data.push_back(p);
	}
}


struct
http://cplusplus.com/doc/tutorial/structures/

std::vector
http://cplusplus.com/reference/stl/vector/
So, I took the advice on that integer function-which I think is brilliant- is only half working for me right now. I think my syntax is wrong again, could you check the function?

1
2
3
4
5
6
7
8
9
10
11
12
int search_name(std::string itemname[maxitem], std::string inputname, int search_resp_par)
{
	using namespace std;
	int i;
	int j=0;
    int maxitem = 400;

	for(i = 0;( (i < maxitem) && (inputname != itemname[i])); ++i);
     {j++;}
    search_resp_par = j;// ; means no body
	return search_resp_par;
}
Actually it seems that it's my other void function that isn't outputing anything. check please?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
char search_name_char_change(char search_char,char new_char) //letter conversion
{ using namespace std;

  if (search_char == 'v'|| search_char == 'V')
  {new_char = 'V';
  }else   if (search_char == 'm' || search_char == 'M')
  {new_char = 'M';
  }else   if (search_char == 'f' || search_char == 'F')
  {new_char = 'F';
  }else   if (search_char == 's' || search_char == 'S')
  {new_char = 'S';
  }else   if (search_char == 'g' || search_char == 'G')
  {new_char = 'G';
  }else;
  return (new_char);
}


1
2
3
4
5
6
7
8
void search_name_string_change(std::string namesearch,std::string ioname) //name conversion
{ using namespace std;
  for (int i=0; i<(namesearch.length()); i++)
	{
		ioname = static_cast<char>(toupper(namesearch[i]));
	}

}
ioname is passed by value; changing it in the function has no affect on it outside of the function.

Your second function has a net effect of nothing. Also, you should get rid of that extraneus using namespace std; as it is quite useless.
I think you are trying to duplicate in the code things that it already does:
1
2
3
4
5
6
7
8
9
10
11
12
int search_name(std::string itemname[maxitem], std::string inputname, int search_resp_par)
{
	using namespace std;
	int i;
	int j=0;
    int maxitem = 400;

	for(i = 0;( (i < maxitem) && (inputname != itemname[i])); ++i);
     {j++;}
    search_resp_par = j;// ; means no body
	return search_resp_par;
}

You don't need to pass in search_resp_par. It doesn't server any purpose.
1
2
3
4
5
6
7
8
9
10
11
12
int search_name(std::string itemname[maxitem], std::string inputname)
{
	using namespace std;
	int i;
	int j=0;
    int maxitem = 400;

	for(i = 0;( (i < maxitem) && (inputname != itemname[i])); ++i);
     {j++;}
   
	return j;
}

Also you don't need j because it is the same value as i
1
2
3
4
5
6
7
8
9
10
11
12
int search_name(std::string itemname[maxitem], std::string inputname)
{
	using namespace std;
	int i;
    int maxitem = 400;

	for(i = 0;( (i < maxitem) && (inputname != itemname[i])); ++i)
	{
	}
   
	return i;
}

Try that and see if it works.
You can do this one of two ways:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
char search_name_char_change(char search_char,char new_char) //letter conversion
{ using namespace std;

  if (search_char == 'v'|| search_char == 'V')
  {new_char = 'V';
  }else   if (search_char == 'm' || search_char == 'M')
  {new_char = 'M';
  }else   if (search_char == 'f' || search_char == 'F')
  {new_char = 'F';
  }else   if (search_char == 's' || search_char == 'S')
  {new_char = 'S';
  }else   if (search_char == 'g' || search_char == 'G')
  {new_char = 'G';
  }else;
  return (new_char);
}

The first is to return the new char
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// input char, return converted char
char search_name_char_change(char search_char)
{ using namespace std;

  char new_char; // declare it here NOT in the parameters
  if (search_char == 'v'|| search_char == 'V')
  {new_char = 'V';
  }else   if (search_char == 'm' || search_char == 'M')
  {new_char = 'M';
  }else   if (search_char == 'f' || search_char == 'F')
  {new_char = 'F';
  }else   if (search_char == 's' || search_char == 'S')
  {new_char = 'S';
  }else   if (search_char == 'g' || search_char == 'G')
  {new_char = 'G';
  }else;
  return (new_char);
}

// ...

// use it like this
itemletter = search_name_char_change(first_char); 

The second is to pass in a reference:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void search_name_char_change(char search_char,char& new_char)
{ using namespace std;

  if (search_char == 'v'|| search_char == 'V')
  {new_char = 'V';
  }else   if (search_char == 'm' || search_char == 'M')
  {new_char = 'M';
  }else   if (search_char == 'f' || search_char == 'F')
  {new_char = 'F';
  }else   if (search_char == 's' || search_char == 'S')
  {new_char = 'S';
  }else   if (search_char == 'g' || search_char == 'G')
  {new_char = 'G';
  }else;
}

// ...

// use it like this
search_name_char_change(first_char, itemletter);

this function is still not working
1
2
3
4
5
6
7
8
9
int search_name(std::string itemname[maxitem], std::string inputname)
{
	using namespace std; 
	int i;
for(i=0; (inputname!=itemname[i]); ++i)
{
}
	return i;
}

it's keep returning value 81. anyone know why?
Well you don't check if you run out of names to check. What happens if the name is not found?
UGH! I was plugging in wrong value to the function! No wonder why it didn't work! Thank you so much everyone.

Lesson learned? If it doesn't work check from beginning.
Topic archived. No new replies allowed.