Thread: what changes you would make?

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    254

    what changes you would make?

    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.

    Code:
    // 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. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    >>Student stud[5];
    std::array<Student, 5> stud;
    Requires <array>.

    Or, std::vector<Student> stud(5);
    Requires <vector>.

    >>void prnt (Student dummystud)
    Pass by reference
    void prnt (const Student & dummystud)
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    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.

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    I would do a lot more input validation. What makes a date of birth OK? Because right now I could enter in calendar dates that don't even exist, like Feb 30. What should sex really contain ("Male" or "Female", or something else) and how are you going to make sure that's what it has? Aren't you going to restrict GPA? Issues like this require thoughtful answers.

  5. #5
    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.

  6. #6
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    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...

  7. #7
    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.

  8. #8
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Thank you, GReaper, grumpy.

    GReaper said:
    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.
    I'm sorry but I don't understand the red part. In my code I didn't call "Student read()" inside the "prnt()". Anyway, by this:
    Code:
    void prnt (const Student & dummystud)
    I have only made the variable "dummystud" of constant type which cannot be messed with. Please explain it if possible. Thank you.

    If you think the answer is too obvious and you think I'm just stupid, then please leave it as it is, and please don't make any hurtful comments. I'm already having very bad time these days. Thank you.
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    What's with all the abbreviations? Why "prnt" - is the one letter required to form the word "print" really so hard to type? I would actually go for longer and clearer names - "readStudent" and "printStudent" instead of "read" and "print", for example. What happens when you add another class, e.g. Lecture? Sure, you can overload print with one version taking a Student and one taking a Lecture, and it could even be convenient to simply use print() for everything. But you can't overload on the return type, so you can't have both "Student read()" and "Lecture read()". Then you have to rename them anyway - and if you have readStudent(), then you should have printStudent() simply for consistency.

    More stuff like this is all over the place. Why are Date's members called "d", "m" and "y"? Why not "day", "month" and "year"? It makes the code read a lot better. In Student, why "DOB", which is not only cryptic but also inconsistent in capitalization with everything else? "dateOfBirth" is a great name.
    Why call the master array of students "stud"? Why not "students"?

    This isn't the 70s. You're no longer limited to 8 or less characters in a name. Make use of that!
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  10. #10
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by jackson6612 View Post
    Thank you, GReaper, grumpy.

    GReaper said:

    I'm sorry but I don't understand the red part. In my code I didn't call "Student read()" inside the "prnt()".
    Code:
    class Student {
        int rollno;
        string sex;
        string name;
        float gpa;
        Date DOB;
    public:
        void prnt() const; //this is a const member function 
    };
    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.

  11. #11
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    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...

  12. #12
    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.

  13. #13
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Quote Originally Posted by King Mir View Post
    Code:
    class Student {
        int rollno;
        string sex;
        string name;
        float gpa;
        Date DOB;
    public:
        void prnt() const; //this is a const member function 
    };
    Thank you, King Mir.

    Please help me with some follow-on queries. Thanks.

    I see that you used the word "public", what does it do? How would it differ if it were "private" instead?

    You have used the word "const" after the function name although before this "const" used to precede anything it acts on. Okay. How would it made the function 'constant' this way?

    I see you didn't include the "read()" function in the class. Any reason?

    Thank you for the help and your time.

    Regards
    Jackson

    PS: Sorry, CornedBee, I forgot to thank you.
    Last edited by jackson6612; 09-25-2011 at 06:54 AM.
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by jackson6612 View Post
    I see that you used the word "public", what does it do? How would it differ if it were "private" instead?
    Familiarize yourself with classes and you shall understand.

    You have used the word "const" after the function name although before this "const" used to precede anything it acts on. Okay. How would it made the function 'constant' this way?
    Familiarize yourself with const correctness and you shall understand.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by jackson6612 View Post
    I see that you used the word "public", what does it do? How would it differ if it were "private" instead?
    The default is private. But a private method can't be called by the user; it is only used internally. So if you created such an object, you would not be able to call obj.prnt(), unless prnt() is public.

    You have used the word "const" after the function name although before this "const" used to precede anything it acts on. Okay. How would it made the function 'constant' this way?
    This just indicates prnt() cannot alter any member variables of the class. Ie, it makes all class members const within the context of prnt().

    I see you didn't include the "read()" function in the class. Any reason?
    Because the code was converted to OO.

    Quote Originally Posted by King Mir View Post
    Code:
    // Student read() definition
    
    Student read()//I would make this a constructor for Student instead.
    {
    Last edited by MK27; 09-25-2011 at 07:07 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

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