1 Replies - 713 Views - Last Post: 11 October 2012 - 06:36 AM Rate Topic: -----

#1 gwalsh3  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 20-March 11

Array subscript issue

Posted 11 October 2012 - 06:03 AM

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

// 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;
    
    total += numbers[counter];                
    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;
} 



Is This A Good Question/Topic? 0
  • +

Replies To: Array subscript issue

#2 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2911
  • View blog
  • Posts: 10,083
  • Joined: 08-August 08

Re: Array subscript issue

Posted 11 October 2012 - 06:36 AM

Array items are numbered starting at 0 so an array of size N will have items 0 to N-1
const int N = 3;
int myarray[N] = {11,12,13};
for(int i = 0; i < N; i++) {
   cout << "Item "<<i << " is: "<< myarray[i] << endl;
}


You test for inequality this way: !=
if (A != B)/> {
  // Do something
}

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1