-3

I am progressing on my "vending machine" code where users are able to add an unlimited amount of items to the cart until they press 0 to checkout. I ran into a problem with the array cost[item] in my "do-while" loop where it says "subscripted value is not an array, pointer, or vector." Could someone please help me with this? I also have a few smaller problems if someone would like to help. Here are my main problems I ran into:

  1. the press 0 to checkout part of the code - I think this problem is related to the cost[item] array problem I described above.

  2. how to get my menu prices to display 2 digit decimals - I am not sure where to put cout << fixed << setprecision(2) << total; in my code if that is the correct way of doing it.

  3. how to print a statement when "checking out" to display items with total cost.

my full code:

#include <iostream>
#include <iomanip>
using namespace std;


string menuItems[5] = {"Popcorn", "Coconut Clusters" , "Granola Bar" , "Trail Mix" , "Chocolate"};

float cost[5] = {2, 3, 2.50, 1.50, 1};


void vendingMachine() {

  for(int i = 0; i < 5; i++)

  cout << i+1 << ". " << menuItems[i] << ": $" << cost[i] << endl;
  
}



int main() {
  cout << "Vending Machine" << endl;
  cout << "----Items------" << endl;

  vendingMachine();
  cout << "Enter 0 to checkout" << endl;

  
  float cost;
  int total;
  total = 0;
  
  
  do {
    cout << "Enter your selection: " << flush;
    int item;
    cin >> item;
    item = item -1;

    cout << menuItems[item] << ": $" << cost[item] << " has been added to cart." << endl;

    total = total + cost[item];
  

  
  } while (item != 0);

  cout << "Proceding to checkout..." << endl;
  cout << "========================" << endl;
  
  cout << "Amount due: " << total << endl;



cout << "Insert money here: $" << flush;
  float money;
  cin >> money;

  if (money > cost) {
    float change = money-cost;
    cout << "Thank you! You have $" << change << " change." << endl;
  }

  if (money == cost) {
    cout << "Thank you! Have a nice day!." << endl;
  }

  if (money < cost) {
    float amountOwed = cost-money;
    cout << "Please insert another $" << amountOwed << endl;

    cout << "Enter amount: " << flush;
    float payment;
    cin >> payment;

    if (payment > amountOwed) {
    float change2 = payment-cost;
    cout << "Thank you! You have $" << change2 << " change." << endl;
    }

    if (payment == amountOwed) {
      cout << "Thank you! Have a nice day!." << endl;
    }

    if (payment < amountOwed) {
      cout << "Sorry, you did not enter enough money. Your cart has emptied." << endl;
    }

  }
  return 0;
}

1 Answer 1

1

I will try to help you sort out where you did mistakes:

  1. About cost[item]: Maybe you already hear about "scope". It's about location of your variables, pointers, etc. In your program you declared float cost[5] = {2, 3, 2.50, 1.50, 1}; in global scope. This scope is where you declare something outside the main function. This scope works in wherever in this file. But you also declared float cost; with the same name in local scope. Local scope works only where you declared this and deeper (in loops of this function, if-statements etc.); Compiler priority is for local variables(notice that compiler allows you to write same name of variables but with different scopes). I have correct your misstakes and add comments for all moments:
#include <iostream>
#include <iomanip>
using namespace std;


string menuItems[5] = { "Popcorn", "Coconut Clusters" , "Granola Bar" , "Trail Mix" , "Chocolate" };

float cost[5] = { 2, 3, 2.50, 1.50, 1 };

void vendingMachine() {

    for (int i = 0; i < 5; i++)

        cout << i + 1 << ". " << menuItems[i] << ": $" << cost[i] << endl;

}

int main() {

    cout.precision(2);
    cout << std::fixed;//you can place this options righе here if you want that it works on all numbers of your program

    cout << "Vending Machine" << endl;
    cout << "----Items------" << endl;

    vendingMachine();
    cout << "Enter 0 to checkout" << endl;
    
    //float costs;you dont need this local variable

    //int total;//if your products have number after point (2.50, 1.50)
    //it could be moment when total will be 2.5 and integer variable
    //will convert it to 2 and you will lose 0.5 so choose float total
    float total;
    total = 0;

    int item;

    do {
        cout << "Enter your selection: " << flush;
        //int item was here. again it's local scope of do{}, "while" ouside this scope;
        cin >> item;
        //item = item - 1; you did it because you display products from 1
        //but if client want to escape and press '0' it will be -1 and this loop never ends;
        item = item - 1;
        //here will be printed : $0 has been added to cart even if you pressed 0 and what to escape
        //use if(item!=-1) {
        cout << menuItems[item] << ": $" << cost[item] << " has been added to cart." << endl;
        total = total + cost[item];//}statement to avoid this bug

    } while (item != -1);//right number  for your case to exit

    cout << "Proceding to checkout..." << endl;
    cout << "========================" << endl;

    cout << "Amount due: " << total << endl;


    cout << "Insert money here: $" << flush;
    float money;
    cin >> money;

    if (money > total) {//Here and below you should to compare money with total
        float change = money - total;
        cout << "Thank you! You have $" << change << " change." << endl;
    }

    if (money == total) {
        cout << "Thank you! Have a nice day!." << endl;
    }

    if (money < total) {
        float amountOwed = total - money;
        cout << "Please insert another $" << amountOwed << endl;

        cout << "Enter amount: " << flush;
        float payment;
        cin >> payment;

        if (payment > amountOwed) {
            float change2 = payment - total;
            cout << "Thank you! You have $" << change2 << " change." << endl;
        }

        if (payment == amountOwed) {
            cout << "Thank you! Have a nice day!." << endl;
        }

        if (payment < amountOwed) {
            cout << "Sorry, you did not enter enough money. Your cart has emptied." << endl;
        }

    }
    return 0;
}

I have tried to minimize the changes so that you do not waste time trying to figure out these changes. I think that you are just starting to learn this language, so we shouldn't overload you with terms. But know that this code is far from perfect, with experience you will understand this, good luck in your achives.

1
  • Thank you so much! This was very helpful!
    – yeliah
    Aug 6, 2020 at 3:31

Not the answer you're looking for? Browse other questions tagged or ask your own question.