简体   繁体   English

如何使用Visual Studio在C ++中使用2D数组?

[英]How can I use 2D array in C++ using Visual Studio?

I'm new to this site and programming in C++ language this semester. 我是这个网站的新手,本学期用C ++语言编程。

I have been really trying for 2 days and have asked classmates but they do not know either. 我已经尝试了2天并且已经问了同学,但他们也不知道。 A classmate said to use the 2D arrays but I don't know what that is and my professor has not gone over 2D arrays. 一位同学说要使用2D阵列,但我不知道那是什么,我的教授还没有超过二维阵列。

I am stuck and would really appreciate help. 我被困住了,真的很感激帮助。

The input file contains this: 输入文件包含:

Plain_Egg 1.45
Bacon_and_Egg 2.45
Muffin 0.99
French_Toast 1.99
Fruit_Basket 2.49
Cereal 0.69
Coffee 0.50
Tea 0.75

idk how to display all the "users" orders idk如何显示所有“用户”订单

Basically a receipt, like he orders this and how many, then ask "u want anything else?", then take the order number and how many again, THEN at the end give back a receipt that looks like this 基本上收据,就像他订购这个和多少,然后问“你想要别的什么?”,然后再拿订单号和多少,然后最后给出一张看起来像这样的收据

bacon_and_eggs    $2.45
Muffin            $0.99
Coffee            $0.50
Tax               $0.20
Amount Due        $4.14

Here is my code: 这是我的代码:

// Libraries

#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include <cmath>

using namespace std;

//structures

struct menuItemType {
    string itemName;
    double itemCost;
};


// Prototypes
void header();
void readData(menuItemType menu[]);
void Display(menuItemType menu[]);

int main() {

    header();
    menuItemType menu [8];
    readData(menu); 
    Display(menu);

    //system("pause");

    return 0;
}

void header() {
    char c= 61;
    for (int i=0; i < 64; i++) {
        cout << c; 
}

cout << c << endl; 
cout << endl;
cout << "Breakfast Menu" <<endl;


    for (int i=0; i < 64; i++) {
        cout << c; 
    }

    cout << "" << c << endl;
    cout << endl;
}

void readData(menuItemType item[]) {
    int i=0;
    ifstream in;
    in.open("input.txt");
    cout << fixed << setprecision(2);

    while(!in.eof()) {
        in >> item[i].itemName >> item[i].itemCost;
        ++i;
    }
}

void Display(menuItemType item[]) {
    int choice = 0, quantity = 0;
    double total = 0.0, totalF = 0.0, tax = 0.0;
    char exit = 'y';
    int j = 1, z = 1, i = 1;

    //the Menu
    for (int i=0; i<8; i++){
        cout << j << ". " << setw(18) << left << item[i].itemName << "$" << setw(10) << item[i].itemCost << endl;
        j++;
    }

    cout << endl;

    while(exit == 'y' || exit == 'Y') {

        cout << "Please Enter your Selection or 0 to Exit : ";
        cin >> choice;

        if(cin.fail()) {
            cout << "*******Invalid selection*******" << endl;
            cin.clear();
            cin.ignore(1000,'\n');
        } else if (choice==0) {break; }
        else {
            cout<< "Enter Quantity: ";
            cin>> quantity;

            if (quantity==0) { break;}
            else {
                choice--;
                total += (quantity * item[choice].itemCost);
                tax = (total * .05);
                totalF = total + tax;
                cout << endl;
            }
            cout << endl;
            cout << "======================================================" << endl;
            cout << item[choice].itemName << "\t\t" << item[choice].itemCost << endl;
            cout << "======================================================" << endl;
            cout << "Do you want to continue (Y/N): ";
            cin >> exit;
        }
    }
}

First off, you don't need a two dimensional array for this! 首先,你不需要二维数组! You already have a one dimensional array of a suitable structure, as far as I can tell: Something which stores the name of the object and its price. 你已经有了一个合适结构的一维数组,据我所知:存储对象名称及其价格的东西。 What is somewhat missing is how many objects are currently in the array and how much space it has. 有些缺失的是阵列中当前有多少个对象以及它有多少空间。 If you want to go with the content of the entire array, make sure that you objects are correctly initialized, eg, that the names are empty (this happens automatically, actually) and that the prices are zero (this does not). 如果你想要使用整个数组的内容,请确保你的对象被正确初始化,例如,名称为空(这实际上是自动发生的),并且价格为零(这不是)。

I'm not sure if it is a copy&paste errors but the headers are incorrectly included. 我不确定它是否是复制和粘贴错误,但标题包含错误。 The include directives should look something like this: include指令看起来应该是这样的:

#include <iostream>

The actual loop reading the values doesn't really work: You always need to check that the input was successful after you tried to read! 读取值的实际循环并不真正起作用:您尝试阅读 始终需要检查输入是否成功! Also, using eof() for checking that the loop ends is wrong (I don't know where people pick this up from; any book recommending the use of eof() for checking input loops is only useful for burning). 此外,使用eof()检查循环结束是错误的(我不知道人们从哪里挑选;任何推荐使用eof()来检查输入循环的书只对燃烧有用)。 The loop should look something like this: 循环应该如下所示:

while (i < ArraySize && in >> item[i].itemName >> item[i].itemCost)
    ++i;
}

This also fixes the potential boundary overrun in case there is more input than the array can consume. 如果输入的数量超过数组可以消耗的数量,这也可以修复潜在的边界溢出。 You might want to consider using a std::vector<Item> instead: this class keeps track of how many elements there are and you can append new elements as needed. 您可能需要考虑使用std::vector<Item> :此类跟踪有多少元素,您可以根据需要添加新元素。

Note that you didn't quite say what you are stuck with: You'd need to come up with a clearer description of what your actual problem is. 请注意,你并没有完全说出你所坚持的内容:你需要更清楚地描述你的实际问题。 The above is just correcting existing errors and readjusting the direction to look into (ie, forget about two dimensional arrays for now). 以上只是纠正现有错误并重新调整要调查的方向(即,暂时忘记二维数组)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Visual Studio 2017 在 C++ 中为参数化对象数组使用唯一指针? - How can I use unique pointers for an array of parameterized objects in C++ using Visual Studio 2017? 使用2D数组的C ++ Visual Studio堆栈溢出 - C++ Visual Studio stack overflow with 2D array 如何在C ++中使用模板更有效地实现带有边界约束的2D数组? - How can I implement a 2D array with bound cheking in C++ using template more efficiently? 如何用随机字符填充二维数组? 在 c++ 中使用类 - How can I fill a 2d array, with random caracters? Using classes in c++ 如何使用 C++ 将 2D RGB 数组转换为 dds 文件? - How can I convert a 2D RGB array into a dds file using C++? 如何在Visual Studio中使用C ++ shlwapi库? - How can I use the C++ shlwapi library in Visual Studio? 为什么我不能在C ++中填充此2D数组? - Why can't I fill this 2D array in C++? 使用float 2d数组的C ++内存泄漏,如果我使用double则消失 - C++ memory leak using float 2d array, disappears if I use double 我可以将Visual Studio用作cocos2d-x 3.0rc2,C ++开发的IDE吗? - Can I use Visual Studio as an IDE for cocos2d-x 3.0rc2, c++ development? C ++-如何对2D数组中的项目使用memmove? - C++ - How to use memmove for items in 2D array?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM