Write a C++ program to get the age of a student from the user at run time and display it on the screen.


Write a C++ program to get the age of a student from the user at run time and display it on the screen.
Write a C++ program to get the age of a student from the user at run time and display it on the screen.


Write a C++ program to get the age of a student from the user at run time and display it on the screen.

C++ is a powerful programming language that is used to create efficient and effective software applications. In this article, we will discuss how to create a simple program that will allow us to get the age of a student from the user at runtime and display it on the screen.


First, we need to create a C++ program that will ask the user to enter their age. We can do this using the cout and cin commands. The cout command is used to display a message on the screen, while the cin command is used to accept input from the user.


Here's a sample program that prompts the user to enter their age:

#include <iostream>
using namespace std;

int main()
{
    int age;
    cout << "Please enter your age: ";
    cin >> age;
    cout << "Your age is " << age << endl;
    return 0;
}

In this program, we have declared an integer variable age to store the user's input. The cout command displays the message "Please enter your age: " on the screen, and the cin command accepts the user's input and stores it in the age variable. Finally, the program displays the user's age on the screen using the cout command again.


Once the user enters their age, the program stores it in the age variable. We can then use this variable in our program to perform various operations. For example, we can check if the user is old enough to vote or buy alcohol, or we can use their age to calculate their birth year.


Here's an example program that calculates the user's birth year based on their age:


#include <iostream>
#include <chrono>
using namespace std;

int main()
{
    int age;
    cout << "Please enter your age: ";
    cin >> age;

    auto current_time = chrono::system_clock::now();
    auto now_c = chrono::system_clock::to_time_t(current_time);

    int birth_year = 1900 + localtime(&now_c)->tm_year - age;

    cout << "You were born in " << birth_year << endl;
    return 0;
}

In this program, we have used the chrono library to get the current time, and we have calculated the user's birth year based on their age. The localtime function is used to convert the current time into a tm struct, which contains information about the year, month, day, etc. We then subtract the user's age from the current year to get their birth year, and we add 1900 to get the actual year.


Overall, creating a C++ program to get the age of a student from the user at runtime and display it on the screen is a simple task. By using the cin and cout commands, we can accept user input and display output on the screen, and we can use variables to store and manipulate the data entered by the user.