C++ Basic Program

Introduction: 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!!!!!!!

Step 1: #include Files

step 1- first, we need to include the library's we are going to use:

#include <iostream>

#include <string>

#include <vector>

Step 2: Declaring Our Main Function

step 2- now we need to declare our main function in the program:

int main()

{

return 0;

}

Step 3: 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;

Step 4: Making Our Main Loop

now we need to code the do-while loop:

do

{

} while (selection != "q" and selection != "Q");

Step 5: 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;

Step 6: 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;

Step 7: 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;

}

Step 8: 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;

}

}

Step 9: 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;

}

Step 10: 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;

Step 11: We Are Done!!!!!!!!

now we are done, you may now test your program and have fun with it, thank you!!!!!!

Be the First to Share

    Recommendations

    • Lamps Challenge

      Lamps Challenge
    • Rice & Grains Challenge

      Rice & Grains Challenge
    • Puzzles Challenge

      Puzzles Challenge

    Comments