Array subscript Issue

Hello all,
I am trying to write a program using an array. One of the problems I am having is that when I select number 2 it selects the number one in the array. I cannot figure out how to write the code so that I access the correct number in the array. Another problem is that I am not sure where to place the line (11)while(choice <> SENTINEL) of code or do I place it at all. Would I place it in front of the first output line (52)in pseudocode or in front of the line (66)if(choice <> -1) or am I wrong all together? Finally I have the line (20)total = total + numbers[counter] in pseudocode and when I implement it into code the compiler comes back with the error `total' undeclared (first use this function). Also I am not sure where to place it in code if it does belong.The pseudocode was given to the class by my teacher and I am confused about should total have been declared and maybe he left that out by accident?

Thank you for your time,
gwalsh3

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
// Start
//     Declarations
//         num SIZE = 5
//         num COFFEEPRICE = 2.00
//         string products[SIZE]="Whipped cream", "Cinnamon", "Chocolate sauce", "Amaretto", ""Irish whiskey"
//         num prices[SIZE]=0.89, 0.25, 0.59, 1.50, 1.75
//         num totalPrice = 0
//         num choice = 0
//         num  SENTINEL = -1
//
//     while (choice <> SENTINEL))
//     output "Please select an item from the Product menu by selecting the item number (1 - 5)  or -1 to terminate: "
//       output "Product		   Price ($)"
//       output "=======		   ========="
//       output "1. Whipped cream     0.89"
//       output "2. Cinnamon          0.25"
//       output "3. Chocolate sauce   0.89"
//       output "4. Amaretto          1.50"
//       output "5. Irish whiskey     1.75"
//       total = total + numbers[counter]
//       output "Please enter a positive number: "
//       input choice
//       if (choice <> -1) then
//         if ((choice >= 1) and (choice <= 5)) then
//           totalPrice = totalPrice + prices[choice]
//           output "Item number ", choice,": ", products[choice], " has been added"
//         else
//           output "Item number ",choice, " is not valid", "Sorry we do not carry that item"
//         endif
//       endif
//     endwhile
//     output "Total price of order is ",totalPrice
//     output "Thanks for purchasing from Jumpin Jive Coffee Shop"
// Stop

#include <iostream>
#include <cstdlib>
#include <string>

using namespace std;
int main() 
{
    const int SIZE = 5;
    double COFFEEPRICE = 2.00;
    string products[SIZE] = {"Whipped cream", "Cinnamon", "Chocolate sauce", "Amaretto", "Irish whiskey"};
    double prices[SIZE] = {0.89, 0.25, 0.59, 1.50, 1.75};
    double totalPrice = 0;
    int choice = 0;
    int SENTINEL = -1;
    
    
    cout << "Please select an item from the Product menu by selecting the item number (1 - 5) or -1 to terminate: " << endl;
    cout << "Product             Price ($)" << endl;
    cout << "=======             =========" << endl;
    cout << "1. Whipped cream     0.89" << endl;
    cout << "2. Cinnamon          0.25" << endl;
    cout << "3. Chocolate sauce   0.89" << endl;
    cout << "4. Amaretto          1.50" << endl;
    cout << "5. Irish Whiskey     1.75" << endl;
    
                  
    cout << "Please enter a positive number: ";
    cin >> choice;
    

    if(choice != -1)
                 {
                           if(choice >= 1 && choice <= 5)
                           {
                                     
                                     totalPrice = COFFEEPRICE + prices[choice];
                                     cout << "Item number " << choice << ": " << products[choice] << " has been added" << endl;
                           }
                           else
                           {
                                     cout << "Item number " << choice << " is not valid" << endl;
                                     cout << "Sorry we do not carry that item" << endl;
                           }
                 }
    
                        
    cout << "Total price of order is: $" << totalPrice << endl;
    cout << "Thanks for purchasing from Jumpin Jive Coffee Shop" << endl;
                     
    system("PAUSE");
    return 0;
}    
Array indices start at 0, so putting in 0 will give the 1st element, 1 will give the 2nd element, 2 will give the 3rd etc. This also means an array of size 5 will only have indices of 0 - 4.
One of the problems I am having is that when I select number 2 it selects the number one in the array. I cannot figure out how to write the code so that I access the correct number in the array.

That's the annoying thing about arrays. The position of elements starts counting from 0, not 1. You prices array is like this
prices[0] = 0.89
prices[1] = 0.25
prices[2] = 0.59
prices[3] = 1.50
prices[4] = 1.75

So you can see that if you enter choice 1, you will access element prices[1], which is actually the second.
To fix, simply access element prices[choice - 1], or change the choices to range from 0 to 4

Finally I have the line (20)total = total + numbers[counter] in pseudocode and when I implement it into code the compiler comes back with the error `total' undeclared (first use this function). Also I am not sure where to place it in code if it does belong.The pseudocode was given to the class by my teacher and I am confused about should total have been declared and maybe he left that out by accident?

I don't see any total in your actual code. I see a totalPrice. Does that give you an error?

Another problem is that I am not sure where to place the line (11)while(choice <> SENTINEL) of code or do I place it at all. Would I place it in front of the first output line (52)in pseudocode or in front of the line (66)if(choice <> -1) or am I wrong all together?

That depends on what you want to do. Assuming you correctly implement the loop, in the first case you will be presented with the menu every time you make a choice. In the second case you will enter choices until you decide to exit (note that if you do this as the code is now, it will loop forever)
Last edited on
You are correct about that the while should be put in front of line 52. The while loop should end on line somewhere around line 80.

total = total + numbers[counter] looks out of place. There is no mention about total or numbers elsewhere so you can probably just remove that line.
Last edited on
Thank you guys for all your help. It took me a little while to figure out what I needed to do but I think I have it now. Thanks again ResidentBiscuit, maeriden, and Peter87 you guys are the best!!!!
Last edited on
Topic archived. No new replies allowed.