Exercise:
Write a C++ program that allows the user to store student information in a structure.
Click Here to View the Solution:
#include <iostream>
using namespace std;
struct stdinfo
{
char name[50];
float marks;
int rollno;
};
int main()
{
stdinfo i;
cout << "Student's information" << endl;
cout << "\nInsert name: ";
cin >> i.name;
cout << "Insert roll number: ";
cin >> i.rollno;
cout << "Insert marks: ";
cin >> i.marks;
cout << "\nDisplaying Information:" << endl;
cout << "Name: " << i.name << endl;
cout << "Roll no: " << i.rollno << endl;
cout << "Marks: " << i.marks << endl;
return 0;
}
Click Here to View the Output:
Student's information Insert name: john Insert roll number: 1234 Insert marks: 89 Displaying Information: Name: john Roll no: 1234 Marks: 89
Click Here to View the Explanation:
- 3 variables are initialized. The
char
type variablename [50]
. This string can contain up to 50 elements for the name. Afloat
type variablemarks
is used to store student’s score and anint
type variablerollno
for roll number. - A structure is
stdinfo
is created and a variable for the structurei
is created. - The student is asked to enter the name, roll number, and marks and they’re stored in i, structure variable.
- The student data is printed onto the screen.