类函数在工作时被击中是错过的,即使它们是相同的(对我来说)

class functions are hit are miss when it comes to working even though they are the same (to me)

本文关键字:对我来说 类函数 错过 工作      更新时间:2023-10-16

我试图运行这串类函数,每个函数都在类头中声明并在类中定义.cpp

我遇到的问题是它跳过了街道名称,然后将其放置在城市中(将所有内容都移开),然后当涉及到邮政编码时,它只是重新输入街道号码。

类.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;

相关类.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;
}

和我的主要.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;
}

提前感谢您的任何帮助!

中和未命中问题是由使用

openFile >> tempInt;

其次

getline(openFile, temp);

读取tempInt后,换行符仍保留在流中。对getline的下一个调用只是获取一个空字符串。

您可以在调用后立即向getline添加另一个调用以读取tempInt

openFile >> tempInt;
std::string ignoreString;
getline(openFile, ignoreString);
...
getline(openFile, temp);