
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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)