Search code examples
c++parallel-arrays

Why is my parallel array displaying random values?


I am supposed to use a parallel array to show how much a cup of coffee is based on what add-in is added. The original cup of coffee is 2 dollars. I am mostly confused with how to output the correct results. Currently, it will output saying "Order total is2". What am I missing?

// JumpinJava.cpp - This program looks up and prints the names and prices of coffee orders.  
// Input:  Interactive
// Output:  Name and price of coffee orders or error message if add-in is not found 

#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

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

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

   return 0;
} // End of main() 

Solution

  • In this line:

    x = orderTotal + addInPrices[i];
    

    you are setting x (an int value) to something like 2.00 + 0.25, right? Your compiler is likely warning you about a possible loss of precision here. An integer value can only contain whole numbers: 1, 2, 3, etc. If you try to set it to a floating point number like 2.25, it will be truncated (the decimal points chopped off) leaving only the integer part. So the result of x = 2.25 will be the value 2 in x, which is consistent with your output.

    In your assignment template, your instructor has written this comment next to the declaration of x:

    int x;                // Loop control variable
    

    It seems clear to me that the intent was for x to be what you put in the for loop, i.e. the variable controlling how many loops happen and when it ends. You are choosing to create a new variable i instead. This would also explain why x is not initialized to anything - the initialization would happen in the for-loop if you did it the intended way.

    Try this: Instead of using x to store the new price, simply add the add-in price to orderTotal, so that it's always up-to-date and has the correct value. This way you do not need to use x for this at all, and can use it in the for-loop instead. You would then be printing orderTotal instead of x in your output.