我试图运行这个类函数字符串,每个函数都在类头中声明并在class.cpp中定义
我遇到的问题是,它会跳过街道名称,然后将其放入城市(将所有内容移到一个城市),然后当涉及到邮政编码时,只需重新输入街道号码即可。class.h看起来像
class AddressBook
{
private:
string firstName;
string lastName;
int streetNum;
string streetName;
string city;
string state;
int zipCode;
public:
static int entryCnt;
void setFirstName(string temp);
void setLastName(string temp);
void setStreetNum(int tempInt);
void setStreetName(string temp);
void setCity(string temp);
void setState(string temp);
void setZipCode(int tempInt);
//copies some properties into out agruments
void getFirstName(string buff, int sz) const;
void getLastName(string buff, int sz) const;
void addEntryFromConsole();
void printToConsole(int entryCnt);
void appendToFile();
void operator=(const AddressBook& obj);
};
bool operator==(const AddressBook& obj1, const AddressBook& obj2);
#endif // !ADDRESSBOOK_ENTRY
string temp;
int tempInt;
和相关的class.cpp部分看起来像
#include <iostream>
#include "AddressBook.h"
void AddressBook::setFirstName(string temp) {
firstName = temp;
}
void AddressBook::setLastName(string temp) {
lastName = temp;
}
void AddressBook::setStreetNum(int tempInt) {
streetNum = tempInt;
}
void AddressBook::setStreetName(string temp) {
streetName = temp;
}
void AddressBook::setCity(string temp) {
city = temp;
}
void AddressBook::setState(string temp) {
state = temp;
}
void AddressBook::setZipCode(int tempInt) {
zipCode = tempInt;
}
和我的main.cpp部分。
while (openFile.good())
{
getline(openFile, temp);
AddrBook[entryCnt].setFirstName(temp);
openFile.clear();
getline(openFile, temp);
AddrBook[entryCnt].setLastName(temp);
openFile.clear();
openFile >> tempInt;
//getline(openFile, tempInt);
AddrBook[entryCnt].setStreetNum(tempInt);
openFile.clear();
getline(openFile, temp);
AddrBook[entryCnt].setStreetName(temp);
openFile.clear();
getline(openFile, temp);
AddrBook[entryCnt].setCity(temp);
openFile.clear();
getline(openFile, temp);
AddrBook[entryCnt].setState(temp);
openFile.clear();
openFile >> tempInt;
AddrBook[entryCnt].setZipCode(tempInt);
openFile.clear();
entryCnt = entryCnt + 1;
}
提前感谢您的帮助!
答案 0 :(得分:0)
使用
导致命中和遗漏问题openFile >> tempInt;
接着是
getline(openFile, temp);
读取tempInt
后,换行符仍留在流中。下一次调用getline
只会获取一个空字符串。
您可以在致电阅读getline
后立即向tempInt
添加另一个电话。
openFile >> tempInt;
std::string ignoreString;
getline(openFile, ignoreString);
...
getline(openFile, temp);