Hello syeare,
Sorry I ended going to bed after my last post.
For example, why did you use the std:: prefix even though you already declared that you're using the std namespace? |
Using
using namespace std;
. This is not the best idea even though it seams to make your code easier to write. This may be useful now, but someday it
WILL cause a problem. I have left out the using statement for so long that typing "std::" is now just habit. Putting it in the code with the using statement makes no difference to the compiler.
Why is the function on L15 made twice (empty first)? |
It is not made twice. In a class or a struct the compiler will provide a default ctor, constructor, and a default dtor, destructor. Line 16 is an overloaded ctor and when you define this in a class or a struct the compiler no longer provides a default ctor, so you have to provide your own. In this program you can get by without a default ctor, but it is a good habit to get into.
The other part of this is since you have provided a default and overloaded ctor, but nothing for the dtor the compiler will provide a default dtor.
Why does the for loop have to be changed from an int to a size_t? |
When you look at the line
for (int i = 0; i < dataToRead.size(); i++)
the size function on "dataToRead.size()" returns a "size_t" type response. By changing the "int" to a "size_t" the "i < dataToRead.size()" now have matching data types. The "int" does work, but produces a compiler warning about the data types not matching.
Finally and most importantly, I'm sorry but it doesn't seem to work? |
What part does not work. Be more specific.
Going back to the struct. Defining the overloaded ctor is to work with
dataToRead.emplace_back(ID, lineData);
. As I understand it "emplace_back" will first construct an object of what it needs before using "push_back" to add it to the vector. By putting the 2 variables in the () you construct an object with the proper information before adding it to the vector.
"push_back" would work, but "emplace_back" is much easier. Using "push_back" you first would have to create an object of the struct like
ProductData temp;
, put the read information into the temp struct then use the "push_back" to add it to the vector.
Using "binary" for the output file is not always necessary. Since you read a ".txt" file you should write to a ".txt" file. When I left the "binary" mode and looked at the output file I noticed that the normal "CR/LF" had changed to just a "LF". This did not seam to make any difference just reading the file, but should you ever have a need to read this file, ("new.txt") it could be a problem.
The first clue is the ".txt" extension. This usually means there is no need for anything special, like "binary" mode.
The other part of using "binary" mode is that you usually read or write in chunks of data not individual variables. And in "binary" mode numbers can be stored differently than in text mode. Writing a string may not be a problem in "binary" mode, but trying to read it back could be a problem. Also there is the possibality that a string greater than 15 characters may not all write to a "binary" file.
Regarding structs, I have been reading http://www.cplusplus.com/doc/tutorial/structures/ , but I don't get how I should manipulate that vector to get my desired results. A for loop to iterate through inventory ?
That link is a start, but have a look at
https://www.learncpp.com/ and specifically
https://www.learncpp.com/cpp-tutorial/47-structs/
I have found this to be a good tutorial and a good place for reference.
As to the for loop I left that to see what you would come up with.
Right or wrong post your attempt and we can fix it.
In
lastchance's example he overloaded the "<<" and ">>" operators. This is the better approach, but if you are not ready for this yet the for loop will work. I would suggest staying with the for loop for now until you have a better understanding of how to access the
vector of structs and the
vector inside the struct, ( hint, hint, nudge, nudge).
Andy