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.
First, I had to create an integer that will store the age of the user. Within the
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),
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
All together we should have:
[CODE lang="cpp" title="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;
}[/CODE]
I hope that I inspired you guys to learn, practice, build and share!
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 Haves | Later Haves |
---|---|
|
|
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;
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 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:
[CODE lang="cpp" title="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;
}[/CODE]
I hope that I inspired you guys to learn, practice, build and share!
Last edited: