Created
April 21, 2015 04:41
-
-
Save kylemcdonald/940a3d0cc55183d4922e to your computer and use it in GitHub Desktop.
Some of the first C++ code I ever wrote, as a freshman in College.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
#define songsPerCd 12 | |
#define totalCds 5 | |
//this code creates an automatic 2d array | |
bool shoppingCart[totalCds][songsPerCd]; | |
void enterStore(){ | |
cout << "Welcome to the buyTunes Music Store.\n";} | |
bool cdIsPurchased(int i){ | |
int j; | |
int songsOffCd=0; | |
for(j=0;j<songsPerCd;j++){ | |
if(shoppingCart[i][j]){songsOffCd++;}} | |
return (songsOffCd==songsPerCd)?1:0;} | |
bool songIsPurchased(int cdChoice, int songChoice){ | |
return (shoppingCart[cdChoice-1][songChoice-1])?1:0;} | |
void exitStore(){ | |
cout << "You have purchased:\n"; | |
double totalPrice=0; | |
for(int i=0;i<totalCds;i++){ | |
if(cdIsPurchased(i)){ | |
totalPrice+=9.98; //add the price of each CD to the total | |
cout << "\tCD #" << i+1 << " for $9.98\n";} //tell them they have bought that CD | |
else{ //otherwise, report each song on the CD they have bought | |
for(int j=0;j<songsPerCd;j++){ | |
if(shoppingCart[i][j]==1){ | |
totalPrice+=.98; //add the price of each song to the total | |
cout << "\tCD #" << i+1 << ", song #" << j+1 << " for $0.98\n";}}}} | |
cout << "Your total is: $" << totalPrice << "\n" << | |
"Thank you for shopping at the buyTunes Music Store.\n";} | |
void purchaseCD(int cdChoice){ | |
for(int i=0;i<songsPerCd;i++){ | |
shoppingCart[cdChoice-1][i]=1;} | |
cout << "CD #" << cdChoice << " has been added to shopping cart.\n";} | |
void purchaseSong(int cdChoice,int songChoice){ | |
shoppingCart[cdChoice-1][songChoice-1]=1; | |
cout << "CD #" << cdChoice << ", song #" << songChoice << " has been added to shopping cart.\n";} | |
int main(){ | |
char option; | |
enterStore(); | |
do{ //primary loop | |
option=' '; | |
cout << "Command> "; | |
cin >> option; | |
if(option=='s'){ //if the user chooses s, go through the single song purchase | |
int songChoice; | |
int cdChoice; | |
cout << "Purchase a single song from which CD? (1-" << totalCds << ")> "; | |
cin >> cdChoice; | |
if((cdChoice<1)||(cdChoice>totalCds)){ //start over if the CD number is invalid | |
cout << "Invalid CD number entered.\n"; | |
continue;} | |
if(cdIsPurchased(cdChoice)){ //start over if the CD has already been purchased | |
cout << "You have already purchased that CD!\n"; | |
continue;} | |
cout << "Which song from CD #" << cdChoice << "? (1-" << songsPerCd << ")> "; | |
cin >> songChoice; | |
if((cdChoice<1)||(cdChoice>songsPerCd)){ | |
cout << "Invalid song number entered.\n"; | |
continue;} | |
if(songIsPurchased(cdChoice,songChoice)){ //start over if the CD has already been purchased | |
cout << "You have already purchased that song!\n"; | |
continue;} | |
purchaseSong(cdChoice,songChoice);} | |
else if(option=='c'){ //if the user chooses c, go through the entire CD purchase | |
int cdChoice; | |
cout << "Purchase which CD? (1-" << totalCds << ")> "; | |
cin >> cdChoice; | |
if((cdChoice<1)||(cdChoice>totalCds)){ | |
cout << "Invalid CD number entered.\n"; | |
continue;} | |
if(cdIsPurchased(cdChoice)){ //start over if the CD has already been purchased | |
cout << "You have already purchased that CD!\n"; | |
continue;} | |
purchaseCD(cdChoice);} | |
else{ | |
cout << "Invalid option.\n";}} | |
while(option!='q'); //keep in the "command>" loop until the user decides to quit | |
exitStore();} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment