C++ Basic Program
in this program you will learn the basics of c++ by coding a simple c++ program to make multiple users and display these users, hope you enjoy!!!!!!!
#include Files
step 1- first, we need to include the library's we are going to use:
#include <iostream>
#include <string>
#include <vector>
Declaring Our Main Function
step 2- now we need to declare our main function in the program:
int main()
{
return 0;
}
Declaring Our Variables
now we need to declare all the variables we are going to use in the program:
std::string user;
char selection;
std::vector users;
Making Our Main Loop
now we need to code the do-while loop:
do
{
} while (selection != "q" and selection != "Q");
Making the Main Menu
now inside the main loop lets code are main menu:
std::cout << "--------------------------------------------" << std::endl;
std::cout << "a - add a user" << std::endl;
std::cout << "d - display all users" << std::endl;
std::cout << "q - quit" << std::endl;
Asking for the User Selection
now we need to ask the user what they would like to do and store that in the selection variable:
std::cout << "please pick a selection";
std::cin >> selection;
Checking If the User Input Was = "A"
now if the user put "A", we need to make a user and store it in our vector:
if (selection == 'a' or selection == 'A')
{
std::cout << "please type the users name";
std::cin >> user;
users.push_back(user);
std::cout << "the user was successfully added" << std::endl;
}
Checking If the User Input Was = "D"
if the user input was "D" and not "A" then we need to display all the users in our vector using a simple for loop:
else if (selection == 'd' or selection == 'D')
{
std::cout << "here are all of the users: " << std::endl;
for (auto use: *users.data())
{
std::cout << use << std::endl;
}
}
Checking If the User Input Was = "Q"
if the user input was = "Q" then we need to say "goodbye" then the loop will terminate and so will the program:
else if (selection == 'q' or selection == 'Q')
{
std::cout << "goodbye" << std::endl;
}
Checking for Anything Else
if the use put in anything else that we do not understand we will display an error message:
else
std::cout << "hmmmm, I do not recognize that command" << std::endl;
We Are Done!!!!!!!!
now we are done, you may now test your program and have fun with it, thank you!!!!!!