Jump to content

what changes you would make?

- - - - -

  • Please log in to reply
5 replies to this topic

#1
jackson6612

jackson6612

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 304 posts
Hi :)

I have the compiled and run the code given below. I'm interested to know what changes you would like to make like changes in syntax, formatting of the code, etc? Please let me know.


// student_data_read_practice.cpp

// program which reads data of some students


#include <iostream>

#include <cstdlib>

#include <string>

#include <cstring>


using namespace std;


/////////////////////////////////////////////////////

struct Date {int d; string m; int y;};

////////////////////////////////////////////////////


/////////////////////////////////////////////////////

struct Student {int rollno; string sex; string name; float gpa; Date DOB;};

/////////////////////////////////////////////////////


Student read();

void prnt(Student dummystud);


int main()

{

    Student stud[5];


	for(int i=0; i<5; i++)

	{

	    cout << "enter student #" << (i+1) << "'s details below:-" << endl;

		stud[i] = read();

		prnt(stud[i]);

		cout << endl << endl;

	}


	system("pause");

	return 0;

}


//-----------------------------------------------

// Student read() definition


Student read()

{

    Student stud; string name;


    cout << "enter name: "; getline(cin, name);

	cout << "enter roll number: "; cin >> stud.rollno;

	cout << "enter sex: "; cin >> stud.sex;

	cout << "enter date of birth (e.g. 01 Jan 2000) below:-" << endl;

	cout << "enter day: "; cin >> stud.DOB.d;

	cout << "enter month: "; cin >> stud.DOB.m;

	cout << "enter year: "; cin >> stud.DOB.y;

	cout << "enter GPA: "; cin >> stud.gpa;

	cin.ignore();


	return stud;

}


//-------------------------------------------------

// void prnt() definition


void prnt (Student dummystud)

{

    cout << "\n\n\n************************************\n\n\n";

	cout << "roll no.: " << dummystud.rollno << endl;

	cout << "name: " << dummystud.name << endl;

	cout << "sex: " << dummystud.sex << endl;

	cout << "date of birth: " << dummystud.DOB.d << "-" << dummystud.DOB.m

         << "-" << dummystud.DOB.y << endl;


	if (dummystud.gpa >= 3.5)

        cout << "grade: A" << endl;


	else if (dummystud.gpa >= 3.0)

        cout << "grade: B" << endl;


	else if (dummystud.gpa >= 2.0)

        cout << "grade: C" << endl;


    else

        cout << "Pass" << endl;


    cout << "\n\n\n************************************";

}


//----------------------------------------------------


I'm an outright beginner, learning C++. Using Win XP Pro and Code::Blocks. Be nice to me, please.:)

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
Assuming you don't know about classes yet, the only real change I would make is to call "read" "readStudent", and "prnt" "printStudent" to make them more self-documenting.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
jackson6612

jackson6612

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 304 posts
Thank you, WP.

The member function "read()" I used is of type structure "Student", so don't you think writing "Student readStudent" would look little strange? Please let me know. Thanks.
I'm an outright beginner, learning C++. Using Win XP Pro and Code::Blocks. Be nice to me, please.:)

#4
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
read is not member function. Member functions or methods are class' functions. Also, Student is return type of read function so naming it readStudent​ wouldn't really be odd.
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#5
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
I'm thinking of the line:

stud[i] = read();

vs

stud[i] = readStudent();

Especially in a larger program, where you may read several different types of objects in, having the read function clearly identify what it reads in the name will help.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#6
jackson6612

jackson6612

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 304 posts
Thank you, Dutchman, WP.
I'm an outright beginner, learning C++. Using Win XP Pro and Code::Blocks. Be nice to me, please.:)




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users