Need Help Searching Array

Menu driven program. Function is to allow the user to enter a fruit type and display the price. Data is loaded into the arrays from a text file and I have already confirmed the data in the arrays is correct by also creating a function to display all fruits and their price. Struggling with the logic of how to determine if (user entered) "apple" = fruit[x] and then outputting the parallel price[x]. Any help is appreciated. If i don't provide enough information feel free to ask and i will try to supplement.

Results in an infinite loop displaying "Fruit doesn't exist"

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
  

#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;

//Global Variables
#define tab2 "\t\t";
#define down3 "\n\n\n";
const int SIZE = 7;	

//Prototypes
void showMenu();
void loadArrays(string fruit[], double price[], int& count);
void showArrays(string fruit[], double price[], int& count);
void lookUpPrice(string fruit[], double price[], int& count);

int main()
{
	//Variables Declared in Main
	int choice = 0;
	int count = 0;
	double price[SIZE];
	bool quit = false;
	string fruit[SIZE];

	//Set floating point to 2 places for currency display
	cout << setprecision(2) << fixed;								


	loadArrays(fruit, price, count);

	while (!quit)	//Menu option #3 changes bool to true
	{
		showMenu();									

		cout << "\nEnter your selection: ";
		cin >> choice;

		switch (choice)								
		{
		case 1:
			showArrays(fruit, price, count);		
			break;
		case 2:
			lookUpPrice(fruit, price, count);		
			break;
		case 3:
			cout << "\nProgram will end";			
			quit = true;
			break;
		default:									
			cout << "\nNot a valid entry. Try again.";
			break;
		}

	}


	cout << down3;
	system("Pause");
	return(0);
}
	
void loadArrays(string fruit[], double price[], int& count)
{
	ifstream input;
	string fileName;

	cout << "\nEnter the name of the file you wish to open: ";
	cin >> fileName;
	input.open(fileName);

	for (int x = 0; x < SIZE; x++)
	{
		getline(input, fruit[x]);
		input >> price[x];
		input.ignore();

		count++;
	}
	input.close();
}
	
void showArrays(string fruit[], double price[], int& count)
{


	for (int x = 0; x < count; x++)
	{
		cout << "Fruit: " << fruit[x] << endl;
		cout << "Price: $" << price[x] << endl;
	}
}

void showMenu()
{

	cout << "\nPlease select an option from the following menu: " << endl;
	cout << "\n\t1 - Display all fruits and prices";
	cout << "\n\t2 - Look up price of specific fruit";
	cout << "\n\t3 - Exit the program";

}

void lookUpPrice(string fruit[], double price[], int& count)
{


	string searchedFruit;
	price = 0;
	
	cout << "\nEnter the name of the fruit you want the price of: ";
	cin >> searchedFruit;
	
	for (int x = 0; x < count; x++)
	{									
		if (searchedFruit == fruit[x])		
		{
			price = price[x];
			
			//cout << "\nPrice: $" << price[x];
		}
		else
		{
			cout << "\nThat fruit does not exist in this list. Try again.";
		}
	}

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void lookUpPrice(string fruit[], double price[], int& count)
{
        bool exist = false; // Set a flag here
	string searchedFruit;
	price = 0;
	
	cout << "\nEnter the name of the fruit you want the price of: ";
	cin >> searchedFruit;
	
	for (int x = 0; x < count; x++)
	{									
		if (searchedFruit == fruit[x])		
		{
			price = price[x];
			exist = true;
		}
	}
        if(!exist){
            cout << "Fruit does not exist in this list. Try again.";
        } 

}


I think the problem with your code was the if / else. The way your if / else was structured printed out "That fruit does not exist" every time you check each index of the array instead of iterating through the whole array before deciding if the fruit exist.
@Hengry - It never occurred to me to create a bool to determine if the item was found in the array. Thank you! gonna implement something like this in my code and see how it goes.

The conditional is correct though, right? like, that is the way to determine if a user searched string matches an item within that array?
> The conditional is correct though, right?
> that is the way to determine if a user searched string matches an item within that array?

Yes.
(You may want to make the comparison case-insensitive ie. 'Apple' matches 'apple')


One the fruit has been located, and its price printed out, we can return from the function immediately. There is no need to continue iterating till the end of the list is reached.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void lookUpPrice( const std::string fruit[], const double price[], int count ) // const
{
    std::string searchedFruit;
    std::cout << "\nEnter the name of the fruit you want the price of: ";
    std::cin >> searchedFruit;

    for( int i = 0; i < count; ++i )
    {
        if( searchedFruit == fruit[i] )
        {
            std::cout << "price: " << price[i] << '\n' ;
            return ; // nothing more to be done; we got what we were looking for
        }
    }

    // if we got here, the searched for fruit was not found
    std::cout << "Fruit '" << searchedFruit << "' does not exist in this list\n";
}
closed account (48T7M4Gy)
@aneurysm

http://www.cplusplus.com/forum/beginner/220979/

Don't duplicate posts.
Topic archived. No new replies allowed.