Searching Arrays and displaying parallel data

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.";
		}
	}

}
Last edited on
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void lookUpPrice(string fruit[], double price[], int& count)
{
    string searchedFruit;
    int found_x = -1;
    
    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])
        {
            found_x = x;
        }
    }
    
    if(found_x == -1)
        cout << "\nThat fruit does not exist in this list. Try again.";
    else
        cout << "\nPrice: $" << price[found_x];
}
closed account (48T7M4Gy)
Another more efficient way:
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
void lookUpPrice(string fruit[], double price[], int& count)
{
    string searchedFruit;
    
    cout << "\nEnter the name of the fruit you want the price of: ";
    cin >> searchedFruit;
    
    bool found = false;
    int index = 0;
    
    while( index < count && !found)
    {
        if(searchedFruit == fruit[index])
        {
            found = true;
            break;
        }
        index++;
    }
    
    if(found)
        cout << "\nPrice: $" << price[index];
    else
        cout << "\nThat fruit does not exist in this list. Try again.";
}
closed account (48T7M4Gy)
@aneurysm

Don't duplicate posts.

http://www.cplusplus.com/forum/general/220980/
You could also ask the standard library to do all the dirty work for you and focus only on the amusing 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
#include <fstream>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <string>
#include <stdexcept>

//Prototypes
int showMenu();
std::string askForFileName();
std::map<std::string, double> loadMap(const std::string& filename);
void showMap(std::map<std::string, double>& fruits);
void lookUpPrice(std::map<std::string, double>& fruits);
void waitForEnter();

int main()
{
    std::map<std::string, double> fruits = loadMap(askForFileName());
    //Set floating point to 2 places for currency display
    
    std::cout << std::setprecision(2) << std::fixed;

    bool quit = false;
    while (!quit)   //Menu option #3 changes bool to true
    {
        switch (showMenu())
        {
        case 1:
            showMap(fruits);
            break;
        case 2:
            lookUpPrice(fruits);
            break;
        case 3:
            std::cout << "\nProgram will end.\n\n\n";
            quit = true;
            break;
        default:
            std::cout << "\nNot a valid entry. Try again.";
            break;
        }
    }
    waitForEnter();
    return(0);
}

std::string askForFileName()
{
    std::cout << "\nEnter the name of the file you wish to open: ";
    std::string filename;
    std::getline(std::cin, filename);
    return filename;
}

std::map<std::string, double> loadMap(const std::string& filename)
{
    std::ifstream input(filename);
    std::string name;
    double price;
    std::map<std::string, double> fruits;
    while(input >> name >> price) {
        fruits.emplace(name, price);
    }
    input.close();
    return fruits;
}

void showMap(std::map<std::string, double>& fruits)
{
    for(const auto& p : fruits)
    {
        std::cout << "Fruit: " << p.first << '\t'
                  << "Price: $" << p.second << '\n';
    }
}

int showMenu()
{
    std::cout << "\nPlease select an option from the following menu:\n"
                 "\n\t1 - Display all fruits and prices"
                 "\n\t2 - Look up price of specific fruit"
                 "\n\t3 - Exit the program"
                 "\n\nEnter your selection: ";
    int choice {};
    std::cin >> choice;
    std::cin.ignore(1);
    return choice;
}

void lookUpPrice(std::map<std::string, double>& fruits)
{
    std::string searched_fruit;

    std::cout << "\nEnter the name of the fruit you want the price of: ";
    std::getline(std::cin, searched_fruit);
    try {
        std::cout << "\nPrice: $" << fruits.at(searched_fruit);
    } catch(std::out_of_range& e) {
        std::cout << "\nThat fruit does not exist in this list. Try again.";
    }
}

void waitForEnter()
{
    std::cout << "\nPress ENTER to continue...\n";
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}


Note: it assumes the data file is like:
fruits.txt:
apple 10.00
orange 11.00
apricot 12.00
blackberry 13.00
ananas 14.00
peach 15.00
grapes 16.00

Topic archived. No new replies allowed.