CSc 300 – Assignment #1 solution

Write a complete C++ program to process the store inventory for KAG Video Games. • Maximum of 8 video games in inventory. o Use an array of structure variables to store the collection of individual video game attributes. • Read the video game attributes from a text file whose file name is entered using the command line. o If no file name is specified then prompt the user for the file name. • Each line in the file contains attributes for one video game (no blank lines). o GAME UNIT COST UNITS Data File Example: // free format layout Rage 4.96 3 // 0 or more blanks in front of the game field Fallout 34.95 13 // 1 or more blanks in front of the other fields
Solution Algorithm (do not reorder tasks): 1. Display a message on the screen that describes to the user what the program does. o Part of the required program documentation (separate introduction function). 2. Read one video game at a time from the data file (read one entire line as a single large string value). o Build each individual video game attribute value for the current video game one character at a time: • game, unit cost, units (see note 2 below) o Store each extracted video game attribute value in the appropriate structure variable field within the inventory array using appropriate data types (only the game attribute is stored as a string value). 3. Calculate the individual video game attributes: o Total Cost: unit cost times the number of units (see note 3 below) 4. Sort the video games in reverse alphabetical order (z..a, Z..A) o Use a sorting algorithm with a separate swap function (bubble sort, …) 5. Calculate the overall store inventory summary: o Unit Cost: average video game unit cost o Units: average video game units o Total Cost: average video game unit cost times the average video game units (see note 3 below) 6. Display the individual video game attributes and the store inventory summary using the format below.
Notes: 1. Each step (1..6) is a separate task (each task requires at least one separate function). 2. Build all string values using the exact same technique (see the assignment example). o Define and reuse a single function that builds exactly one small string value from a larger string value. o Call once per video game attribute: game, unit cost, units (call 3 times for each video game). 3. Individual video game values and store values are calculated using the exact same technique. o Define and reuse functions to be used for both the individual video games and the store. o Call once per video game and once for the store. KAG Video Games Game Unit Cost Units Total Cost
Witcher 49.99 10 $ 499.90 Skyrim 59.00 0 $ 0.00 Rage 4.96 3 $ 14.88 Fallout 34.95 13 $ 454.35 Borderlands 9.49 6 $ 56.94
Averages 31.68 6 $ 190.07
Completely document the program file. • See documentation requirements on the course web site.
E-mail the program file to the account listed on the course syllabus using the following naming convention. • username1.cpp // I would use: gamradtk1.cpp
List the course number (300), your username, and the assignment number as the message SUBJECT: • csc300 – username – a1 // I would use: csc300 – gamradtk – a1
Required user-define data types:
GameType structure data type used to describe one video game LineType C style character string data type Maximum of 60 characters – don't forget the NULL character
Basic Program Layout:
void getFilename( const int, const char* [], LineType ) // global function declarations // other required functions
int main( const int argc, const char* argv[] ) // general solution algorithm getFilename( argc, argv, filename ); while ( !inFile.getline( gameLine, MAX_LINE, ‘\n’ ).eof( ) ) // ‘\n’ terminates – ‘\0’ stored // process current line // LineType gameLine; // build each individual video game attribute one at a time as a string value // game, unit cost, units // store each individual video game attribute using an appropriate data type // process individual video game attributes // sort video games // process store inventory attributes // display individual video game attributes and store inventory attributes
Use conventions discussed in class: 1. Identifier naming: variables, constants, functions, user-defined data types 2. Use of constants and arrays: MAX_GAMES, MAX_LINE, … 3. Use of user-defined data types 4. Do NOT use global variables under any circumstances o ONLY constants, user-defined data types, and function declarations/prototypes where appropriate 5. Etc…
Additional resources needed: 1. cstring C style string tools 2. cstdlib system(“clear”) and atoi, atof – convert C style string values to numbers: int, double, …
sortInventory( inventory, size ) // Bubble Sort pseudocode for pass 1..size-1 for comp 1..size-pass if inventory[…].game < inventory[…].game // C style strings swapGames // Use separate swap function – REQUIRED // Only pass the two video games being swapped