Detailed Print – Computer Sir Ki Class

Solved Problem#CPP#2781

## Problem Statement - Collect student details to structure and update selectively

Write a program to collect follow entry about an student in a structure type c++ data structure.
integer – rollno, char array – name, float – marks and char passfail. Declare a global instance of this structure and then collect the rollno,name and marks in the main routine. Within the main routine update the value of passfail as ‘Y’ or ‘N’ if the marks are more than or equal to 33.0 . Finally it should report as per following example.

R.No. Name Marks Status
———————————
22       Vijay  45         Pass

## Solution

```		#include <iostream>
#include <stdio.h>
using namespace std;
struct Student
{
int rollno;
char name[50];
float marks;
char passfail;
}s1;

int main()
{
cout<<"roll no:";
cin>>s1.rollno;
cin.ignore(100,'\n');
cout<<"name:";
cin.getline(s1.name,50);
cout<<"marks:";
cin>>s1.marks;
if(s1.marks>=33) s1.passfail='Y';
else s1.passfail='N';
cout<<"\nR.No.\tName\tMarks\tStatus"<<endl;
cout<<"---------------------------------"<<endl;
cout<<s1.rollno<<"\t"
<<s1.name<<"\t"<<s1.marks
<<(s1.passfail=='Y'?"\tPass":"\tFail")<<endl;
return 0;
}

```

## Run Output

``````roll no:22
name:Vijay
marks:45

R.No.   Name    Marks   Status
---------------------------------
22      Vijay   45      Pass
``````

## Solved Problem Understanding

struct Student //This is a structure definition with name as Student
{
int rollno;             //Will collect roll number here in integer variable
char name[50];   //Will collect the name of student here in character array
float marks;         //Will marks here as a float variable
char passfail;      //Will update the calculated pass/fail status here
}s1;                       //This one instance of student declared globally

cout<<“roll no:”; cin>>s1.rollno;
Here we collect the roll number into the structure instance s1.

cin.ignore(100,’\n’);
This we are giving to avoid  the cin.getline problem of collecting n from previous input. This is given if before cin.getline() function use we have some simple cin as input. This means that we are ignoring upto 100 characters before the press of newline for next entry.

cout<<“name:”; cin.getline(s1.name,50);
Here we are collecting name in s1 instance in char array.

cout<<“marks:”; cin>>s1.marks;
Here are collecting the marks in s1 instance of float type variable

if(s1.marks>=33) s1.passfail=’Y’;  else s1.passfail=’N’;
Here we are updating the s1.passfail character variable with Y or N.

cout<<“nR.No.\tName\tMarks\tStatus”<<endl;
cout<<“———————————“<<endl;
This is heading lines above the result.

cout<<s1.rollno<<“\t” <<s1.name<<“\t”<<s1.marks
<<(s1.passfail==’Y’?”\tPass”:”\tFail”)<<endl;
This is output printing. For flag ‘Y ‘ and ‘N’ we are using ternary operator based selection to print Pass or Fail within the cout chain. To get more details on ternary operators you can refer #2791.

## Notes

• Some teachers prefer to guide students with the use of gets() function in place of cin.getline() as it requires  cin.ignore(100,’n’); kind of command if previous input is collected using cin. We strongly oppose use of gets in c++ as it can easily allow buffer over run and corrupt memory area and can even make the system unstable.
• The perfect syntax of using cin.ignore is as follows.
cin.ignore(std::numeric_limits<std::streamsize>::max(), ‘\n’);
this looks too scary to we have devised a a simple syntax like
cin.ignore(100,’\n’);
This assumes that only up to 100 characters need to be ignored before pressing newline in the previous interaction of cin. The perfect syntax however allows up to maximum stream size.
• A safe way is to collect char array inputs before all other integer or fraction inputs are taken.