Search code examples
c++arraysparallel-arrays

Parallel Arrays in C++


Trying to create a program that takes a coffee flavor add-in and checks if it's valid using an array.
If valid it uses the array index to gather price information.

I managed to write the code below, but it only works for 1 iteration.
How can alter it so a user can enter: Cream and cinnamon and output the total of each add-in as well as the total price of the cup of coffee? The cup of coffee starts with a base price of $2.00

#include <iostream>
#include <string>

using namespace std;

int main()
{
    // Declare variables.
    string addIn;               // Add-in ordered
    const int NUM_ITEMS = 5;    // Named constant

    // Initialized array of add-ins
    string addIns[] = { "Cream", "Cinnamon", "Chocolate", "Amaretto", "Whiskey" };
    // Initialized array of add-in prices
    double addInPrices[] = { .89, .25, .59, 1.50, 1.75 };

    bool foundIt = false;        // Flag variable
    int x;                       // Loop control variable
    double orderTotal = 2.00;    // All orders start with a 2.00 charge
    string QUIT = "XXX";

    // Get user input
    cout << "Enter coffee add-in or XXX to quit: ";
    cin >> addIn;

    // Write the rest of the program here.
    for (x = 0; x < NUM_ITEMS && foundIt == false && addIn != QUIT; x++) {
        if (addIn == addIns[x]) {
            foundIt = true;
            x--;
        }
    }
    if (foundIt == true) {
        cout << addIns[x] << " $" << addInPrices[x] <<endl;
        cout << "$" << orderTotal + addInPrices[x] <<endl;
    }
    else {
        cout << "Sorry, we do not carry that." <<endl;
        cout << "Order total is $ " << orderTotal <<endl;
    }

    return 0;
}

Solution

  • Your program is written to get a single output. For multiple outputs there have to be loops and the not found condition also has to be re-written.

    try this

    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
        // Declare variables.
                 
        const int NUM_ITEMS = 5;        // Named constant
        string addIn[NUM_ITEMS];        // Add-in ordered
    
        // Initialized array of add-ins
        string addIns[] = { "Cream", "Cinnamon", "Chocolate", "Amaretto", "Whiskey" };
        // Initialized array of add-in prices
        double addInPrices[] = { .89, .25, .59, 1.50, 1.75 };
    
        //bool foundIt = false;        // Flag variable
        int x, i, j;                       // Loop control variable
        double orderTotal = 2.00;    // All orders start with a 2.00 charge
        string QUIT = "XXX";
    
        // Get user input
        cout << "Enter coffee add-ins followed by XXX to quit: ";
        for(i=0; i<NUM_ITEMS; i++) {
            cin >> addIn[i];
            if(addIn[i] == QUIT) {
                i++;
                break;
            }
        }
    
        int foundIt[i];
    
        // Write the rest of the program here.
        
        for(j=0; j<i; j++) {
            foundIt[j] = -1;
            for(x = 0; x<NUM_ITEMS && foundIt[j] == -1 && addIn[j] != QUIT; x++) {
                if (addIn[j] == addIns[x]) {
                    foundIt[j] = x;
                }
            }
        }
        
        for(j=0; j<i-1; j++) {
            cout << addIn[j];
            if(foundIt[j] != -1) {
                cout << " $" << addInPrices[foundIt[j]] << endl;
                orderTotal = orderTotal + addInPrices[foundIt[j]];
            }
            else {
                cout << " - Sorry, we do not carry that." <<endl;
            }
        }
    
        cout << "$" << orderTotal <<endl;
        
        return 0;
    }
    

    Sample Outputs

    Enter coffee add-ins followed by XXX to quit: Cream Cinnamon XXX
    Cream $0.89
    Cinnamon $0.25
    $3.14
    
    Enter coffee add-ins followed by XXX to quit: Cream Onion XXX
    Cream $0.89
    Onion - Sorry, we do not carry that.
    $2.89
    

    What I did was made addIn array of srings with NUM_ITEMS size instead of variable. Also, foundIt was made an integer array to keep track of indexes where the items are found in addIns array and -1 if not found.

    To only access the items that user has entered in addIn, your QUIT has been made the termination condition in that array.