Challenge 76

Description

The purpose of this challenge is to use arrays and functions. This challenge also uses a menu.

Requirements

  1. Start by writing the main() function as given below
    int main()
    {
        int input;
        const int SIZE = 10;
        string apples[SIZE] = { "Ambrosia", "Granny-Smith", "Honeycrisp", "Fuji", "Gala", "Pink-Lady", "Braeburn", "Envy", "McIntosh", "Jazz" };
        double prices[SIZE] = { 1.29, 2.49, 3.99, 1.99, 1.99, 3.29, 2.59, 4.99, 2.79, 3.49 };
    
        // create other variables as needed
    
        system("clear"); 
        do 
        {
            cout << "1 - Display all apples" << endl;
            cout << "2 - Display an apple - find by index" << endl;
            cout << "3 - Display an apple - find by name" << endl;
            cout << "4 - Quit" << endl;
            cout << "Choice: "; 
            cin >> input; 
    
            switch (input)
            {
                case 1: // display all
                        break;
    
                case 2: // display apple - find by index
                        break;
    
                case 3: // display apple - find by name
                        break;
    
            }
    
        } while (input != 4);
    
        return 0;
    }
  2. Write a function void show_all(string array[], double prices[], int size). This function will display all elements of array and prices. Use this format when displaying as an example Ambrosia ($1.29). Display each set of data one per line.
  3. Write a function void show_one(int index, string array[], double prices[], int size). This function will display one single element of array and prices. Use this format when displaying as an example Ambrosia ($1.29). The index parameter will be used to identify a single element from array and prices. HOWEVER, you will also need to check that index is within the size of the array; if it is not, then display “Not found.” Of course, you will write the prototype as well as the function definition. This function should not ask the user for input.
  4. Write a function int find_one(string name, string array[], int size). This function will search for name in array. This function will return -1 if name is not found in array. Otherwise, it will return the index/location where name is found in array. This function should not ask the user for input.
  5. Look at the case blocks in main(). Each case block will correspond to making the appropriate call to one of the functions you wrote as above.
    • For Option 2 of the menu, you will need to ask the user to specify an index value. Then call the show_one() function passing in the appropriate parameters.
    • For Option 3 of the menu, you will need to ask the user to enter the name of an apple. Then, call the find_one() function passing in the appropriate parameters. Remember that the find_one() function returns -1 if the name is not found; display a message “applename is not found.” If the find_one() function returns a value other than -1, then use this value as an index into the apples and prices arrays to display a message like Ambrosia ($1.29) using the appropriate information.
  1. Sample Interaction 1
1 - Display all apples
2 - Display an apple - find by index
3 - Display an apple - find by name
4 - Quit
Choice: 1

Ambrosia ($1.99)
Granny-Smith ($2.49)
... show the rest here
Jazz ($3.49)

  1. Sample Interaction 2
1 - Display all apples
2 - Display an apple - find by index
3 - Display an apple - find by name
4 - Quit
Choice: 2

Enter index number: 3
Honeycrisp ($3.99)
  1. Sample Interaction 2b
1 - Display all apples
2 - Display an apple - find by index
3 - Display an apple - find by name
4 - Quit
Choice: 2

Enter index number: 12
Not found
  1. Sample Interaction 3a
1 - Display all apples
2 - Display an apple - find by index
3 - Display an apple - find by name
4 - Quit
Choice: 3

Enter name: Envy
Envy ($4.99)
  1. Sample Interaction 3b
1 - Display all apples
2 - Display an apple - find by index
3 - Display an apple - find by name
4 - Quit
Choice: 3

Enter name: Cosmic
Not found

LEGEND

PROGRAM OUTPUT
USER INPUT
FROM INPUT
META/HELPER TEXT

CATALOG ID: CPP-CHAL00076

Print Requirements