Home » C++ programs » C++ Most popular & searched programs

C++ program to display name and age

In this program, we will read name and age of the person and display them on the output screen. Here, we will learn how to read string (name) with spaces in C++ language?

Here, we are declaring a string (character array) variable named name that will store name of the person and integer variable named age that will store the age of the person.

Program to Read and Display Name and Age in C++



#include <iostream>
using namespace std;

#define MAX_LENGTH 100

int main()
{
	char name[MAX_LENGTH]={0};
	int age;
	
	cout<<"Enter name of the person: ";
	cin.getline(name,MAX_LENGTH);
	cout<<"Enter age: ";
	cin>>age;
	
	cout<<"Name: "<<name<<endl;
	cout<<"Age: "<<age<<endl;
	
	return 0;
}

Output

Enter name of the person: Vanka Manikanth 
Enter age: 25 
Name: Vanka Manikanth
Age: 25

#define MAX_LENGTH 100

This Macro is using to define maximum number of character to declare character array and to read maximum number of character through cin.getline().

cin.getline(name,MAX_LENGTH)

This is a library method of cin object (istream class), which is using to read maximum of MAX_LENGTH (100) characters from the keyboard with spaces.






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.