• Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.
    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

C++ How I built my age checker in C++

Malcolm

Administrator
Administrator
Verified
Hello Code Forum!

I'm back again with another How I built my series! Today, I want to show you how I built my age checker. In recent news in Canada, we have made cannabis legal. But you have to be of age to purchase. So I decided to build an age checker. I didn't put too much planning into this as it was fairly simple.

What I needed was to ask for users' input (their age), then check to see if it's below, above or at the age requirement.

Must HavesLater Haves
  • Collect user input, check to see if user input is above, below or at the legal requirement.
  • Make sure to include instructions that are understandable.
  • Select from options to check to see if the user is of age.
  • Visual UI.

First, I had to create an integer that will store the age of the user. Within the int main() {}, I added the integer int age; but left the integer empty so it can be filled later. It looks similar to this:
C++:
int age;
However, I now want to user to be able to know when they should enter their age. So now I'm going to display text to the out stream using cout (Character output), std::cout << "Enter your age" << std::endl; (The std::endl signifies that end of the line or else whatever I write will remain on the same line). Let's listen to in-stream, to collect the user input using cin (Character input) and store it into the variable above, std::cin >> age;.

C++:
std::cout << "Enter Age" << std::endl;
std::cin >> age;
I have the required information to determine whether or not the user is of age. So we are now going to create an if statement; if age is equal to and greater (>=) then 19 then the user is of age, else, if it is below then the user is not legal to consume cannabis products. A general concept of an if statement is that if something equals the condition defined then do this, however, you can add onto this so that if it does not equal to the first condition then else do this.

I created the if statement by stating if followed by the condition (age>=19) {, if user is equal to 19 or greater; follow the statement below: std::cout << "You are of legal age to buy and consume weed products." << std::endl;. Now, what happens if the user age does not follow the first condition? Well, then we add else following a statement } else { std::cout << "Opps, You are not of legal age to consume weed products" << std::endl;. The code below is what we should have now:

C++:
if (age>=19) {
    std::cout << "You are of legal age to buy and consume weed products." << std::endl;
} else {
    std::cout << "Opps, You are not of legal age to consume weed products" << std::endl;
}

All together we should have:
All together:
#include <iostream>

int main() {
    int age;
   
    //Ask user their age
    std::cout << "Enter age" << std::endl;
    std::cin >> age;
   
    //If statement to determine if the user inputed number is below, above or at the legal age.
    if (age>=19) { // display if user is above, at or below legal age.
        std::cout << "You are of legal age to buy and consume weed products." << std::endl;
    } else { //Display if user is below legal age.
        std::cout << "Opps, You are not of legal age to buy and consume weed products." << std::endl;
    }
    return 0;
}

I hope that I inspired you guys to learn, practice, build and share! :)
 
Last edited:
Top Bottom