C++ 类函数在工作时被击中或丢失,即使它们是相同的(对我来说)
我试图运行这个类函数字符串,每个类函数都在类头中声明,并在class.cpp中定义 我遇到的问题是,它跳过了街道名称,然后将其放在城市中(将所有内容都从一个位置移开),然后在zipcode中重新输入街道编号 这个班级看起来像C++ 类函数在工作时被击中或丢失,即使它们是相同的(对我来说),c++,C++,我试图运行这个类函数字符串,每个类函数都在类头中声明,并在class.cpp中定义 我遇到的问题是,它跳过了街道名称,然后将其放在城市中(将所有内容都从一个位置移开),然后在zipcode中重新输入街道编号 这个班级看起来像 class AddressBook { private: string firstName; string lastName; int streetNum; string streetName; string city;
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;
#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;
}
相关的class.cpp部分如下所示
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;
#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;
}
提前谢谢你的帮助 命中和未命中问题是由使用
openFile >> tempInt;
接
getline(openFile, temp);
读取tempInt
后,换行符仍保留在流中。对getline
的下一次调用只获取一个空字符串
在调用readtempInt
之后,可以添加另一个对getline
的调用
openFile >> tempInt;
std::string ignoreString;
getline(openFile, ignoreString);
...
getline(openFile, temp);
这起作用了。即使是异性恋男人,我也可以吻你。非常感谢。@Paul:并将成功检查添加到每个I/O操作中!