0

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() 
Drew Dormann
  • 59,987
  • 13
  • 123
  • 180
Gabby
  • 41
  • 5
  • Did you try to step line by line through your code with a debugger? – Thomas Sablik Feb 28 '20 at 19:20
  • `x` is an `int`, which will truncate the double number. I doubt this is intended? (also your comment says "flow control variable" which doesnt seem acurate either). Suggested reading: [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – Borgleader Feb 28 '20 at 19:20
  • The thing is it’s displaying an output. So I’m not getting any errors. I believe it’s the output that’s messing up, I just can’t figure out how to word it instead – Gabby Feb 28 '20 at 19:23
  • Where is the parallel array? – Coral Kashri Feb 28 '20 at 19:29
  • Double to int truncation is not an error (though some compilers warn about it). So yes you would get output. [for example](http://coliru.stacked-crooked.com/a/e77645600dba3546) – Borgleader Feb 28 '20 at 19:30
  • What compiler are you using? – JohnFilleau Feb 28 '20 at 19:31
  • I am using MindTap – Gabby Feb 28 '20 at 19:46

1 Answers1

1

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.

Klaycon
  • 10,599
  • 18
  • 35
  • Like this? totalPrice = orderTotal + addInPrices[i]; When I do that, it just says totalPrice is not declared – Gabby Feb 28 '20 at 19:49
  • Sorry, `totalPrice` was a typo. The variable you're given is `orderTotal`. The point of what I was saying is that you should simply reuse the same variable to keep track of the order total as it changes. I've edited to correct. – Klaycon Feb 28 '20 at 20:04
  • Oh ok. Thank you! – Gabby Feb 28 '20 at 20:16