Error with getline
Error with getline
我在这里得到一个错误,我不知道为什么,我是c++的新手,如果你能看看我的代码的其余部分,以确保它是正确的,那将是伟大的。
这两行出现了错误。
getline(in, e.first, ',');
getline(in, e.last, ',');
它说Employee类没有成员First,而我知道它不在那个函数中,我该如何修复它?
下面是我剩下的代码。
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
struct Person {
string first;
string last;
};
struct Address {
string street;
string city;
string state;
string zipcode;
};
struct Employee {
Person name;
Address homeAddress;
int eid;
};
void readEmployee(istream& in, Employee& e);
void displayEmployee(ostream& out, const Employee& e);
int main(int argc, const char* argv[]) {
Employee e[50];
ifstream fin;
ofstream fout;
fin.open("employeesIn.txt");
if (!fin.is_open()) {
cerr << "Error opening employeesIn.txt for reading." << endl;
exit(1);
}
fout.open("employeesOut.txt");
if (!fout.is_open()) {
cerr << "Error opening employeesOut.txt for writing." << endl;
exit(1);
}
int EmployeePopulation = 0;
readEmployee(fin, e[EmployeePopulation]);
while (!fin.eof()) {
EmployeePopulation++;
readEmployee(fin, e[EmployeePopulation]);
}
fin.close();
for (int i = 0; i <= EmployeePopulation - 1; i++) {
displayEmployee(fout, e[i]);
}
fout.close();
cout << endl;
return 0;
}
void readEmployee(istream& in, Employee& e) {
string cidText;
if (getline(in, cidText, ',')) {
e.eid = stoi(cidText);
getline(in, e.first, ',');
getline(in, e.last, ',');
getline(in, e.homeAddress.street, ',');
getline(in, e.homeAddress.city, ',');
getline(in, e.homeAddress.state, ',');
string zipcodeText;
getline(in, zipcodeText, ',');
e.homeAddress.zipcode = stoi(zipcodeText);
}
}
我们将Person结构重命名为Name结构如何?
(毕竟它只包含第一个和最后一个)
这将给我们这个:
struct Name {
string first;
string last;
};
雇员现在是什么样子?
它看起来像这样:
+-------------+ | Employee | | | | +---------+ | | | Name | | | +---------+ | | | | +---------+ | | | Address | | | +---------+ | | | +-------------+
Name是Employee的一部分,但是第一个和最后一个在哪里呢?它们是Name的一部分。
这是相同的图片,除了它更深入地显示第一和最后:
+---------------+
| Employee |
| |
| +-----------+ |
| | Name | |
| | | |
| | +-------+ | |
| | | first | | |
| | +-------+ | |
| | | |
| | +-------+ | |
| | | last | | |
| | +-------+ | |
| | | |
| +-----------+ |
| |
| +---------+ |
| | Address | |
| +---------+ |
| |
+---------------+
需要使用两个点操作符('.')来访问first和last,因为它们的深度是前者的两倍。
Employee e;
e.name.first = "Joe";
您的代码已被重构以反映这些更改:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
struct Name {
string first;
string last;
};
struct Address {
string street;
string city;
string state;
string zipcode;
};
struct Employee {
Name name;
Address homeAddress;
int eid;
};
void readEmployee(istream& in, Employee& e);
void displayEmployee(ostream& out, const Employee& e);
int main(int argc, const char* argv[]) {
Employee e[50];
ifstream fin;
ofstream fout;
fin.open("employeesIn.txt");
if (!fin.is_open()) {
cerr << "Error opening employeesIn.txt for reading." << endl;
exit(1);
}
fout.open("employeesOut.txt");
if (!fout.is_open()) {
cerr << "Error opening employeesOut.txt for writing." << endl;
exit(1);
}
int EmployeePopulation = 0;
readEmployee(fin, e[EmployeePopulation]);
while (!fin.eof()) {
EmployeePopulation++;
readEmployee(fin, e[EmployeePopulation]);
}
fin.close();
for (int i = 0; i <= EmployeePopulation - 1; i++) {
displayEmployee(fout, e[i]);
}
fout.close();
cout << endl;
return 0;
}
void readEmployee(istream& in, Employee& e) {
string cidText;
if (getline(in, cidText, ',')) {
e.eid = stoi(cidText);
getline(in, e.name.first, ',');
getline(in, e.name.last, ',');
getline(in, e.homeAddress.street, ',');
getline(in, e.homeAddress.city, ',');
getline(in, e.homeAddress.state, ',');
string zipcodeText;
getline(in, zipcodeText, ',');
e.homeAddress.zipcode = stoi(zipcodeText);
}
}
void displayEmployee(ostream& out, const Employee& e){
//you can access the specific name values this way:
cout << e.name.first << " " << e.name.last << endl;
return;
}
相关文章:
- Seg Fault Issue C++ (file IO / getline)
- Problems with std::cin.fail()
- 当用户键入分隔符时,停止getline()输入
- getline() 的原型/库是什么;
- 错误 没有与参数列表匹配的重载函数"getline"实例
- 应用程序崩溃并显示"symbol _ZdlPvm, version Qt_5 not defined in file libQt5Core.so.5 with link time reference"
- 如何在 c++ 中使用 ',' 作为 getline 分隔符
- 错误:调用'getline'没有匹配函数
- 如何在 c++ 中使用 getline 从文件中读取字符串?
- 这对"With a stackless coroutine, only the top-level routine may be suspended."意味着什么
- Boost.TEST with CLion: "Test framework quit unexpectedly"
- std::getline with std::fstream
- getline and printing with cout
- Error with getline
- Getline with WinApi - Using WOFSTREAM / WIFSTREAM - C++ / WI
- C++ getline not working with double
- Issues with getline c++
- Problems with cin.getline()
- Error with getline ifstream
- Error with std::getline