首页 > 解决方案 > 将数组的所有元素输出到c ++中的文件

问题描述

我做了一个小程序,询问用户他想要多少件商品(变量 y),然后要求用户输入每件商品的名称和每件商品的价格。

但是这段代码创建的所有 txt 文件只打印第一个项目名称和第一个项目价格。

我应该如何纠正这个?

我已经尝试为客户名称和商品名称\价格创建不同的类。

我还尝试为这些项目创建一个不同的数组。

 #include "Item.h"
#include "Receipt.h"


Item iArray[1000];
Receipt boA[1000];
int loc = 0;
int y;
string array[];
string iName[1000];
void addItem();
void functFix();
void printI();
void p_opt();
int main()
{
    int sel = 0;

    cout << "Welcome\n\n\n";


    do {
        cout << "Please select an option from the menu below:\n\n";
        cout << "1) Add an order" << endl; //need to find a way to make 1) add 2) print 3)exit
        cout << "2) Exit" << endl;
        cin >> sel;
        system("cls");
        switch (sel) {
        case 1:
            functFix();
            system("cls");

            break;

        case 2:
            cout << "Bye" << endl;
            system("pause");
            exit;
            break;
        default:
            cout << "Invalid option" << endl;
            system("pause");
            system("cls");
            break;




        }
    } while (sel != 2);
    return 0;
}
void addItem()
{
    string itemName;
    double itemPrice;





        cout << "Please enter the item Name: ";
        cin.ignore();
        getline(cin, itemName);
        iArray[loc].setItemName(itemName);
        cout << "Please enter the item Price: ";
        cin >> itemPrice;
        iArray[loc].setItemPrice(itemPrice);





}
void functFix()
{
    string Name;

    cout << "Please enter the customer name: ";
    cin.ignore();
    getline(cin, Name);
    boA[loc].setName(Name);
    ofstream order;
    order.open(boA[loc].getName() + ".txt", ios::out | ios::app); //trying to make customer name to be file name so that  a new file is created once the order is put in place
    order << setw(30) << "MONTILLATECH\n" << setw(40) << "382 Via Versalles Villas Reales\n" << setw(33) << "Guaynabo, PR-00969\n" << setw(31) << "787-678-6043\n\n";
    order << "Customer name: " << Name << endl;
    order << "---------------------------------------" << endl;

    order.close();
    cout << "how many items in the order?" << endl;
    cin >> y;
    for (int i = 0; i < y; i++)
    {
        addItem();
    }
    p_opt();
}
void printI()
{
    ofstream order;
    string filename;

    order.open(boA[loc].getName() + ".txt", ios::out | ios::app); //trying to make customer name to be file name so that  a new file is created once the order is put in place
    order << "Ordered items:\n\n";
    //Need to make a loop that keeps printing items in an array //Array could be of any number but equal to y (y=quantiy of items desired)


    for (loc=0; loc < y; loc++)
    {
        order << iArray[loc].getItemName();
        iArray[loc].getItemPrice();

        order << "Your item name is: " << iArray[loc].getItemName();
        order << " ---------- $" << iArray[loc].getItemPrice() << endl;
        cout << "y: " << y << endl;

    }

    order << "---------------------------------------" << endl;
    cout << "y: " << y << endl;
    order << "\nTotal: total" << " $" << endl;

    order << "---------------------------------------" << endl;
    cout << "\n\n";
    order.close();
    loc++;
}
void p_opt()
{
    int opt1 = 0;


        cout << "Do you want to print the order?" << endl;
        cout << "1) yes" << endl;
        cout << "2) No" << endl;
        cin >> opt1;

        switch (opt1)
        {
        case 1:
            printI();
            break;
        case 2:
            exit;
            break;
        default:
            cout << "Invalid option" << endl;
        }

}

程序现在只打印数组的最后一项。它应该打印存储在数组中的所有项目。

代码运行示例:

请输入客户名称:Test66 订单有多少件?3 请输入商品名称:商品1 请输入商品价格:1 请输入商品名称:商品2 请输入商品价格:2 请输入商品名称:商品3 请输入商品价格:3 您是否要打印订单? 1) 是 2) 不是 1

txt文件中输出的文本:

测试 382 Via Versalles Villas Reales Guaynabo, PR-00969 787-678-6043

客户名称:Test66

订购的物品:

item3您的商品名称是:item3 ---------- $3

您的商品名称是: ---------- $0

您的商品名称是: ---------- $0

总计:总计 $

标签: c++visual-c++

解决方案


除了为商品名称和商品价格添加单独的数组外,我还可以通过重新构建和组织代码来解决上述问题。

我建立:

Item ItemNameArray[];
Item ItemPriceArray[];

我的代码(输出函数)最后看起来像这样:

void printOrder()
{


    ofstream order;
    string filename;
    double total;

    order.open(NameArray[loc].getName() + ".txt", ios::out | ios::app);
    order << "Ordered items:\n\n";

    for (int t = 0; t < y; t++) 

/*y is a variable for the amount of items user wants in the
order.*/


    {

        order << "Your item name is: " << itemNameArray[t].getItemName();
        order << " ---------- $" << itemPriceArray[t].getItemPrice() << endl;

    }
    order << "---------------------------------------" << endl;

    Item* tot;
    tot = itemPriceArray;
    for (int o=0;o<y;o++)
    total = total + *(tot + o);

    order << "\nTotal: "<<total<< " $" << endl;

    order << "---------------------------------------" << endl;
    cout << "\n\n";
    order.close();
    loc++;
}

以下是程序现在如何输出到 txt 文件的示例:

测试, 382 Via Versalles Villas Reales, Guaynabo, PR-00969, 787-678-6043

客户名称:测试3

订购的物品:

您的商品名称是:test1 ---------- $2

您的商品名称是:test2 ---------- $3

您的商品名称是:test3 ---------- $4

您的商品名称是:test4 ---------- $5


总计:14 美元


通过阅读 Tony Gaddis 的《从 C++ 开始》(第 8 版),我能够找到这个问题的答案。无论如何,感谢您的评论和帮助。


推荐阅读