C++ program to make student type data and display in proper format


Suppose we have provided the first name, last name, age and class of a student in different lines. We shall have to write a program using structs in C++ to read them all and show in this format (age, first_name, last_name, class). The age and class will be of type integer, and first_name and last_name are of time string.

So, if the input is like

priyam
kundu
16
10

then the output will be (16, priyam, kundu, 10)

To solve this, we will follow these steps −

  • define a structure with first_name, last_name of type string and age, cl of type integer

  • read each line and store it into first_name, last_name, age, cl respectively into a student type data block stud

  • display student info in this fashion (stud.age, stud.first_name, stud.last_name, stud.cl)

Example

Let us see the following implementation to get better understanding −

#include <iostream>
using namespace std;
struct Student{
    int age, cl;
    string first_name, last_name;
};
int main() {
    Student stud;
    cin >> stud.first_name >> stud.last_name >> stud.age >> stud.cl;
    cout << "(" << stud.age << ", " << stud.first_name << ", " << stud.last_name << ", " << stud.cl << ")";
}

Input

priyam
kundu
16
10

Output

(16, priyam, kundu, 10)

Updated on: 07-Oct-2021

103 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements