类函数在工作时被击中是错过的,即使它们是相同的(对我来说)
class functions are hit are miss when it comes to working even though they are the same (to me)
我试图运行这串类函数,每个函数都在类头中声明并在类中定义.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);
相关文章:
- 奇怪的(对我来说)返回声明 - 在谷歌上找不到任何关于它的信息
- 为什么我的类函数不使用我的全局变量?
- 对自定义元素向量进行排序时出现意外(至少对我来说)行为
- 对于我的类函数,我得到双重释放或损坏错误
- 这对我来说真的很难,我该怎么办
- 错误C4430:丢失类型指定词 - 假设INT.注意:C 不支持对我的构造函数的默认设置
- mod 功能对我来说无法正常工作
- 在这种情况下,为什么Sfinae对我来说不正确以及如何修复它
- 使用嵌套环以简单(对我来说很难)输出
- 是否可以安全地假设64位指针中的16个高位(2个对我来说足够了)是未设置的
- 无法将字符串传递给我的类函数
- 无符号和有符号int(对我来说)之间的比较似乎是必需的(c++)
- 第4章 斯特劳斯特鲁普钻头一个具有挑战性的一步(至少对我来说!
- 对我的构造函数的未定义引用
- libc ++is_copy_constructible对我来说似乎是错误的
- 对我来说,使 boost::statechart::state_machine 线程安全的最简单方法是什么?
- 来自 gcc 原子标头的对我的类静态字段错误的未定义引用
- 未定义的引用错误对我来说没有任何意义
- C++ - 获取对我的类的未定义引用
- 类函数在工作时被击中是错过的,即使它们是相同的(对我来说)