首页 > 解决方案 > 如何使我的“下标值”在 C++ 中成为一个数组

问题描述

我正在开发我的“自动售货机”代码,用户可以在购物车中添加无限数量的物品,直到他们按 0 结帐。我在“do-while”循环中遇到了数组的问题cost[item],它说“下标值不是数组、指针或向量”。有人可以帮我吗?如果有人愿意提供帮助,我还有一些小问题。以下是我遇到的主要问题:

  1. 按 0 检出部分代码 - 我认为这个问题与cost[item]我上面描述的数组问题有关。

  2. cout << fixed << setprecision(2) << total;如何让我的菜单价格显示 2 位小数 -如果这是正确的做法,我不确定在哪里输入我的代码。

  3. 如何在“结帐”时打印报表以显示总成本的项目。

我的完整代码:

#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;
}

标签: c++arraysdo-while

解决方案


我会尽力帮助你找出你做错的地方:

  1. 关于成本[项目]:也许您已经听说过“范围”。它与变量、指针等的位置有关。在您的程序中,您float cost[5] = {2, 3, 2.50, 1.50, 1};在全局范围内声明。这个范围是你在主函数之外声明一些东西的地方。此范围适用于此文件中的任何位置。但是您还在float cost;本地范围内声明了相同的名称。本地范围仅在您声明 this 的地方有效(在此函数的循环中,if 语句等);编译器优先考虑局部变量(请注意,编译器允许您编写相同名称但范围不同的变量)。我已纠正您的错误并为所有时刻添加评论:
#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;
}

我试图最小化这些变化,这样你就不会浪费时间试图找出这些变化。我认为你才刚刚开始学习这门语言,所以我们不应该用术语让你负担过重。但是要知道这段代码远非完美,有了经验你就会明白这一点,祝你好运。


推荐阅读