For my junior high, IT final project, I decided to make a C++ program which functions as a simple library manager. The functions of the manager are to check your own library, view the store's library and buy from it, and check your transaction history.
I made separate arrays for each of the three above, and use it as a main database storage system. I added some extra functions such as to check if the data already has been purchased and if the person has sufficient funds to buy the data. The code is can be read at : http://pastebin.com/UQW0sAaQ
/* dycesM */
#include <iostream>
#include <string>
#include <string.h>
using namespace std;
int money = 5000; // Global Variable
struct database
{
string songName;
int songPrice;
int songNumber;
int songID;
}userStorage[30], storeStorage[30];
struct transaction
{
string songName;
int songPrice;
}transactionStorage[30];
void userStorageDisplay()
{
for (int i = 0; i <= 30; i++)
{
if (userStorage[i].songID != NULL)
{
cout << "\n" << i << ". " << userStorage[i].songName << "\n" << endl;
}
}
}
void storeStorageDisplay()
{
for (int i = 0; i <= 30; i++)
{
if (storeStorage[i].songID != NULL)
{
cout << "\n" << i << ". " << storeStorage[i].songName << endl;
cout << "\t" << "Price: $" << storeStorage[i].songPrice << "\n" << endl;
}
}
}
void transactionStorageDisplay()
{
for (int i = 0; i < 30; i++)
{
if (transactionStorage[i].songPrice != NULL)
{
cout << "\n" << transactionStorage[i].songName << " $:" << transactionStorage[i].songPrice << "\n" << endl;
}
}
}
int checkDuplicate(int songStorageValue)
{
for (int j = 0; j <= 30; j++)
{
if (userStorage[j].songID == storeStorage[songStorageValue].songID)
{
return 0;
}
}
return 1;
}
int checkBankDetails(int songNumber)
{
if (storeStorage[songNumber].songPrice > money)
{
return 1;
}
}
void purchase(int songNumber)
{
int verification;
int &moneyBalance = money;
cout << "\nAre you sure you wish to purchase: " << storeStorage[songNumber].songName << " for a price of: " << storeStorage[songNumber].songPrice << " ?" << endl;
cout << "Press 1 to confirm or 2 to exit." << endl;
cin >> verification;
if (verification == 1)
{
if (checkBankDetails(songNumber) != 1)
{
if (checkDuplicate(songNumber) == 1)
{
for (int i = 0; i <= 30; i++)
{
if (userStorage[i].songID == NULL)
{
userStorage[i].songName = storeStorage[songNumber].songName;
userStorage[i].songNumber = i;
userStorage[i].songPrice = storeStorage[songNumber].songPrice;
userStorage[i].songID = storeStorage[songNumber].songID;
for (int a = 0; a <= 30; a++)
{
if (transactionStorage[a].songPrice == NULL)
{
transactionStorage[a].songName = storeStorage[songNumber].songName;
transactionStorage[a].songPrice = storeStorage[songNumber].songPrice;
money = money - transactionStorage[a].songPrice;
cout << "\nRemaining Value: $" << money << "\n";
a = 30; // To end the loop.
}
}
cout << "\n" << storeStorage[songNumber].songName << " has been added to your library at position number: " << i << "\n" << endl;
i = 30; // To end the loop.
}
}
}
else if (checkDuplicate(songNumber) == 0)
{
cout << "\nYou already own this song. Purchase Cancelled." << endl;
}
}
else if (checkBankDetails(songNumber) == 1)
{
cout << "\nInsufficient funds. Purchase cancelled." << endl;
}
}
else if (verification != 1)
{
cout << "Transaction Aborted.";
}
}
void main()
{
char rerun;
//Store Data added for demonstration purposes.
storeStorage[0].songName = "Daughter - Youth";
storeStorage[0].songPrice = 25;
storeStorage[0].songID = 2000;
storeStorage[1].songName = "Archive - Bullets";
storeStorage[1].songPrice = 25;
storeStorage[1].songID = 2001;
storeStorage[2].songName = "Swedish House Mafia - Don't you worry child";
storeStorage[2].songPrice = 25;
storeStorage[2].songID = 2002;
storeStorage[3].songName = "Roykossop - Running to the sea";
storeStorage[3].songPrice = 25;
storeStorage[3].songID = 2003;
storeStorage[4].songName = "French Teen Idol - Shouting can have different meanings";
storeStorage[4].songPrice = 25;
storeStorage[4].songID = 2004;
do
{
int userOperationChoice;
int userPurchaseQuery;
cout << "Hello and Welcome to the Dyces Song Library. \n\nPlease select one of the operations below. \n\n 1 - View your own library \n\n 2 - View the Store Library \n\n 3 - View your transactions. \n\n 4 - Check Bank Details \n\n Selection:";
cin >> userOperationChoice;
switch (userOperationChoice)
{
case(1) :
{
goto userLibrary;
break;
}
case(2) :
{
goto storeLibrary;
break;
}
case(3) :
{
goto transactionLibrary;
break;
}
case(4) :
{
goto bankDetails;
break;
}
}
userLibrary:
system("CLS");
cout << "\nThe current songs in your library are: \n\n";
userStorageDisplay();
goto programEnd;
storeLibrary:
system("CLS");
cout << "\nThe Store's library is: \n";
storeStorageDisplay();
cout << "\nPlease enter the song you wish to purchase. \n";
cin >> userPurchaseQuery;
purchase(userPurchaseQuery);
goto programEnd;
transactionLibrary:
system("CLS");
cout << "Your transactions are: \n";
transactionStorageDisplay();
goto programEnd;
bankDetails:
char viewTransactionHistory;
system("CLS");
cout << "You have: $" << money << " left in your bank account." << endl;
cout << "\nWould you like to check your transaction history? Y/N" << endl;
cin >> viewTransactionHistory;
if (viewTransactionHistory == 'y' || viewTransactionHistory == 'Y')
{
goto transactionLibrary;
}
goto programEnd;
programEnd:
cout << "\nWould you like to go back to the main menu? Y/N" << endl ;
cin >> rerun;
system("CLS");
} while (rerun == 'y' || rerun == 'Y');
}
Any suggestions / reviews are greatly welcome!
goto
are bad. Any future answers should address that. – Jamal♦ Jan 5 at 3:18goto
to a great extent, just to make the code a bit more readable I guess. I'm trying to find a workaround for the global variable – Mayur Mohan Jan 5 at 3:22g++
, I tend to use the flags-Wall -Wextra -Weffc++ -Wstrict-aliasing -pedantic
as a minimum. I'm not very familiar with the flags on any other compilers unfortunately :) – Corbin Jan 5 at 5:21CLS
, andvoid main
(that none of Linux compilers accept). Enable at least/W4
in project settings (or even/Wall
, but this will be really strict - but that's good in my opinion). Fix all warnings, this way you will learn to code properly. – xfix Jan 5 at 8:59