C++ 如何将文本文件中的数据读入结构的int数组?
我创建了一个RentalAgency结构,其中包含名称、邮政编码和库存。我还创建了一个RentalCar类,其中包含库存的成员。我试图将文本文件中的数据读入每个相应的位置,但遇到了问题C++ 如何将文本文件中的数据读入结构的int数组?,c++,arrays,class,struct,io,C++,Arrays,Class,Struct,Io,我创建了一个RentalAgency结构,其中包含名称、邮政编码和库存。我还创建了一个RentalCar类,其中包含库存的成员。我试图将文本文件中的数据读入每个相应的位置,但遇到了问题 struct RentalAgency { char name[25]; //25 characters max int zipcode[5]; //5 digits in zipcode RentalCar inventory[5]; //5 cars }; class Rental
struct RentalAgency {
char name[25]; //25 characters max
int zipcode[5]; //5 digits in zipcode
RentalCar inventory[5]; //5 cars
};
class RentalCar {
int m_year;
char m_make[256], m_model[256]; //256 characters max
float m_price;
bool m_available;
public:
RentalCar();
RentalCar(int, char[], char[], float, bool);
void setYear(int);
void setMake(char[]);
void setModel(char[]);
void setPrice(float);
void setAvailable(bool);
int getYear();
char* getMake();
char* getModel();
float getPrice();
bool getAvailable();
void print();
float estimateCost(int);
};
我正在尝试读取此文本文件
Hertz 93619
2014 Toyota Tacoma 115.12 1
2012 Honda CRV 85.10 0
2015 Ford Fusion 90.89 0
2013 GMC Yukon 110.43 0
2009 Dodge Neon 45.25 1
Alamo 89502
2011 Toyota Rav4 65.02 1
2012 Mazda CX5 86.75 1
2016 Subaru Outback 71.27 0
2015 Ford F150 112.83 1
2010 Toyota Corolla 50.36 1
Budget 93035
2008 Ford Fiesta 42.48 0
2009 Dodge Charger 55.36 1
2012 Chevy Volt 89.03 0
2007 Subaru Legacy 59.19 0
2010 Nissan Maxima 51.68 1
到目前为止,我已经设置了一个函数来读取数据。我已经设法创建了一个for循环,读取租赁机构的名称,但是当涉及到邮政编码时,我就卡住了
void input(struct RentalAgency data[])
{
char inputFile[50]; //50 characters max
char tmp;
std::ifstream inputStream;
std::cout << "Enter input file name: ";
std::cin >> inputFile;
inputStream.open(inputFile);
for(int i = 0; i < 3; i++) //3 agencies
{
inputStream >> data[i].name;
for(int j = 0; j < 5; j++)
{
inputStream >> tmp;
data[i].zipcode[j] = tmp;
}
}
}
我想要的是:
data[0].zipcode[0] = 9
data[0].zipcode[1] = 3
data[0].zipcode[2] = 6
data[0].zipcode[3] = 1
data[0].zipcode[4] = 9
您正在邮政编码中存储字符0
-9
的值。您需要从读取的字符中减去0
的ASCII值:
for(int j = 0; j < 5; j++)
{
inputStream >> tmp;
data[i].zipcode[j] = tmp - '0';
}
for(int j=0;j<5;j++)
{
输入流>>tmp;
数据[i].zipcode[j]=tmp-'0';
}
请在下面找到完整的工作解决方案,包括您的讲师提供的非感知约束
所以,我们在这里做的,通常没有人会做的
- 使用字符数组而不是字符串(为什么会这样?真是无稽之谈)
- 使用普通的老式C样式数组
- 对数组维度使用幻数,而不是动态大小
- 使用原始指针李>
- 每件事都使用设置器和获取器,制动密封
- 返回指向成员变量的指针
#include <iostream>
#include <sstream>
#include <fstream>
#include <iterator>
#include <algorithm>
std::istringstream inputFile{ R"(Hertz 93619
2014 Toyota Tacoma 115.12 1
2012 Honda CRV 85.10 0
2015 Ford Fusion 90.89 0
2013 GMC Yukon 110.43 0
2009 Dodge Neon 45.25 1
Alamo 89502
2011 Toyota Rav4 65.02 1
2012 Mazda CX5 86.75 1
2016 Subaru Outback 71.27 0
2015 Ford F150 112.83 1
2010 Toyota Corolla 50.36 1
Budget 93035
2008 Ford Fiesta 42.48 0
2009 Dodge Charger 55.36 1
2012 Chevy Volt 89.03 0
2007 Subaru Legacy 59.19 0
2010 Nissan Maxima 51.68 1
)" };
constexpr size_t MakeAndModelSize = 256;
constexpr size_t MaxInputLineSize = 1024;
constexpr size_t ZipCodeSize = 5;
constexpr size_t InventorySize = 5;
constexpr size_t NameOfAgenySize = 25;
class RentalCar {
public:
RentalCar() {
std::fill(m_make, m_make + MakeAndModelSize, 0);
std::fill(m_model, m_model + MakeAndModelSize, 0);
};
RentalCar(int y, char* make, char* model, float price, bool available) : m_year(y), m_price(price), m_available(available) {
std::copy_n(make, MakeAndModelSize, m_make);
std::copy_n(model, MakeAndModelSize, m_model);
}
// I am breaking the whole encapsulation
// We make some more noensense and define setters and getters for everything.
// so, we can also make all data public . . .
void setYear(int year) { m_year = year; }
void setMake(char* make) { std::copy_n(make, MakeAndModelSize, m_make); }
void setModel(char* model) { std::copy_n(model, MakeAndModelSize, m_model); }
void setPrice(float price) { m_price = price; }
void setAvailable(bool avail) { m_available = avail; }
int getYear() { return m_year; };
char* getMake() { return m_make; };
char* getModel() { return m_model; }
float getPrice() { return m_price; }
bool getAvailable() { return m_available; };
void print() { std::cout << *this; } // This function is not needed. We overlaoded the inserter already for that purpose
double estimateCost(int days) { return days * m_price * (m_available ? 1.0 : 0.0);}
// Overload inserter and extractor
friend std::ostream& operator << (std::ostream& os, const RentalCar& rc) {
return os << rc.m_year << ' ' << rc.m_make << ' ' << rc.m_model << ' ' << rc.m_price << ' ' << rc.m_available;
}
friend std::istream& operator >> (std::istream& is, RentalCar& rc) {
is >> rc.m_year >> rc.m_make >> rc.m_model >> rc.m_price >> rc.m_available;
return is;
}
protected:
// The data of the car
int m_year{};
char m_make[MakeAndModelSize]; // Lord, forgive me, I am using a fixed size plain char array for a string
char m_model[MakeAndModelSize]; // Lord, forgive me, I am using a fixed size plain char array for a string
float m_price{ 0.0 };
bool m_available{ false };
};
class RentalAgency {
public:
// Extractor operator. Read all data from stream
friend std::istream& operator >> (std::istream& is, RentalAgency& ra) {
is >> ra.name ;
std::copy_n(std::istream_iterator<char>(is), ZipCodeSize, ra.zipcode);
// Read all inventory data
std::copy_n(std::istream_iterator<RentalCar>(is), InventorySize, ra.inventory);
return is;
}
// Inserter operator. Output Data
friend std::ostream& operator << (std::ostream& os, const RentalAgency& ra) {
// Show name and zip code
os << ra.name << ' ';
std::copy_n(ra.zipcode, ZipCodeSize, std::ostream_iterator<char>(os));
os << '\n';
// Print inventory data
std::copy_n(ra.inventory, InventorySize, std::ostream_iterator<RentalCar>(os, "\n"));
return os << '\n';
}
protected:
char name[NameOfAgenySize]{};
int zipcode[ZipCodeSize]{};
RentalCar inventory[InventorySize]{};
};
int main()
{
RentalAgency ra1{};
RentalAgency ra2{};
RentalAgency ra3{};
// Read all data
inputFile >> ra1 >> ra2 >> ra3;
// For verification. Write all data to std::cout
std::cout << ra1 << ra2 << ra3;
return 0;
}
#包括
#包括
#包括
#包括
#包括
std::istringstream输入文件{R'(赫兹93619
2014丰田塔科马115.12 1
2012本田CRV 85.10 0
2015福特Fusion 90.89 0
2013年GMC育空110.43 0
2009道奇霓虹灯45.25 1
阿拉莫89502
2011丰田Rav4 65.02 1
2012马自达CX5 86.75 1
2016斯巴鲁内地71.27 0
2015年福特F150 112.83 1
2010丰田花冠50.36 1
预算93035
2008福特嘉年华42.48 0
2009道奇充电器55.36 1
2012雪佛兰伏特89.03 0
2007斯巴鲁遗产59.19 0
2010日产Maxima 51.68 1
)" };
constexpr size\u t MakeAndModelSize=256;
constexpr size\u t MaxInputLineSize=1024;
constexpr size_t ZipCodeSize=5;
constexpr size\u t InventorySize=5;
constexpr size\u t NameOfAgenySize=25;
租车类{
公众:
RentalCar(){
标准::填充(m_make,m_make+make和ModelSize,0);
std::fill(m_model,m_model+MakeAndModelSize,0);
};
RentalCar(整数y,字符*品牌,字符*型号,浮动价格,布尔可用):m_年(y),m_价格(价格),m_可用(可用){
标准:复制(制造、制造和型号尺寸,m制造);
std::copy_n(model,MakeAndModelSize,m_model);
}
//我正在破坏整个封装
//我们制造了更多的noensense,为每件事定义了setter和getter。
//所以,我们也可以公开所有数据。
无效设置年份(整数年){m_year=year;}
void setMake(char*make){std::copy_n(make,MakeAndModelSize,m_make);}
void setModel(char*model){std::copy_n(model,MakeAndModelSize,m_model);}
无效设置价格(浮动价格){m_价格=价格;}
void setAvailable(bool avail){m_available=avail;}
int getYear(){return m_year;};
char*getMake(){return m_make;};
char*getModel(){return m_model;}
float getPrice(){return m_price;}
bool getAvailable(){return m_available;};
void print(){std::cout>rc.m_可用;
回报是;
}
受保护的:
//汽车的数据
国际年{};
char m_make[MakeAndModelSize];//上帝,原谅我,我正在使用一个固定大小的纯字符数组作为字符串
char m_model[MakeAndModelSize];//上帝,请原谅,我正在使用一个固定大小的纯字符数组作为字符串
浮动市价{0.0};
布尔m_可用{false};
};
类租金代理{
公众:
//提取器运算符。从流中读取所有数据
friend std::istream&operator>>(std::istream&is、RentalAgency&ra){
is>>ra.name;
std::copy_n(std::istream_迭代器(is),zipcodeze,ra.zipcode);
//读取所有库存数据
std::copy_n(std::istream_迭代器(is),InventorySize,ra.inventory);
回报是;
}
//插入器运算符。输出数据
friend std::ostream&operator>ra3;
//用于验证。将所有数据写入std::cout
std::数据[i].zipcode[0],数据[i].zipcode[1]……数据[i].zipcode[4]现在是否有值?将zipcode更改为字符[6]问题解决了。但是除非你喜欢处理缓冲区溢出和内存损坏,而且你希望编写现代C++代码,否则你将想使用<代码> STD::String < /Calp> S代替原来的代码> char < /Cord>数组。当我使用数据[i]。ZIPCODE(0),数据[i]。ZIPCODE(1)等。程序读取数据[i]。ZIPCODE(0)为93619,然后数据[I]。.zipcode[1]将于2014年发布。为什么不单独读取每个数字?我是否必须在这里的某个地方使用atoi?我们不允许在此作业中使用字符串。邮政编码为整数数组是作业的一部分。如果您认为应该将邮政编码存储在int[5]中,我敢肯定您误解了作业
。要么是这样,要么就是作业太疯狂了。很高兴它起到了作用,但我真的很怀疑为什么要用int[5]
来存储一个邮政编码。我想不出任何理由这样做。
#include <iostream>
#include <sstream>
#include <fstream>
#include <iterator>
#include <algorithm>
std::istringstream inputFile{ R"(Hertz 93619
2014 Toyota Tacoma 115.12 1
2012 Honda CRV 85.10 0
2015 Ford Fusion 90.89 0
2013 GMC Yukon 110.43 0
2009 Dodge Neon 45.25 1
Alamo 89502
2011 Toyota Rav4 65.02 1
2012 Mazda CX5 86.75 1
2016 Subaru Outback 71.27 0
2015 Ford F150 112.83 1
2010 Toyota Corolla 50.36 1
Budget 93035
2008 Ford Fiesta 42.48 0
2009 Dodge Charger 55.36 1
2012 Chevy Volt 89.03 0
2007 Subaru Legacy 59.19 0
2010 Nissan Maxima 51.68 1
)" };
constexpr size_t MakeAndModelSize = 256;
constexpr size_t MaxInputLineSize = 1024;
constexpr size_t ZipCodeSize = 5;
constexpr size_t InventorySize = 5;
constexpr size_t NameOfAgenySize = 25;
class RentalCar {
public:
RentalCar() {
std::fill(m_make, m_make + MakeAndModelSize, 0);
std::fill(m_model, m_model + MakeAndModelSize, 0);
};
RentalCar(int y, char* make, char* model, float price, bool available) : m_year(y), m_price(price), m_available(available) {
std::copy_n(make, MakeAndModelSize, m_make);
std::copy_n(model, MakeAndModelSize, m_model);
}
// I am breaking the whole encapsulation
// We make some more noensense and define setters and getters for everything.
// so, we can also make all data public . . .
void setYear(int year) { m_year = year; }
void setMake(char* make) { std::copy_n(make, MakeAndModelSize, m_make); }
void setModel(char* model) { std::copy_n(model, MakeAndModelSize, m_model); }
void setPrice(float price) { m_price = price; }
void setAvailable(bool avail) { m_available = avail; }
int getYear() { return m_year; };
char* getMake() { return m_make; };
char* getModel() { return m_model; }
float getPrice() { return m_price; }
bool getAvailable() { return m_available; };
void print() { std::cout << *this; } // This function is not needed. We overlaoded the inserter already for that purpose
double estimateCost(int days) { return days * m_price * (m_available ? 1.0 : 0.0);}
// Overload inserter and extractor
friend std::ostream& operator << (std::ostream& os, const RentalCar& rc) {
return os << rc.m_year << ' ' << rc.m_make << ' ' << rc.m_model << ' ' << rc.m_price << ' ' << rc.m_available;
}
friend std::istream& operator >> (std::istream& is, RentalCar& rc) {
is >> rc.m_year >> rc.m_make >> rc.m_model >> rc.m_price >> rc.m_available;
return is;
}
protected:
// The data of the car
int m_year{};
char m_make[MakeAndModelSize]; // Lord, forgive me, I am using a fixed size plain char array for a string
char m_model[MakeAndModelSize]; // Lord, forgive me, I am using a fixed size plain char array for a string
float m_price{ 0.0 };
bool m_available{ false };
};
class RentalAgency {
public:
// Extractor operator. Read all data from stream
friend std::istream& operator >> (std::istream& is, RentalAgency& ra) {
is >> ra.name ;
std::copy_n(std::istream_iterator<char>(is), ZipCodeSize, ra.zipcode);
// Read all inventory data
std::copy_n(std::istream_iterator<RentalCar>(is), InventorySize, ra.inventory);
return is;
}
// Inserter operator. Output Data
friend std::ostream& operator << (std::ostream& os, const RentalAgency& ra) {
// Show name and zip code
os << ra.name << ' ';
std::copy_n(ra.zipcode, ZipCodeSize, std::ostream_iterator<char>(os));
os << '\n';
// Print inventory data
std::copy_n(ra.inventory, InventorySize, std::ostream_iterator<RentalCar>(os, "\n"));
return os << '\n';
}
protected:
char name[NameOfAgenySize]{};
int zipcode[ZipCodeSize]{};
RentalCar inventory[InventorySize]{};
};
int main()
{
RentalAgency ra1{};
RentalAgency ra2{};
RentalAgency ra3{};
// Read all data
inputFile >> ra1 >> ra2 >> ra3;
// For verification. Write all data to std::cout
std::cout << ra1 << ra2 << ra3;
return 0;
}