类函数命中时,即使他们是相同的(对我)工作, [英] class functions are hit are miss when it comes to working even though they are the same (to me)

查看:124
本文介绍了类函数命中时,即使他们是相同的(对我)工作,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图运行这个字符串的类函数,每个都在类标题中声明并在class.cpp

I trying to run this string of class functions, with each being declared in the class header and defined in class.cpp

中定义。它跳过街道名称,然后将它放在城市(移动一切关闭一个),然后当它涉及到邮政编码,它只是重新输入街道号码。

the problem i'm having is that it skips the street name and then places it in city (shifting everything off one) and then when it comes to zipcode it just re enters the street number.

class.h看起来像

the class.h looks like

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部分如下所示

and the relevant class.cpp section looks like

#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部分。

and my main.cpp section.

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;
}

感谢您提供任何帮助!

Thanks for any help in advance!

推荐答案

使用

openFile >> tempInt;

后跟

getline(openFile, temp);

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

After tempInt is read, the newline character is still left in the stream. The next call to getline just grabs an empty string.

您可以添加另一个调用 getline 紧接在读取 tempInt 之后。

You can add another call to getline right after the call to read tempInt.

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

...

getline(openFile, temp);

这篇关于类函数命中时,即使他们是相同的(对我)工作,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆