Publishing System Settings Logout Login Register
Creating a simple a User Data holder in C++
TutorialCommentsThe AuthorReport Tutorial
Tutorial Avatar
Rating
Add to Favorites
Posted on October 16th, 2005
7587 views
C++ General
In this tutorial I shall teach you how to make a simple temporary database of user info in C++. I will teach you as if you know almost nothing, and go line by line, step by step.

I expect you to know basics of C++ though I will go in detail alot, and some areas I will not go into detail that much because it either relates to an area already explained or you should know it already.

#include <iostream>
#include <string>


This is the basics of all C++ code, depending on what you are doing in C++ you either need more #include statements, or you might not need the #include statement. These two lines tell the compiler that you are going to be using the functions in those two libraries.
iostream is a library of functions that contain all the basics in C++, such as statements "cout", "cin", most of the basic datatypes for variables.
string is the library that lets you use the string variables.

#include <iostream>
#include <string>

using namespace std;


using namespace std basically means, you are using std for the statements so you dont have to type std:: in front of each statement, if we didnt have this we would have to type things such as
std::cin >> variable

so using namespace std; just makes our lives easier by typing less :)

#include <iostream>
#include <string>

using namespace std;

int main() {


return 0;
}


main is the main function that every C++ program consists of, a function is a block of code that usually only does a limited amount of work in the program. Good programmers do NOT make their whole code in just one function, they usually break it up into multiple functions, and each function usually only does one or two things for the program.

#include <iostream>
#include <string>

using namespace std; //allows us not to type std:: in front of most statement

int main() {

int menuChoice; //variable declaration.
int static track = 0;

return 0;
}


Here we declare two variables we will be using in the program, one is the menu choice, we will be adding a menu that allows the user to choose what they want to do with numbers, so therefore we declared it as an int, or integer.
Integer is a type of variable that only allows whole numbers such as -3, -2, -1, 0, 1, 2, 3
Long is the integer type that allows even bigger numbers than type int.
short is the integer type that saves memory and doesnt let the number get higher than a certain point.(which is less than int)
float/double are the two data types that allow you to use decimals such as 3.2, 1.25. The difference between float and double is, float has a certain limit to how big the number can be, so datatype double is doubled the amount, and double the memory. Though the memory is not an issue anymore as we have super computers :P


int static track = 0;


This piece of code declares track as an integer, but static, which means it makes sure the computer remembers the value of track throughout the program even when its not needed anymore, the = 0 part sets the value of track to 0.

#include <iostream>
#include <string>

using namespace std; //allows us not to type std:: in front of most statement

int menu(int &menuChoice); // declare our function

int main() {

int menuChoice; //variable declaration.
int static track = 0;

do {
menu(menuChoice);
switch(menuChoice) {
case 1:
break;
case 2:
break;
}
} while(menuChoice != 3);

return 0;
}

int menu(int &menuChoice) {

return menuChoice;
}


Okay, this piece of code may be overwhelming but really its quite easy.

int menu(int &menuChoice); // declare our function


in this piece it does exactly what the comment says, we have declared our function and called it menu, it takes in parameter int &menuChoice, I named it the same as the other variable in function main so you can see whats going on, we will be passing that same variable into this function. We put the & in front so we pass the address of the variable. The address is the location of the variable being passed in memory, so it the value can be changed.

Then as you can see at the bottom of the code we have

int menu(int &menuChoice) {

return menuChoice;
}


This is the function, you write it just like main. Also the Declaration of the function MUST be before the function calling it, so lets say function main, calls function menu. If the declaration of function menu is not before function main, you will get a compile error. You can also put your functions at the top of your code so you do not have to declare them, your choice.

The
return menuChoice;

means to return the value of menuChoice back to the function calling it. If you noticed we have return 0; at the bottom of function main, return 0; means return to the operating system, or end program.

do {
menu(menuChoice);
switch(menuChoice) {
case 1:
break;
case 2:
break;
}
} while(menuChoice != 3);


This area might seem crazy but really its not. the first thing is to look at is
do {
}while(menuChoice != 3);

this means do whats inbetween the { } while variable menuChoice is not equal to 3.
Basically this is a loop, and it will not end until menuChoice is equal to 3. We will only be having 2 options, Enter user info, and list all users, the number 3 means quit, so the main loop will quit and the program will exit when menuChoice is equal to 3.

menu(menuChoice);

This calls function menu and passes the variable menuChoice to the function to use.

switch(menuChoice) {
case 1:
break;
case 2:
break;
}


This is called a switch statement, switch statements work with only data types, char, and integers. We will not be getting into chars in this tutorial as they are very complicated and to time consuming to explain in this tutorial.
Switch statement basically goes through and checks which statemenst are true or not. If its not true than it skips it. so
case 1:

means if menuChoice equals 1, then do whats below case 1:, we dont have anything below yet but we will. If its not equal to 1, it goes to case 2:, if its not equal to case 2, it exits the loop. the break; means break the loop, exit the loop without continuing. Often values change and it might go into another case when you dont want it to, make sure you have a break statement after each case.

#include <iostream>
#include <string>

using namespace std; //allows us not to type std:: in front of most statement

int menu(int &menuChoice); // declare our function

int main() {

int menuChoice; //variable declaration.
int static track = 0;

do {
menu(menuChoice);
switch(menuChoice) {
case 1:
break;
case 2:
break;
}
} while(menuChoice != 3);

return 0;
}

int menu(int &menuChoice) {

do {
cout << \"- Menu -\" << endl;
cout << \"1 - Enter user and info\" << endl;
cout << \"2 - List all users and info\" << endl;
cout << \"3 - Quit\" << endl;
cout << \"nPlease enter your choice: \";
cin >> menuChoice;
if( menuChoice > 3 || menuChoice < 1) {
cout << \"nPlease select an existing option!n\" << endl;
}
} while(menuChoice > 3 || menuChoice < 1);

return menuChoice;
}


In this changed area, we just finished the function menu. we have the same do while loop around the whole thing except different expectations at the bottom
while(menuChoice > 3 || menuChoice < 1);

means while menuChoice is greater then 3 or while menuChoice is less than 1 continue the loop. we do this so they cannot enter an option that is not on the menu. Then we have a few statements. cout means print to the screen, anything you want to say must be in quotes, if its a variable you just type the variable, endl means end line and goto the next line.
cout << \"- Menu -\" << endl;

This means print out - Menu - and end line.
the "<<" are required you put it inbetween each variable and line you want to print. You will see what I mean later.
cin means read in the user input to the given variable
cin >> variable


and remember, the "<<" means cout, and the ">>" means cin.
So in the function we just print out a few lines with cout, read in their answer with cin, check it, and if its correct, return the value to function main.
Then if you look right after where we called the function menu, its the dwitch statement I explained, which means right when we finish with function menu and return the value of menuChoice, it will go into checking the "case" for menuChoice.

We are going to veer off the main topic for a couple minutes, and explain exactly what we will be holding our user data in. We will be holding it in whats called a "struct" which is basically your own data type. when declaring a struct you type:
struct Name {

};


the "Name" is the name you want your struct to be called. it looks like a function except it doesnt take parameters in, AND it has a semicolon right after the last brace, dont forget that or you will get a compile error.
Structs are very hard to explain so I will try my best, you basically put anything in a struct that is related, like if you want to hold information about a car, you could make a struct called cars with variables that have to do with cars. Or, for our example, info about people we will be using these things:

struct person {
string firstName;
string lastName;
int age;
double height;
};


string means a string of letters...for example "This is a string" anything can be a string, numbers, letters, special characters. So we have firstName, lastName which are both strings, age which is an integer, and height which is a double, remember int and double can only take in numbers.

What I meant by struct being your own data type is exactly that, if you want a variable to use the struct then it must be declared like this:

person variableName


person is the name of the struct, and then the variable name.

#include <iostream>
#include <string>

using namespace std;

struct person {
string firstName;
string lastName;
int age;
double height;
};

int menu(int &menuChoice);
void userInfo(person user[], int size, int const track); // Declare two new functions
void listUser(person user[], int size, int const track);

int main()
{
int menuChoice;
int static track = 0;
person user[50]; // Declare our variable that uses our struct
do {
menu(menuChoice);
switch(menuChoice) {
case 1:
userInfo(user, 50, track);
track++;
break;
case 2:
listUser(user, 50, track);
break;
}
} while(menuChoice != 3);

return 0;
}

int menu(int &menuChoice)
{
do {
cout << \"- Menu -\" << endl;
cout << \"1 - Enter user and info\" << endl;
cout << \"2 - List all users and info\" << endl;
cout << \"3 - Quit\" << endl;
cout << \"nPlease enter your choice: \";
cin >> menuChoice;
if( menuChoice > 3 || menuChoice < 1) {
cout << \"nPlease select an existing option!n\" << endl;
}
} while(menuChoice > 3 || menuChoice < 1);

return menuChoice;
}

void userInfo(person user[], int size, int const track) // function userInfo
{

return;
}

void listUser(person user[], int size, int const track) // Function list users
{

return;
}


The only difference in this code is we declared two new functions, userInfo which will be where you enter users info, and listUser which is where it lists all the users. We have also added a new variable
person user[50];

this is the variable that uses the struct I described. Now this is called an array, any variable with [] is an array. This array has 50 slots, which means this program can hold up to 50 users. I will explain Arrays a bit.

lets say we have an array called array1[5] so this means it has 5 slots.
________
|_|_|_|_|_|

Each box can hold one data if its an integer, then each box can hold one integer, instead of having 100 variables that do all the samething we can just have 1 variable that holds 100 slots.
the first slot starts at 0 then counts up, so if we were to call the first slot on array1, we would say array1[0], to call upon slot 2, we would say array1[1] and so on. Because our array is data type "person" (the datatype we created in the struct if you forgot :P ) we can hold each person in one box.

To call each part of the struct we would just say user[0].firstName
the .firstName means call firstname from slot 1 in array user from struct "person", then we can do user[0].age to get the first persons age or record his/her age.

void userInfo(person user[], int size, int const track);


This declares the function userInfo if you didnt guess that already, we pass in variable user[], the square brackets tells it, its an array. int size tells it the size of the array, and const track means pass in track as a constant, which means the value cannot be changed no matter what in this function. We will be using the track to count how many users are in, so if track = 0; then 0 users are in and we will be entering the user in
user[0].firstName


but instead we can also use a variable to enter in the square brackets so the tracker can be used to tell the function where to place the user.
user[tracker].firstName


listUser has the same parameters and we show the functions at the bottom like usual.

If you look at the switch statement you will see I added stuff under the case, if
case 1:
userInfo(user, 50, track);
tracker++;
break;


that calls userInfo, sends variable user, the number 50 for int size, and track for the track. After it goes through userInfo it adds 1 to tracker.

++ means add 1 to whatever variable precedes it.
-- means subtract 1 from whatever variable precedes it.

then break the loop.

case 2 runs the userList then breaks the loop, same parameters so just read whats above to know whats going on with that also.
The reason these two functions have void in front of them is because they do not return any value, they just return to the function that called them. So we just put void.

#include <iostream>
#include <string>

using namespace std;

struct person {
string firstName;
string lastName;
int age;
double height;
};

int menu(int &menuChoice);
void userInfo(person user[], int size, int const track); // Declare two new functions
void listUser(person user[], int size, int const track);

int main()
{
int menuChoice;
int static track = 0;
person user[50]; // Declare our variable that uses our struct
do {
menu(menuChoice);
switch(menuChoice) {
case 1:
userInfo(user, 50, track);
track++;
break;
case 2:
listUser(user, 50, track);
break;
}
} while(menuChoice != 3);

return 0;
}

int menu(int &menuChoice)
{
do {
cout << \"- Menu -\" << endl;
cout << \"1 - Enter user and info\" << endl;
cout << \"2 - List all users and info\" << endl;
cout << \"3 - Quit\" << endl;
cout << \"nPlease enter your choice: \";
cin >> menuChoice;
if( menuChoice > 3 || menuChoice < 1) {
cout << \"nPlease select an existing option!n\" << endl;
}
} while(menuChoice > 3 || menuChoice < 1);

return menuChoice;
}

void userInfo(person user[], int size, int const track) // function userInfo
{
cout << \"Enter your first name:\";
cin >> user[track].firstName;
cout << \"Enter your last name:\";
cin >> user[track].lastName;
cout << \"Enter your age:\";
cin >> user[track].age;
cout << \"Enter your height(cm, ex: 150):\";
cin >> user[track].height;
return;
}

void listUser(person user[], int size, int const track) // Function list users
{
int count;
cout << \"nnUsers\" << endl;
cout << \"---------------\" << endl;
for(count = 0; count < track; count++) {
cout << \"First name: \" << user[count].firstName << endl;
cout << \"Last Name: \" << user[count].lastName << endl;
cout << \"Age: \" << user[count].age << endl;
cout << \"Height: \" << user[count].height << \"cm\" << endl;
cout << \"---------------nn\" << endl;
}
return;
}


We have added in the rest of function of userInfo which should be pretty self explanatory. remember user[track] is just the variable with a number in the square brackets disguised as a word, the track only tells it where to go.

We have also added in the rest of function listUser, this has a for loop in it a for loop keeps going until the condition is met.

we declare the variable count to be the counter.
track already has a the number of people in the "database" so we will be sure that is the limit.
for(count = 0; count < track; count++)

means count = 0, and while count < track count++ and execute the body of the for loop until count < track is not true anymore.
C++ is smart and only executes the count = 0 part only once, and count++ is added after the first run through of the loop, if we wanted to add one to the count before the first runthrough we would put
++count


The body of the loop just prints out the info which you should know how that works through the steps of this tutorial.
Pressing 3 quits the program, because of the do while loop at the end of function main, != means not equal too.

so your final code should look like this and run without errors.


#include <iostream>
#include <string>

using namespace std;

struct person {
string firstName;
string lastName;
int age;
double height;
};

int menu(int &menuChoice);
void userInfo(person user[], int size, int const track);
void listUser(person user[], int size, int const track);

int main()
{
int menuChoice;
int static track = 0;
person user[50];
do {
menu(menuChoice);
switch(menuChoice) {
case 1:
userInfo(user, 50, track);
track++;
break;
case 2:
listUser(user, 50, track);
break;
}
} while(menuChoice != 3);

return 0;
}

int menu(int &menuChoice)
{
do {
cout << \"- Menu -\" << endl;
cout << \"1 - Enter user and info\" << endl;
cout << \"2 - List all users and info\" << endl;
cout << \"3 - Quit\" << endl;
cout << \"\nPlease enter your choice: \";
cin >> menuChoice;
if( menuChoice > 3 || menuChoice < 1) {
cout << \"\nPlease select an existing option!\n\" << endl;
}
} while(menuChoice > 3 || menuChoice < 1);

return menuChoice;
}

void userInfo(person user[], int size, int const track)
{

cout << \"Enter your first name:\";
cin >> user[track].firstName;
cout << \"Enter your last name:\";
cin >> user[track].lastName;
cout << \"Enter your age:\";
cin >> user[track].age;
cout << \"Enter your height(cm, ex: 150):\";
cin >> user[track].height;
return;
}

void listUser(person user[], int size, int const track)
{
int count;
cout << \"\n\nUsers\" << endl;
cout << \"---------------\" << endl;
for(count = 0; count < track; count++) {
cout << \"First name: \" << user[count].firstName << endl;
cout << \"Last Name: \" << user[count].lastName << endl;
cout << \"Age: \" << user[count].age << endl;
cout << \"Height: \" << user[count].height << \"cm\" << endl;
cout << \"---------------\n\n\" << endl;
}
return;
}
Premium Publisher
Dig this tutorial?
Thank the author by sending him a few P2L credits!

Send
l3lueMage

This author is too busy writing tutorials instead of writing a personal profile!
View Full Profile Add as Friend Send PM
Pixel2Life Home Advanced Search Search Tutorial Index Publish Tutorials Community Forums Web Hosting P2L On Facebook P2L On Twitter P2L Feeds Tutorial Index Publish Tutorials Community Forums Web Hosting P2L On Facebook P2L On Twitter P2L Feeds Pixel2life Homepage Submit a Tutorial Publish a Tutorial Join our Forums P2L Marketplace Advertise on P2L P2L Website Hosting Help and FAQ Topsites Link Exchange P2L RSS Feeds P2L Sitemap Contact Us Privacy Statement Legal P2L Facebook Fanpage Follow us on Twitter P2L Studios Portal P2L Website Hosting Back to Top