C++ || Dynamic Arrays – Create A Music Store Database Which Sorts CD Information & Display Grand Total
This program was presented as a homework assignment in a programming class to demonstrate the use of dynamic arrays, and pointer variables. The pointer variables which are displayed in this program are very excessive; and many are not needed, but it was good practice when trying to understand the logic behind it all.
REQUIRED KNOWLEDGE FOR THIS PROGRAM
Pointer Variables
Dynamic Arrays
2-D Dynamic Arrays - How To Declare
Bubble Sort
While Loops
For Loops
Constant Variables
Functions
Switch Statements
Toupper
Strcpy
Strcmp
This is an interactive program, which simulates a database for a music store, in which the user inputs data into the program (artist, CD title, genre, sales price, tax) and stores that information into multiple dynamic arrays (2-D and/or one dimensional dynamic arrays). The program will also apply any discounts that may currently be available for the selected CD genre, and applies that discount to the current CD information. When the user chooses to quit, the program will sort any data which is currently stored inside the array (by artist) in ascending order, and output the subtotal, tax applied, and grand total for all of the CD information which is entered into the array to the user.
After the program is complete, it will display a summary of the data which was stored into the array like so:
1 2 3 4 5 6 7 8 9 |
My Programming Notes Record Company Sales Report for 2/9/2012 Artist Title SalesPrice Genre DiscountRate SubTotal SalesTax GrandTotal Name1 CD1 12.99 Jazz 18.00% 10.65 1.26 11.91 Name2 CD2 12.99 Rap 7.00% 12.08 1.26 13.34 -------------------------------------------------------------------------------------------------------- Total (2 transactions) 22.73 2.52 25.25 |
NOTE: On some compilers, you may have to add #include < cstdlib> and #include < cstring> in order for the code to compile.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 |
// ============================================================================= // This is an interactive program, which simulates a database for a music store // in which the user inputs data into the program (atrist, CD title, genre, // sales price, tax) and stores the data into a dynamic array. The program will // also apply any discounts that may currently be available to the selected CD // genre, and applies that discount to the current CD. When the user choses // to quit, the program will sort any data that is stored inside the array // (by artist) in ascending order and it will output the subtotal, tax applied, // and grand total for all of the CD information which is entered into the array // ============================================================================= #include <iostream> #include <iomanip> #include <string> #include <ctime> using namespace std; // function prototypes double* GetDiscounts(); void DisplayMenu(); void GetCDInfo(char** &artist, char** &CDTitle, char** &genre,double* &salesPrice, double* &taxRate, int* numItems); void GetDiscountRate(double* salesPrice,char** musicGenre, char** genre,int* numItems, double* &discountPercent,double* discountRate); void GetSubTotal(double* salesPrice, double* discountRate, int* numItems,double* subTotal); void GetTaxAmount(double* salesPrice, double* taxRate, int*numItems,double* salesTax); void GetGrandTotal(double* subTotal,double* salesTax,int* numItems,double* grandTotal); void SortData(char**artist, char**CDTitle, double*salesPrice, char**genre,double*discountPercent, double*subTotal, double*salesTax, double*grandTotal, int* numItems); void DisplayData(char**artist, char**CDTitle, double*salesPrice, char**genre,double*discountPercent, double*subTotal, double*salesTax, double*grandTotal, int* numItems); // constant discount values // these values are defines within the // 'GetDiscounts' function const double* discounts = GetDiscounts(); // ============================================================================= // main // ============================================================================= int main() { //-- Declare Variables START ---// // numItems for artist array int* numItems = new int(0); // 2-D Dynamic array for music genres char** musicGenre = new char*[7]; for(int index=0; index < 7; ++index) {musicGenre[index] = new char[13];} musicGenre[0]= "Classical"; musicGenre[1]= "Country"; musicGenre[2]= "International"; musicGenre[3]= "Jazz"; musicGenre[4]= "Pop"; musicGenre[5]= "Rock"; musicGenre[6]= "Rap"; // 2-D Dynamic Array for artist name char** artist = new char*[50]; for(int index=0; index < 50; ++index) {artist[index] = new char[20];} // 2-D Dynamic array for CD titles char** CDTitle = new char*[50]; for(int index=0; index < 50; ++index) {CDTitle[index] = new char[20];} // 2-D Dynamic array of music genre char** genre = new char*[50]; for(int index=0; index < 50; ++index) {genre[index] = new char[20];} // array for list price double* salesPrice = new double[20]; // array for tax rate double* taxRate = new double[20]; // array for discount amount double* discountRate= new double[20]; // array for sales price double* subTotal = new double[20]; // array for taxamount double* salesTax= new double[20]; // array for grandTotal double* grandTotal = new double[20]; // array for discount percent double* discountPercent = new double[20]; // variable for the while loop menu char* userInput = new char('n'); // variable if the user wants to enter more data char* enterMoreData = new char('y'); //-- Declare Variables END ---// // display menu DisplayMenu(); cin >> userInput; // loop thru menu options while((toupper(*userInput) !='Q')) { switch(toupper(*userInput)) { case 'E': // get artist info GetCDInfo(artist, CDTitle, genre, salesPrice, taxRate, numItems); // get discount GetDiscountRate(salesPrice, musicGenre, genre, numItems,discountPercent, discountRate); // get sub total GetSubTotal(salesPrice, discountRate, numItems,subTotal); // get tax amount GetTaxAmount(salesPrice, taxRate, numItems,salesTax); // get cash price GetGrandTotal(subTotal, salesTax, numItems,grandTotal); // ask if they want to enter more data cout << "nDo you want to enter more CD's? (y/n): "; cin >> *enterMoreData; if(toupper(*enterMoreData)=='Y') { (++*numItems); } else { (++*numItems); *userInput='q'; } break; case 'D': DisplayData(artist, CDTitle, salesPrice, genre, discountPercent,subTotal,salesTax, grandTotal, numItems); break; default: cout<<"nYou have selected an invalid command"; break; } //creates a line separator cout<<endl; cout<<setfill('-'); cout<<left<<setw(30)<<""<<right<<setw(30)<<""<<endl; // if user wants to enter more data, display menu to screen if(toupper(*enterMoreData)=='Y') { DisplayMenu(); cin >> userInput; } }// end of while loop // sort the current data inside the array SortData(artist, CDTitle, salesPrice, genre, discountPercent,subTotal,salesTax, grandTotal, numItems); // display the current data inside the array DisplayData(artist, CDTitle, salesPrice, genre, discountPercent,subTotal,salesTax, grandTotal, numItems); return 0; }// End of Main // ============================================================================= // DisplayMenu // displays the menu to the user // ============================================================================= void DisplayMenu() { cout<<"Welcome to the CD Management System!"; cout<<"nFrom the following menu, select an option"; cout<<"nnE - Enter new CD information into the database"; cout<<"nD - Display the current information in the database"; cout<<"nQ - Quit"; cout<<"nn>> "; }// End of DisplayMenu // ============================================================================= // DisplayData // displays the current contents in the array. If there is nothing in the array // the program promts a message to the user // ============================================================================= void DisplayData(char**artist, char**CDTitle, double*salesPrice, char**genre, double*discountPercent, double*subTotal, double*salesTax, double*grandTotal, int* numItems) { double* totSalePrice = new double(0); double* totSalesTax = new double(0); double* totgrandTotal = new double(0); // variables which will get the current date time_t t = time(0); // get current CPU time struct tm * now = localtime(& t); // if array is empty, display message if(*numItems ==0) { cout<<"nThe database is currently empty!n"; } else { // displays the company header to the user cout<<setfill(' '); cout << "nttttMy Programming Notes Record Company" << "ntttt Sales Report for "<<now->tm_mon + 1<<"/"<<now->tm_mday<<"/"<<now->tm_year + 1900<<"nn"; // displays the categories which will be shown to the user cout <<setw(1)<<right<<"Artist"<<setw(13)<<"Title"<<setw(13)<<"SalesPrice" <<setw(13)<<"Genre"<<setw(20)<<"DiscountRate"<<setw(13)<<"SubTotal" <<setw(13)<<"SalesTax"<<setw(13)<<"GrandTotal"; // displays the data which is currently inside the array's cout<<fixed<<setprecision(2); for(int* index = new int(0); *index < *numItems; ++*index) { cout << endl; cout <<setw(1)<<right<<artist[*index]<<setw(15)<<CDTitle[*index] <<setw(13)<<salesPrice[*index]<<setw(15)<<genre[*index]<<setw(13) <<discountPercent[*index]*100<<"%"<<setw(15)<<subTotal[*index]<<setw(16) <<salesTax[*index]<<setw(13)<<grandTotal[*index]; } // finds the total prices for the entire list of CD's for(int* index = new int(0); *index < *numItems; ++*index) { *totSalePrice+= subTotal[*index]; *totSalesTax+= salesTax[*index]; *totgrandTotal+= grandTotal[*index]; } // creates a line separator cout<<setfill('-'); cout<<endl<<endl<<left<<setw(52)<<""<<right<<setw(52)<<""<<endl; // displays the total to the user cout<< "Total ("<<*numItems<<" transactions)ttttttt" <<*totSalePrice<<"tt"<<*totSalesTax<<"t"<<*totgrandTotal<<endl; } }// End of DisplayData // ============================================================================= // SortData // sorts all the data which is currently present in the arrays via bubble sort // this function does not display any data to the user // ============================================================================= void SortData(char**artist, char**CDTitle, double*salesPrice, char**genre,double*discountPercent, double*subTotal, double*salesTax, double*grandTotal, int* numItems) { // bool which will tell us if sorting is complete bool sorted = false; // temporary variables for sorting purposes only char* tempArtist = new char[20]; char* tempCDTitle= new char[20]; char* tempGenre= new char[20]; double* tempListPrice= new double[20]; double* tempDiscountPercent = new double[20]; double* tempsubTotal = new double[20]; double* tempTaxAmt = new double[20]; double* tempgrandTotal = new double[20]; // this is the bubble sort // which sorts the entries by artist in ascending order while (sorted==false) { sorted=true; for(int* index= new int(0); *index < *numItems-1; ++*index) { // checks the artist to see if they are in the correct order if (strcmp(artist[*index],artist[*index+1]) > 0) { // swaps artist places strcpy(tempArtist, artist[*index]); strcpy(artist[*index], artist[*index+1]); strcpy(artist[*index+1], tempArtist); // swaps CD title strcpy(tempCDTitle, CDTitle[*index]); strcpy(CDTitle[*index], CDTitle[*index+1]); strcpy(CDTitle[*index+1], tempCDTitle); // swaps music genre strcpy(tempGenre, genre[*index]); strcpy(genre[*index], genre[*index+1]); strcpy(genre[*index+1], tempGenre); // swaps the CD price *tempgrandTotal = grandTotal[*index]; grandTotal[*index] = grandTotal[*index+1]; grandTotal[*index+1] = *tempgrandTotal; // swaps the tax amount *tempTaxAmt = salesTax[*index]; salesTax[*index] = salesTax[*index+1]; salesTax[*index+1] = *tempTaxAmt; // swaps the sales price *tempsubTotal = subTotal[*index]; subTotal[*index] = subTotal[*index+1]; subTotal[*index+1] = *tempsubTotal; // swaps the discount percent *tempDiscountPercent = discountPercent[*index]; discountPercent[*index] = discountPercent[*index+1]; discountPercent[*index+1] = *tempDiscountPercent; // swaps the list price *tempListPrice = salesPrice[*index]; salesPrice[*index] = salesPrice[*index+1]; salesPrice[*index+1] = *tempListPrice; // sets the 'sorted' variable to false sorted=false; }// end of if statement }// end for loop }// end while loop }// End of SortData // ============================================================================= // GetGrandTotal // calculates the grand total for the current transaction // ============================================================================= void GetGrandTotal(double* subTotal,double* salesTax,int* numItems,double* grandTotal) { grandTotal[*numItems]= (subTotal[*numItems])+(salesTax[*numItems]); }// End of GetgrandTotal // ============================================================================= // GetTaxAmount // calculates the sales tax for the current transaction // ============================================================================= void GetTaxAmount(double* salesPrice, double* taxRate, int*numItems,double* salesTax) { salesTax[*numItems] = (salesPrice[*numItems])* (taxRate[*numItems]); }// End of GetTaxAmount // ============================================================================= // GetSubTotal // gets the subtotal for the current transaction // ============================================================================= void GetSubTotal(double* salesPrice, double* discountRate, int* numItems,double* subTotal) { subTotal[*numItems] = (salesPrice[*numItems]) - (discountRate[*numItems]); }// End of GetsubTotal // ============================================================================= // GetDiscountRate // gets the discount rate for the currently selected CD // ============================================================================= void GetDiscountRate(double* salesPrice,char** musicGenre, char** genre,int* numItems, double* &discountPercent, double* discountRate) { // comapres the user inputted current genre to the pre-defined // genres as defined in the main function, then assigns a // discount to the current CD if(strcmp(genre[*numItems],musicGenre[0]) == 0) // if music genre is Classical {discountPercent[*numItems] = discounts[0];} else if(strcmp(genre[*numItems],musicGenre[1]) == 0) // if music genre is Country {discountPercent[*numItems] = discounts[1];} else if(strcmp(genre[*numItems],musicGenre[2]) == 0) // if music genre is International {discountPercent[*numItems] = discounts[2];} else if(strcmp(genre[*numItems],musicGenre[3]) == 0) // if music genre is Jazz {discountPercent[*numItems] = discounts[3];} else if(strcmp(genre[*numItems],musicGenre[4]) == 0) // if music genre is Pop {discountPercent[*numItems] = discounts[4];} else if(strcmp(genre[*numItems],musicGenre[5]) == 0) // if music genre is Rock {discountPercent[*numItems] = discounts[5];} else if(strcmp(genre[*numItems],musicGenre[6]) == 0) // if music genre is Rap {discountPercent[*numItems] = discounts[6];} else{discountPercent[*numItems] = discounts[4];} // if music genre is not any of these ^, then there is no discount // assign the discount rate to the current CD discountRate[*numItems] = (discountPercent[*numItems]) * (salesPrice[*numItems]); }// End of GetDiscountRate // ============================================================================= // GetCDInfo // obtains the CD information from the user // ============================================================================= void GetCDInfo(char** &artist, char** &CDTitle, char** &genre, double* &salesPrice, double* &taxRate, int* numItems) { cin.ignore(); cout << "nEnter the name of the artist: "; cin.getline(artist[*numItems],50, 'n'); cout << "nEnter the title of the CD: "; cin.getline(CDTitle[*numItems],50, 'n'); cout << "nEnter the genre: "; cin.getline(genre[*numItems], 20); cout << "nEnter the sales price $"; cin >> salesPrice[*numItems]; cout << "nEnter the sales tax: "; cin >> taxRate[*numItems]; }// End of GetInfo // ============================================================================= // GetDiscounts // returns the discount rate for the selected genre // ============================================================================= double* GetDiscounts() { double* temp = new double[7]; temp[0] = .09; // discount rate for Classical temp[1] = .03; // discount rate for Country temp[2] = .11; // discount rate for International temp[3] = .18; // discount rate for Jazz temp[4] = .00; // discount rate for Pop temp[5] = .10; // discount rate for Rock temp[6] = .07; // discount rate for Rap return temp; }// http://programmingnotes.org/ |
QUICK NOTES:
The highlighted lines are sections of interest to look out for.
The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.
Once compiled, you should get this as your output
Welcome to the CD Management System!
From the following menu, select an optionE - Enter new CD information into the database
D - Display the current information in the database
Q - Quit>> e
Enter the name of the artist: Nm8
Enter the title of the CD: CD8
Enter the genre: Rock
Enter the sales price $12.99
Enter the sales tax: .098
Do you want to enter more CD's? (y/n): y------------------------------------------------------------
Welcome to the CD Management System!
From the following menu, select an optionE - Enter new CD information into the database
D - Display the current information in the database
Q - Quit>> e
Enter the name of the artist: Nm7
Enter the title of the CD: CD7
Enter the genre: Country
Enter the sales price $10.99
Enter the sales tax: .078
Do you want to enter more CD's? (y/n): y------------------------------------------------------------
Welcome to the CD Management System!
From the following menu, select an optionE - Enter new CD information into the database
D - Display the current information in the database
Q - Quit>> e
Enter the name of the artist: Nm6
Enter the title of the CD: CD6
Enter the genre: Pop
Enter the sales price $11.50
Enter the sales tax: .067
Do you want to enter more CD's? (y/n): y------------------------------------------------------------
Welcome to the CD Management System!
From the following menu, select an optionE - Enter new CD information into the database
D - Display the current information in the database
Q - Quit>> e
Enter the name of the artist: Nm5
Enter the title of the CD: CD5
Enter the genre: Jazz
Enter the sales price $12.24
Enter the sales tax: .045
Do you want to enter more CD's? (y/n): y------------------------------------------------------------
Welcome to the CD Management System!
From the following menu, select an optionE - Enter new CD information into the database
D - Display the current information in the database
Q - Quit>> e
Enter the name of the artist: Nm4
Enter the title of the CD: CD4
Enter the genre: Other
Enter the sales price $12.99
Enter the sales tax: .094
Do you want to enter more CD's? (y/n): y------------------------------------------------------------
Welcome to the CD Management System!
From the following menu, select an optionE - Enter new CD information into the database
D - Display the current information in the database
Q - Quit>> e
Enter the name of the artist: Nm3
Enter the title of the CD: CD3
Enter the genre: Classical
Enter the sales price $11.45
Enter the sales tax: .078
Do you want to enter more CD's? (y/n): y------------------------------------------------------------
Welcome to the CD Management System!
From the following menu, select an optionE - Enter new CD information into the database
D - Display the current information in the database
Q - Quit>> e
Enter the name of the artist: Nm2
Enter the title of the CD: CD2
Enter the genre: International
Enter the sales price $10.99
Enter the sales tax: .093
Do you want to enter more CD's? (y/n): y------------------------------------------------------------
Welcome to the CD Management System!
From the following menu, select an optionE - Enter new CD information into the database
D - Display the current information in the database
Q - Quit>> e
Enter the name of the artist: Nm1
Enter the title of the CD: CD1
Enter the genre: Rap
Enter the sales price $12.99
Enter the sales tax: .0975
Do you want to enter more CD's? (y/n): n
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
My Programming Notes Record Company Sales Report for 2/9/2012 Artist Title SalesPrice Genre DiscountRate SubTotal SalesTax GrandTotal Nm1 CD1 12.99 Rap 7.00% 12.08 1.27 13.35 Nm2 CD2 10.99 International 11.00% 9.78 1.02 10.80 Nm3 CD3 11.45 Classical 9.00% 10.42 0.89 11.31 Nm4 CD4 12.99 Other 0.00% 12.99 1.22 14.21 Nm5 CD5 12.24 Jazz 18.00% 10.04 0.55 10.59 Nm6 CD6 11.50 Pop 0.00% 11.50 0.77 12.27 Nm7 CD7 10.99 Country 3.00% 10.66 0.86 11.52 Nm8 CD8 12.99 Rock 10.00% 11.69 1.27 12.96 -------------------------------------------------------------------------------------------------------- Total (8 transactions) 89.16 7.85 97.01 |
Leave a Reply