Issue with C++ using getline and vector -
i have issue reading line file using getline , using stringstream separate different variables using comma delimiter. issue standard cout of variables shows seatdes properly, using vector name instead of seatdes. not sure why happening.
a standard line in file: jane doe,04202013,602,1a
#include <iostream> #include <fstream> #include <string> #include <sstream> #include <vector> #include <cstdlib> #include "reservation.h" int main(int argc, char **argv) { std::ifstream flightfile; std::string name, date, seatdes, flightnum, line; int error = 0, conflightnum; flightfile.open("reservations.txt"); if(!flightfile) { //returns error value if there problem reading file error = 1; return error; } else { //start reading files , sticks them class object , sticks object vector set while (std::getline(flightfile, line)) { std::istringstream ss(line); std::getline(ss, name, ','); std::getline(ss, date, ','); std::getline(ss, flightnum, ','); conflightnum = atoi(flightnum.c_str()); ss >> seatdes; reservation newres(name, date, conflightnum, seatdes); std::cout << name << std::endl << date << std::endl << conflightnum << std::endl << seatdes << std::endl; std::cout << "vector component" << std::endl; std::cout //<< newres.getname() << std::endl << newres.getdate() << std::endl << newres.getflightnum() << std::endl << newres.getseatdesg() << std::endl; } } flightfile.close(); return 0; }
reservation.h file
class reservation { private: std::string name, seatdesg, date; int flightnum; public: //default constructor reservation(){} //big constructor reservation(std::string name, std::string date, int flightnum, std::string seatdesg) { this->name = name; this->seatdesg = name; this->date = date; this->flightnum = flightnum; } //setters void setname(std::string name) { this->name = name; } void setflightnum(int flightnum) { this->flightnum = flightnum; } void setseatdesg(std::string seatdesg) { this->seatdesg = seatdesg; } void setdate(std::string date) { this->date = date; } //getters std::string getname() { return name; } std::string getseatdesg() { return seatdesg; } std::string getdate() { return date; } int getflightnum() { return flightnum; } };
reservation(std::string name, std::string date, int flightnum, std::string seatdesg) { this->name = name; this->seatdesg = name; // here problem this->date = date; this->flightnum = flightnum; }
should be
this->seatdesg = seatdesg;
Comments
Post a Comment