How to create a database?

I'm new to C++ programming. I'm trying to make a database that stores the information without overwrite it. I want this program to store the client’s selection in somewhere that doesn't change and also that creates a new storage for the new value any time the client enters a new selection instead of overwriting it. I did something like that in my code but any time the program runs again and the client enters a new selection, the client’s selection is overwritten with the new value. I don’t know if it’s possible to do that (store the client’s selection in somewhere where doesn't change) with C++. I've been reading and I think I can create a database for my program.

THIS IS MY CODE:
#include <iostream>
#include <string>

using namespace std;

// FUNCTIONS
void mainMenu();
void laptopsMenu();
void pcsMenu();
void tabletsMenu();

//Global Variables
string userOrder = " ";

void main ()
{
string personType = " ";

do
{
cout << "Welcome to the Computer Store!" << endl << endl;
cout << "Are you a client or manager?" << endl;
cin >> personType;

if ( personType == "client" )
{
mainMenu(); // Calls Function mainMenu
}

else if ( personType == "manager" )
{
cout << endl << "The client ordered a " << userOrder << "."<< endl << endl;
}
}
while ( personType == "client" | personType == "manager" );

cout << endl << "Thank you for your business. BYE !!! " << endl;

system("pause");
}

//**********************************************************
// FUNCTIONS
//**********************************************************


// mainMenu Function
//***************************************
void mainMenu ()
{
int mainMenuOption = 0;

cout << endl << "Computer Menu Options: " << endl;
cout << "1) Laptop." << endl;
cout << "2) PC." << endl;
cout << "3) Tablet." << endl;
cout << "4) Exit." << endl << endl;
cout << "What kind of computer would you like to order?" << endl;
cin >> mainMenuOption;

switch ( mainMenuOption )
{
case 1:
laptopsMenu(); // Calls Function laptopsMenu
break;
case 2:
pcsMenu(); // Calls Function pcsMenu
break;
case 3:
tabletsMenu(); // Calls Function tabletsMenu
break;
default:
cout << "Thank you for your business. BYE !!! " << endl;
break;
}

if ( mainMenuOption == 1 | mainMenuOption == 2 | mainMenuOption == 3 )
{
cout << endl <<"You just ordered a " << userOrder << "." << endl;
cout << "Thank you for your selection." << endl;
cout << "Your order is at the front desk." << endl << endl;
}
}


// laptopsMenu Function
//***************************************
void laptopsMenu()
{
int laptopsMenuOption = 0;

cout << endl << "Laptop Selection Menu" << endl;
cout << "1) MacBook" << endl;
cout << "2) Asus" << endl;
cout << "3) Dell" << endl;
cin >> laptopsMenuOption;

switch ( laptopsMenuOption )
{
case 1:
userOrder = "Laptop MacBook";
break;
case 2:
userOrder = "Laptop Asus";
break;
case 3:
userOrder = "Laptop Dell";
break;
}

}


// pcsMenu Function
//***************************************
void pcsMenu()
{
int pcsMenuOption = 0;

cout << endl << "PC Selection Menu" << endl;
cout << "1) Alienware" << endl;
cout << "2) Gateway" << endl;
cout << "3) Dell" << endl;
cin >> pcsMenuOption;

switch ( pcsMenuOption )
{
case 1:
userOrder = "PC Alienware";
break;
case 2:
userOrder = "PC Gateway";
break;
case 3:
userOrder = "PC Dell";
break;
}
}


// tabletsMenu Function
//***************************************
void tabletsMenu()
{
int tabletsMenuOption = 0;

cout << endl << "Tablet Selection Menu" << endl;
cout << "1) iPad" << endl;
cout << "2) Nexus 7 (Android)" << endl;
cout << "3) Kindle" << endl;
cin >> tabletsMenuOption;

switch ( tabletsMenuOption )
{
case 1:
userOrder = "Tablet iPad";
break;
case 2:
userOrder = "Tablet Nexus 7 (Android)";
break;
case 3:
userOrder = "Tablet Kindle";
break;
}
}


THANK YOU FOR YOUR HELP
I even dont know if I understand what you want.

You wanne store all the orders of the client?
And when the programm stops? Should the orders, which he did, stored beyond the program end?

In that case, you have to write the orders in a file, and read if you need them.

Independently, if you write a variable, the old content is overwritten. And that is what you do. Everytime the user did a choice, the choice is stored into the same variable (userOrder).

Btw: its not a good idea to use global variables.
Take a look into how program's are compiled and work before trying something like this because program's cannot store data, they're just a list if instructions for the computer. Program's can allocate memory on the computer for temparary storage (the variables) but everything is deleted on app close.
If you want to save data between program runs then you will most definitely need to use file streams... Don't worry, once you set it up its just like std::cin and std::cout.
Check this out for tutorials with using files
http://www.cplusplus.com/reference/fstream/fstream/
Last edited on
Topic archived. No new replies allowed.