Collect student details to structure and update selectively – Computer Sir Ki Class

Login


Lost your password?

Don't have an account ?
Register (It's FREE) ×
  

Login
[lwa]



Solved Problem #CPP#2781 siteicon   siteicon   siteicon  

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

TC++ #2781

 

Run Output

roll no:22
name:Vijay
marks:45

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

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.


Share

sunmitra| Created: 18-Jan-2018 | Updated: 19-Feb-2019|






×
Introductory Sessions Beginning to Program Tokens Keyword and Identifiers Data Types Variables and Constants Operators Simple User Input Building Expressions and Formulas Simple Real World Problems Simple If and If Else Multiple-Nested-Ladder of If Else Switch case selection Simple Loops Tricks in Loops - break continue scope Loop Applications - Handling numerals Series printing loops Nested Loops Pattern printing loops Number Varieties and Crunches String Handling (Null Terminated) Strings - string class type Functions (Built-in) Functions - user defined Functions Reference Passing/Returning Arrays Concepts and 1-D Arrays Array Data Management Two dimensional arrays and Matrices Structures Basics Structures passing/returning 2D Array Memory Addressing Display Using IO Manipulation Display Using C Formatting Tricks User Defined Data Types Enumerated Types Preprocessor Directives And Macros Exception Handling Programming Paradigms and OOPs Advantages Abstraction and Encapsulation Polymorphism Inheritance Function Overloading Concepts Function Overloading Varieties Function Overloading Special Cases Defining Classes Creating and Using Class Objects Class Members Accessibility Class Function Types Inline Functions Constant Functions Nesting of Functions Class Members Scope Resolution Static Members in a Class Array of Objects Constructor Concepts Default Constructor Parameterized Constructor Copy Constructor Constructor Overloading Destructors Inheritance Fundamentals Public Derivations Private and Protected Derivations Multiple Inheritance Multi-Level Inheritance Class Nesting Data File Concepts Handling Text Files Handling Binary Files Pointer Concepts Pointer and Arrays Pointers and Functions Object Pointers This Pointer Linked Lists Stacks Queues


Back