Home Ask Login Register

Developers Planet

Your answer is one click away!

Paul February 2016

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

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.

the class.h looks like

class AddressBook
    string firstName;
    string lastName;
    int streetNum;
    string streetName;
    string city;
    string state;
    int zipCode;

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


string temp;
    int tempInt;

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;

and my main.cpp section.

while (openFile.good())
    getline(openFile, temp);


R Sahu February 2016

The hit and miss problem is cause by use of

openFile >> tempInt;

followed by

getline(openFile, temp);

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

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

Post Status

Asked in February 2016
Viewed 1,493 times
Voted 9
Answered 1 times


Leave an answer

Quote of the day: live life