Thread: what changes you would make?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Code:
    // student_data_read_practice.cpp
    // program which reads data of some students
    
    #include <iostream>
    #include <cstdlib>
    #include <string>
    #include <cstring>
    
    using namespace std;
    
    /* Move all this to a header file, using std:: as needed.*/
    /////////////////////////////////////////////////////
    struct Date {int d; string m; int y;}; //use longer variable names
    ////////////////////////////////////////////////////
    
    /////////////////////////////////////////////////////
    struct Student {int rollno; string sex; string name; float gpa; Date DOB;};
    //don't use all caps for variable names 
    //Make this into a class with print and read methods
    /////////////////////////////////////////////////////
    
    Student read();
    void prnt(Student dummystud);
    /*end of area to move*/
    
    int main()
    {
        Student stud[5];//Don't mix tabs and spaces for indination.
    
    	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()//I would make this a constructor for Student instead.
    {
        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 (const 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************************************";
    }
    
    //----------------------------------------------------
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  2. #2
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Thanks a lot, Elysia, King Mir, whiteflags.

    @Elysia: I haven't read anything about vectors so I don't know how to use them.

    What's the benefit of const'ing the data type Student?
    Code:
    void prnt (const Student & dummystud)
    Even without it the function won't accept anything else except Student data type structure. I understand the reason for passing arguments by reference.

    @whiteflags: What you say is absolutely correct and valid. But I'm a beginner so I don't know how to really restrict the inputs. Obviously, it will make the code complex and longer.

    @King Mir: How do you move that part of the code into a header? I didn't know there existed user created header files. Could you please tell me how to do this, if it's simple?

    Thank you for the help.

    Best wishes
    Jackson
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  3. #3
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Quote Originally Posted by jackson6612 View Post
    What's the benefit of const'ing the data type Student?
    Code:
    void prnt (const Student & dummystud)
    Even without it the function won't accept anything else except Student data type structure. I understand the reason for passing arguments by reference.
    If you try to modify "dummystud" inside "prnt()", your compiler will complain. The same will happen if you call a "Student" member function in "prnt()" which is non-constant.


    Quote Originally Posted by jackson6612 View Post
    @King Mir: How do you move that part of the code into a header? I didn't know there existed user created header files. Could you please tell me how to do this, if it's simple?
    *) You create a new file as *.h or *.hpp or *.hh ( Stick with *.h )
    *) You cut-paste your code there, making the necessary changes
    *) Include your new header file in your project like this:
    Code:
    #include "myHeader.h"
    Don't forget to wrap your header code in a preprocessor flag:
    Code:
    #ifndef __MY_HEADER_H__
    #define __MY_HEADER_H__
    
    // Your code goes here
    
    #endif // __MY_HEADER_H__
    Devoted my life to programming...

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by GReaper View Post
    Don't forget to wrap your header code in a preprocessor flag:
    Code:
    #ifndef __MY_HEADER_H__
    #define __MY_HEADER_H__
    
    // Your code goes here
    
    #endif // __MY_HEADER_H__
    That is not a "preprocessor flag". It is more usually described as an "include guard".

    Also, in C++, never use identifiers (or macro names) that contain double underscores, or that begin with a single underscore. The standard reserves such identifiers for use by the implementation. That means, if you use them in your code (or header files) your code might break if the implementation (for example, in standard header files) uses the identifier you have.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Quote Originally Posted by grumpy View Post
    That is not a "preprocessor flag". It is more usually described as an "include guard".
    You say po-tay-to, I say po-tah-to.

    Quote Originally Posted by grumpy View Post
    That means, if you use them in your code (or header files) your code might break if the implementation (for example, in standard header files) uses the identifier you have.
    Let's wish that never happens.
    Devoted my life to programming...

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by GReaper View Post
    You say po-tay-to, I say po-tah-to.
    Not at all. You gave wrong information, I corrected you. The term "preprocessor flag" has a completely different meaning from what you described.
    Quote Originally Posted by GReaper View Post
    Let's wish that never happens.
    If wishes were fishes, the ocean would be full.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 04-27-2011, 04:14 PM
  2. Establishing 'make clean' with GNU make
    By Jesdisciple in forum C Programming
    Replies: 9
    Last Post: 04-11-2009, 09:10 AM
  3. How would I make a c++ prgoram make a .exe file?
    By Rune Hunter in forum C++ Programming
    Replies: 9
    Last Post: 12-26-2004, 05:56 PM
  4. Make window in VB but make program in C/C++?
    By Boomba in forum Windows Programming
    Replies: 1
    Last Post: 06-23-2004, 12:29 AM
  5. Replies: 6
    Last Post: 04-20-2002, 06:35 PM