gpt4 book ai didi

c++ - 使用用户输入定义数组

转载 作者:行者123 更新时间:2023-11-28 06:23:51 25 4
gpt4 key购买 nike

我有一个项目,我需要创建一个具有结帐功能的购物 list 。我正在尝试使用用户输入创建一个数组。他们提供了他们购买的产品数量,我需要使用它来定义数组的大小。

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

struct shopList {
double pluCode;
string product;
int saleType; // 0 = per unit, 1 = per pound
double price;
double inventory;


};

int main(){

char line[255];
const int items = 0;
int n;

ofstream outfile;
outfile.open("products.txt");

cout << "How many items in your checkout: ";
cin >> items;

shopList shop[items];

for (n = 0; n < items; n++) {
cout << "Enter the PLU code: ";
cin >> shop.pluCode;
outfile << shop.pluCode << " ";

cout << "Enter product name: ";
cin >> shop.product;
outfile << shop.product << " ";

cout << "Enter the price type (0 for per unit, 1 for per pound): ";
cin >> shop.saleType;
outfile << shop.saleType << " ";

cout << "Enter the price of the product: ";
cin >> shop.price;
outfile << shop.price << " ";

cout << "How much will you purchase: ";
cin >> shop.inventory;
outfile << shop.inventory << " " << "\n";

}



outfile.close();

ifstream infile;
infile.open("products.txt");
infile.getline(line, 255);
cout << line << endl;
}

最佳答案

这是可能的,你只需要像那样改变你的声明;

整数项 = 0 ; cin >> 项目;

shopList *shop =  new  shopList [items];

关于c++ - 使用用户输入定义数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28864228/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com