Question:
Store parts of line as variables in C++?
P8ntballplayah
2013-04-10 15:17:09 UTC
Let's say I have 2 lines of input in a text file, each line containing an age (int), name, weight (double), and height (int) of a person. The individual data on a given line is always separated by a space.

Example:
16 NICK 140.6 68
59 TOM 250.2 77

I have a dynamic array of structures, where a struct has the above variables. If I create a string variable, say string input, then I can do the following to print each set of data:
getline(fileName, input, ' ');
while(fileName) {
cout << input << endl;
getline(fileName, input, ' ');
}

This reads until the next space or newline and prints what it read. What I'm trying to do is instead take what it read and store that as the respective variable in my array. (i.e. set 16 to the age of person 1, NICK to the name of person 1, etc). I run into problems because input is a string, so all non-string values don't work.

I'm sure there are advanced features to do this, but based on what you can see I'm using above, is there a way to do what I'm trying to accomplish?
Thanks for any help
Three answers:
MichaelInScarborough
2013-04-10 19:26:04 UTC
I am almost sure that you are looking for something like the following, but I am not sure, whether or not with your current knowledge you could possibly comprehend this code. You could e-mail me, in case questions arise, ask your teacher, class mates, etc.

Some explanations follow:

The file format is as described by you.



If using cin to read name into a string you need to follow up with a cin to a char in order to read out the blank separating the name from the weight.



As you can see the function read_file with two lines of code handles the file read in problem. In order to do so your class (here Person) needs to implement an operator overload operator>> otherwise the read in will not work



The function display_vector is even shorter (one line, only) and requires an overload of operator<< in the Person class.



Differences from your request:

There is a function called getline (defined in )

Check here for the definition:

http://www.cplusplus.com/reference/string/string/getline/?kw=getline

i.e. getline(cin, name) would be how you could read in one line until a \n is found.

My suspicion is that you are talking about this, but I am really unsure.



So in case you could narrow this down, e-mail me and I would append changes as necessary to this article.





Most of all: Have fun!



#include

#include // for setw, fixed, setprecision

#include // for copy

#include // open files for input ifstream

#include // vector

using namespace std;



class Person {

private:

int age;

string name;

double weight;

int height;

public:

Person(const int& cAge = 0,

const string& cName = "",

const double& cWeight = 0.0,

const int& cHeight = 0) : age(cAge), name(cName), weight(cWeight), height(cHeight)

{

}

const string& get_name() const {return name;}

enum {AGE, NAME, WEIGHT, HEIGHT};

static const int iWidth[];

friend ostream& operator<<(ostream& os, const Person& p)

{

os << setw(iWidth[AGE]) << p.age << " " <<

setw(iWidth[NAME]) << p.name << " " <<

setw(iWidth[WEIGHT]) << fixed << setprecision(2) << p.weight << " " <<

setw(iWidth[HEIGHT]) << p.height;

return os;

}

friend istream& operator>>(istream& is, Person& p)

{

char c;

is >> p.age >> p.name >> c >> p.weight >> p.height;

return is;

}

};

const int Person::iWidth[] = {4, 20, 10, 10};



void read_file(const string& strFile, vector& p)

{

ifstream is(strFile.c_str());

copy(istream_iterator(is), istream_iterator(), back_inserter(p));

}



void read_regular(const string& strFile, vector& p)

{

ifstream is(strFile.c_str());

char c;

int age, height;

string name;

double weight;

while(is >> age >> name >> c >> weight >> height)

p.push_back(Person(age, name, weight, height ));

}

void display_vector(const vector& p)

{

copy(p.begin(), p.end(), ostream_iterator(cout, "\n"));

}

void display_regular(const vector& p)

{

for (int i = 0; i < p.size(); i++) {

cout << p[i] << endl;

}

}

int main(int argc, char *argv[])

{

vector v;

// read_file(argv[1], v);

read_regular(argv[1], v);

// display_vector(v);

display_regular(v);

return 0;

}
anonymous
2013-04-10 16:17:16 UTC
struct Person

{

int age;

string name;

double weight;

int height;

struct Person* next;

};



Person *root;

int main ()

{

Person* current_p; //use something like this if you are making your own list

Person person_object;//use this for template array(vector actually can shrink and grow)

..

..

..

//to read it in, something like, (I assume you know how to use new)

for(count = 0; true; count++)

{

if(fileName >> current_p-> age >> current_p->name

>> current_p-> weight >> current_p-> height )

{

//handle adding it to your dynamic list

//perhaps your are using c++ template array, then never mind my "next" pointer and

// current_p, you would just use person_array.push_back ( person_object);

current_p = new Person;

}

else // fileName is likely at end of file

{

delete current_p;

//whatever else clean up you need to do

break;

}

}// end of for loop



fileName.close();

..

..

}
vagle
2016-11-02 17:50:50 UTC
you will would desire to verify the weights in a loop. you realize how many there are, numberOfBoxes. and you realize a thank you to verify the 1st one. So in simple terms wrap your final 2 traces in a for loop: for(i=0;i


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...