Show person Information using Structure and Function in C++


Example Program : 1

The code defines a C++ struct named Person, which has three members: name (an array of characters), age (an integer), and salary (a floating-point number). The main() function creates an instance of the Person struct, p, and prompts the user to enter values for the name, age, and salary members using the cin object. However, after getting the input, it doesn't display any data of Person object.

The displayData() function takes a Person object as an argument and is intended to display the values of its members on the console. However, the function is currently commented out, so it won't execute. To display the data entered by the user in the main() function, we need to call the displayData() function with the p object as an argument after the user inputs are collected.

Source Code

#include<iostream>
using namespace std;
struct Person
{
  char name[50];
  int age;
  float salary;
};
void displayData(Person);
int main()
{
  Person p;
  cout<<"Enter Full name: ";
  cin.get(p.name, 50);
  cout<<"Enter age: ";
  cin>>p.age;
  cout<<"Enter salary: ";
  cin>>p.salary;
  return 0;
}
void displayData(Person p)
{
  // cout<<"\nDisplaying Information."<<endl;
  // cout<<"Name: "<<p.name<<endl;
  // cout<<"Age: "<<p.age<<endl;
  // cout<<"Salary: "<<p.salary;
}
To download raw file Click Here

Output

Enter Full name: Ram
Enter age: 23
Enter salary: 20000


Example Program : 2

The code defines a C++ struct named Person, which has three members: name (an array of characters), age (an integer), and salary (a floating-point number). The getData() function takes a Person object as an argument and prompts the user to enter values for its members using the cin object. The function then returns the updated Person object.

The displayData() function takes a Person object as an argument and displays its member values on the console. The main() function creates an instance of the Person struct, p, and calls the getData() function to collect user input for its members. It then calls the displayData() function to display the values of p on the console.

This implementation is a good approach to collect user input for a Person object and display its member values. One improvement that can be made is to validate user input before using it. For example, we could check if the age and salary values entered by the user are within acceptable ranges.

Another improvement could be to use std::string instead of a char array for the name member of the Person struct, which would simplify the input/output operations and avoid potential buffer overflows. Overall, this implementation is a good starting point for a basic Person struct implementation in C++.

Source Code

#include<iostream>
using namespace std;
struct Person
{
  char name[50];
  int age;
  float salary;
};
Person getData(Person);
void displayData(Person);
int main()
{
  Person p;
  p = getData(p);
  displayData(p);
  return 0;
}
Person getData(Person p)
{

  cout<<"Enter Full name: ";
  cin.get(p.name, 50);
  cout<<"Enter age: ";
  cin>>p.age;
  cout<<"Enter salary: ";
  cin>>p.salary;
  return p;
}
void displayData(Person p)
{
  cout<<"\nDisplaying Information."<<endl;
  cout<<"Name: "<<p.name<<endl;
  cout<<"Age: "<<p.age<<endl;
  cout<<"Salary: "<<p.salary;
}
To download raw file Click Here

Output

Enter Full name: Ram
Enter age: 23
Enter salary: 20000
Displaying Information.
Name: Ram
Age: 23
Salary: 20000


Program List


Flow Control

IF Statement Examples


Switch Case


Goto Statement


Break and Continue


While Loop


Do While Loop


For Loop


Friend Function in C++


String Examples


Array Examples


Structure Examples


Structure & Pointer Examples


Structure & Functions Examples


Enumeration Examples


Template Examples


Functions


Inheritance Examples

Hierarchical Inheritance


Hybrid Inheritance


Multilevel Inheritance


Multiple Inheritance


Single Level Inheritance


Class and Objects

Constructor Example


Destructor Example


Operator Overloading Example


Operator and Function Example


List of Programs


Pointer Examples


Memory Management Examples


Pointers and Arrays


Virtual Function Examples