PPP2 Chapter 21 Drill

57 views
Skip to first unread message

Osman Zakir

unread,
Dec 10, 2017, 11:35:49 AM12/10/17
to PPP-public
Drill number 1 for the vector implementation says: 

Define a struct Item { string name; int iid; double value; /* . . . */ }; , make
a vector<Item> , vi , and fill it with ten items from a file.

But I don't understand what I need to do for the value.  So any help on being able to understand what to do here would be appreciated.

By the way, on Visual Studio 2017, would C++14 compiler settings be good to use when compiling the code for the exercise and example in the book?  VS2017's support for C++17 is only 75% complete in their latest update anyway (they say their C++17 support will be complete by February 2018).   

Art Werschulz

unread,
Dec 10, 2017, 1:42:30 PM12/10/17
to PPP-public
Hi.

> On Dec 10, 2017, at 11:35 AM, Osman Zakir <osman...@gmail.com> wrote:
>
> Drill number 1 for the vector implementation says:
>
> Define a struct Item { string name; int iid; double value; /* . . . */ }; , make
> a vector<Item> , vi , and fill it with ten items from a file.
>
> But I don't understand what I need to do for the value. So any help on being able to understand what to do here would be appreciated.

I don't have the book with me, so I can only ask questions. My first question: What kind of data does the file contain?

Art Werschulz
a...@comcast.net



Osman Zakir

unread,
Dec 10, 2017, 4:17:45 PM12/10/17
to PPP-public
I'm sorry.  I still  need to create a file to use with it.  I just wanted to first determine what values I could use for Items.  One idea is to use prices.  For the ID (which I think is what iid is), I should probably put numbers in order of construction for the Items, where the first Item has ID 1, second one has ID 2, and so on.   

Osman Zakir

unread,
Dec 10, 2017, 4:19:20 PM12/10/17
to PPP-public
And I wonder if it'd be okay to just put the items' names in the file and do everything else manually.  If that can be done. 

Osman Zakir

unread,
Dec 12, 2017, 4:27:24 PM12/12/17
to PPP-public
I tried doing it.  But now it crashes in main when trying to read into the vector past the first Item.  In Visual Studio 2017, I get a debug assertion failure with the message being "Can't access string iterator one past end" or something like that.

I've attached my code and text file.
main.cpp
Item.h
Item.cpp
items.txt

Osman Zakir

unread,
Dec 15, 2017, 5:29:07 AM12/15/17
to PPP-public
Code: https://gist.github.com/DragonOsman/55fdfe564195be3661992391c859ef40

In the debugger, I see that it correctly reads the Items and puts them into the vector. But that when it then goes back to check the loop condition after filling the vector, it triggers an exception that gets caught by my catch(...) block. The trouble is that I don't know what the exception is. There's also only one item in my call stack the whole time (the current line in main that's being executed). There's also one called "External Code" but it never actually goes in there and shows me what's in it.  

The output I get when I try to run it is this:

items.txt
Unknown exception occurred
Please enter a character to exit
k
Press any key to continue . . .

The Drill specs:

1. Define a struct Item { string name; int iid; double value; /* . . . */ }; , make

a vector<Item> , vi , and fill it with ten items from a file.
2. Sort vi by name.
3. Sort vi by iid .
4. Sort vi by value; print it in order of decreasing value (i.e., largest value
first).
5. Insert Item("horse shoe",99,12.34) and Item("Canon S400", 9988,499.95) .
6. Remove (erase) two Item s identified by name from vi .
7. Remove (erase) two Item s identified by iid from vi .
8. Repeat the exercise with a list<Item> rather than a vector<Item> .

I'm on #1 right now.  

Any help would be greatly appreciated.  Thanks in advance. 

Osman Zakir

unread,
Dec 16, 2017, 3:11:32 PM12/16/17
to PPP-public
I got it figured out.  All problems were in operator>> code.  
Reply all
Reply to author
Forward
0 new messages