使用用户输入定义数组
Using user input to define array
我有一个项目,需要创建一个带有结账功能的购物清单。我正在尝试使用用户输入创建一个数组。他们提供了他们正在购买的产品数量,我需要用它来定义阵列的大小。
#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;
}
这是可能的,您只需要像那样更改声明;
int个项目=0;cin>>项目;
shopList *shop = new shopList [items];
相关文章:
- C++编程从外部文本文件定义数组大小
- 如何在子类中重新定义数组大小?
- 有没有办法根据命令行参数定义数组大小?运行时与编译时实例化?
- 我可以使用常量定义数组的长度,那么为什么 int d[b] 不起作用呢?
- 表达式必须具有常数值,变量不能用作定义数组大小的常数
- 在类中定义数组的方法和字段
- (C++)我的自定义数组无法初始化(编译错误)
- 重写自定义数组类的运算符/开始/结束
- 如何在 C++ 中定义数组数组
- 您可以在类初始化器列表中定义数组大小吗?
- 通过Overloading Operator []访问自定义数组包装器中的元素
- 为自定义数组实现迭代器
- 递归定义数组中的数据对齐和排序
- 如何从类中返回自定义数组项目并操纵其属性?C
- 如何使用类 .h 文件中的静态常量来定义数组的长度
- 使用类定义数组创建C++类
- C++自定义数组容器动态大小
- C++中使用什么类型来定义数组大小
- 在源文件中定义数组,然后在其他源文件中使用它
- C 编程从外部输入文件定义数组大小