| fun2code (1227) | |
|
That's an appealing idea but you must read each item into the correct type of variable. You pretty much have to read the data member by member. You can shorten and simplify the code somewhat by making GetData() a member function of the Details class. Unqualified member names can appear in a member function. | |
|
Last edited on
|
|
| faieq92 (150) | |
|
shouldn't this work? something is terribly wrong I would have thought that would cycle through the array and enter details through it. | |
|
Last edited on
|
|
| faieq92 (150) | |
|
File_Input>>DataEntry(Data[p]); This definetly wont work? | |
|
|
|
| faieq92 (150) | |
|
You can shorten and simplify the code somewhat by making GetData() a member function of the Details class. Unqualified member names can appear in a member function. I have no idea what this means? or what you want me to do | |
|
|
|
| fun2code (1227) | |||
You can do this: File_Input>>Data[p]; but you need to define what ">>" means to a Details object.A definition for >> would look like this:
Then, File_Input>>Data[p]; will work.Recall earlier when you asked "What do you mean by generalize?" and I answered "By overloading >> for a Details object so it can be read in like that."? This is what I meant. I think it's the cleanest way to read the data from a file for a Details object. | |||
|
Last edited on
|
|||
| faieq92 (150) | |
|
I see. Does this go where functions are before int main. also im having trouble understanding this (istream& is, const Details& detail)? especially what istream& is means | |
|
|
|
| faieq92 (150) | |||
and ive tried it and it still doesn't work (x86)\microsoft visual studio 10.0\vc\include\istream(429): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(long double &)' 1> with 1> [ 1> _Elem=char, 1> _Traits=std::char_traits<char> 1> ] 1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\istream(447): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(void *&)' 1> with 1> [ 1> _Elem=char, 1> _Traits=std::char_traits<char> 1> ] 1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\istream(466): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(std::basic_streambuf<_Elem,_Traits> *)' 1> with 1> [ 1> _Elem=char, 1> _Traits=std::char_traits<char> 1> ] 1> while trying to match the argument list '(std::istream, const std::string)' ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ========== | |||
|
|
|||
| faieq92 (150) | |
| long list of errrors that look like that | |
|
|
|
| fun2code (1227) | |
|
Yes, that code goes before int main. istream is an "input stream" object. It could be a file input stream (ifstream), or it could be the input stream from the console (cin). File_Input will be passed as the "input stream" object 'is', and Data[p] would be passed as the Details object 'detail' to the operator>> function in this call: File_Input >> Data[p];In the operator definition istream& operator>>( istream& is, const Details& detail ) 'is' is the "left operand" and detail is the "right operand" in is >> detailI know it's a big conceptual jump to overloading operators, but it is very useful. And, the & in istream& means it is being passed "by reference" (so its value can be changed by the function) instead of by value (actually, not possible for a stream object. They are non-copyable). | |
|
Last edited on
|
|
| faieq92 (150) | |||
ive also tried this but hit a dead end as I don't know how I can get it to cycle through each one eg first to name then last name then age etc. | |||
|
|
|||
| faieq92 (150) | |
|
visual studio doesn't like this for some reason | |
|
Last edited on
|
|
| fun2code (1227) | |||
|
I don't see what you're trying to do there. What is temp and where did it come from? I think that should be like this instead:
Actually, put the definition for operator>> before this function too. | |||
|
Last edited on
|
|||
| faieq92 (150) | |
| removed. | |
|
Last edited on
|
|
| fun2code (1227) | |||
|
I can't keep up with your posts. I just saw this above:
You modified the code. Why? What you put in there makes no sense being there. (Back to the "one object is not itself 10 objects" thing. I will post back soon to put it all together in one place for you. We've gotten a bit lost here. | |||
|
Last edited on
|
|||
| faieq92 (150) | |||
my very own attempt .... | |||
|
Last edited on
|
|||
| fun2code (1227) | |||
|
It's good to try what makes sense to you. There was an error with the definition for operator>>. It was all about the const in istream& operator>>( istream& is, const Details& detail ). This is wrong. My bad. It causes about 100 errors.Here is the necessary code together in one place, so we can get on track here. It's getting late for me here.
This builds error free for me. It should read 10 sets of values for Details objects from "test.txt", though it's hard to tell because that's all the program does. You could add code to output the Data[i] after the file read. Call your GetData() function. EDIT: I added code for calling GetData. | |||
|
Last edited on
|
|||
| faieq92 (150) | |
| Thanks for all the help. And it worked. Now just gotta understand why it work. | |
|
|
|
| fun2code (1227) | |
|
You're welcome. Thank you for giving me something interesting to do during commercial breaks all day! You made some big jumps with using classes. It may take a while to absorb. Classes represent a powerful tool with many cool features. | |
|
|
|
| Chervil (1203) | |||||||
|
I can foresee problems with the format of the data in the file. It's already apparent from the sample presented earlier:
There are two related problems. One, individual pieces of data may contain spaces. UK postcodes are normally shown with a space, such as "LU3 1UL" and a street name may consist of several individual words, for example "St. Catherines Avenue". Secondly, a piece of data may be missing. For example you may have a street address but no email, or vice versa. A solution to both of these problems is to place all of the data for an individual on a single line in the file. Each item is separated by a delimiter, such as a comma or a tab. Then, this function will change from this:
to this:
That will handle data which looks like this, where '\t' is the tab character:
Of course this change will need to be mirrored in the code which outputs the data to the file. That change is relatively straightforward, simply outputting the tab delimiter between the items, and a single newline at the end of each record. Note, the code I posted here could be shortened a bit. I left it like that to show what is going on. | |||||||
|
Last edited on
|
|||||||
| faieq92 (150) | |
| so \t is a tab? | |
|
|
|