wrong Output for searching specfic item using linear search

Hi...im trying to implement a function called viewItem based on the description below. We are supposed to use structures and use files as well.
View the item
A user is prompted to enter the name of the item she wants to view.
A (case insensitive, preferably) search should be done. If found the
item should be displayed on the screen. If not, an appropriate error
message must be displayed.


this is the structure

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
struct StockItem

{

	string manufacturer;

	string model;

	int year;

	double price;

	int quantity;

};


this is the function i tried to implement using linear search.
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
int linearSearch(string phone, StockItem phoneList[], int SIZE)

 {

    int i;

	for (i=0; i<SIZE; i++)

	{

	   if (phone == phoneList[i].model)

	      return i;

	}

	return -1;

}

void viewItem()

{

	int count;

	const int SIZE = 3;

	StockItem phoneList[SIZE];

	string phone;

	fstream inFile;

	inFile.open("stock.txt", ios::in |ios::app);

	

	for(count=0; count < SIZE; count++)

		{

			inFile >> phoneList[count].manufacturer;

			inFile >> phoneList[count].model;

			inFile >> phoneList[count].year;

			inFile >> phoneList[count].price;

			inFile >> phoneList[count].quantity; 

		}

	cout << "What do you want to view:(blackberry) \n";

	cin >> phone;


	if(linearSearch(phoneList, phone))

		cout << phoneList[count].model << phoneList[count].year << phoneList[count].price  << phoneList[count].quantity << endl;

	else

		cout << "Not available\n";

	inFile.close();



}



this is my output
What do you want to view:(curve8520)
curve8520
Segmentation fault


my output should be blackberry, curve8520, 2011, 1590.00, 4
Im not sure if im on the right track...any help will be appreciated
You shall not use open mode flag app with fiies opened for read. So remove app from statement

inFile.open("stock.txt", ios::in |ios::app);

Function linearSearch is defined having three parameters

int linearSearch(string phone, StockItem phoneList[], int SIZE)

but you call it only with two arguments

if(linearSearch(phoneList, phone))

and moreover even these two arguments do not correspond to the parameters.

Further the function in any case returns non-zero value so this condition

if(linearSearch(phoneList, phone))

does not determine whether the string was found or not.





Last edited on
Topic archived. No new replies allowed.