The HighwaySystem ctor assumes valid systems.csv input; it isn't designed to cope with a non-numeric Tier value:
tier = *strtok(0, ";")-48;
We dereference the pointer to the Tier field string to grab just the first character, and convert from a char to the int it represents.
So for a value of QUACK!, we grab the Q, ascii 81, and subtract 48 to arrive at a tier value of 33.
Back at the siteupdate main function, we call hs->is_valid() which returns False because it checks for a tier > 5.
So we delete the invalid HighwaySystem's pointer -- and then attempt to follow it, resulting in reams of garbage output to the terminal.
| if (hs->is_valid()) highway_systems.push_back(hs); |
| elsedelete hs; |
| cout << hs->systemname << '.' << std::flush; |
Change this to:
if (hs->is_valid())
{ highway_systems.push_back(hs); cout << hs->systemname << '.' << std::flush;
}
elsedelete hs; Let's keep the check for a tier value of <1; that's simple enough. Changing tier from an unsigned short to a short will flag more values that are too low, instead of converting negative to large positive numbers.
Leaning toward scrapping the check for a tier > 5:
- The better way to do this would be to use a constant, and that seems a bit mission-creepy.
- The site could conceivably use larger tier #s in the future, and AFAIK larger values pose no problem on the Web side of things.
As far as doing a check for mixed alphanumeric values such as 3A or full-on non-numeric, it comes down to how much we really care about doing this. Edit: Done.
The HighwaySystem ctor assumes valid systems.csv input; it isn't designed to cope with a non-numeric Tier value:
tier = *strtok(0, ";")-48;We dereference the pointer to the Tier field string to grab just the first character, and convert from a
charto the int it represents.So for a value of
QUACK!, we grab theQ, ascii 81, and subtract 48 to arrive at a tier value of 33.Back at the siteupdate
mainfunction, we callhs->is_valid()which returns False because it checks for a tier > 5.So we delete the invalid
HighwaySystem's pointer -- and then attempt to follow it, resulting in reams of garbage output to the terminal.DataProcessing/siteupdate/cplusplus/siteupdate.cpp
Lines 207 to 209 in 54a8482
Change this to:
Let's keep the check for a tier value of <1; that's simple enough. Changing
tierfrom anunsigned shortto ashortwill flag more values that are too low, instead of converting negative to large positive numbers.Leaning toward scrapping the check for a tier > 5:
As far as doing a check for mixed alphanumeric values such as
3Aor full-on non-numeric, it comes down to how much we really care about doing this. Edit: Done.unsigned short->shortreplaceNah. If we make it to the point where a Level value is assigned, we're good. Check for that.HighwaySystem::is_valid()function with a variable. Simpler, and could do a better job at invalid Levels starting with 'a', 'p', or 'd'.