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


i trying run string of class functions, each being declared in class header , defined in class.cpp

the problem i'm having skips street name , places in city (shifting off one) , when comes zipcode re enters street number.

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

and 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 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 in advance!

the hit , miss problem cause use of

openfile >> tempint; 

followed by

getline(openfile, temp); 

after tempint read, newline character still left in stream. next call getline grabs empty string.

you can add call getline right after call read tempint.

openfile >> tempint; std::string ignorestring; getline(openfile, ignorestring);  ...  getline(openfile, temp); 

Comments

Popular posts from this blog

routing - AngularJS State management ->load multiple states in one page -

python - GRASS parser() error -

Swift game error message -