Like Tree4Likes
  • 2 Post By AndrewHunter
  • 2 Post By MK27

what's wrong with it? please help

This is a discussion on what's wrong with it? please help within the C++ Programming forums, part of the General Programming Boards category; Hi This code was working fine until I made some changes to it. Now it doesn't display the name. Below ...

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

    what's wrong with it? please help

    Hi

    This code was working fine until I made some changes to it. Now it doesn't display the name. Below is given output for the student #1, you can see it doesn't display the entered name. It leaves it blank. What's the reason in your view? Please help me with it.

    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);
    char grade(float dummyGpa);
    
    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;
    	cout << "grade: " << grade(dummystud.gpa) << endl;
    	cout << "\n\n\n************************************";
    }
    
    //-------------------------------------------------------------
    // grade() definition
    
    char grade(float dummyGpa)
    {
        if (dummyGpa >= 3.5)
            return 'A';
    
    	else if (dummyGpa >= 3.0)
            return 'B';
    
    	else if (dummyGpa >= 2.0)
            return 'C';
    
        else
            return 'D';
    
    }
    
    //----------------------------------------------------
    OUTPUT:
    Code:
    enter student #1's details below:-
    enter name: jackson heights
    enter roll number: 201
    enter sex: male
    enter date of birth (e.g. 01 Jan 2000) below:-
    enter day: 01
    enter month: 01
    enter year: 1900
    enter GPA: 5
    
    
    
    ************************************
    
    
    roll no.: 201
    name:
    sex: male
    date of birth: 1-01-1900
    grade: A
    
    
    
    ************************************
    
    enter student #2's details below:-
    enter name:
    Last edited by jackson6612; 09-24-2011 at 02:46 PM.
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    2,919
    You never assign a value to the students name in your read function, you assign the value to some other variable.

    Jim

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Thanks a lot, Jim.

    I'm going to learn classes and objects. Could anyone of you please translate the corrected code below into the one which uses concept of classes and objects? It would be kind of you. Thank you.

    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);
    char grade(float dummyGpa);
    
    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;
    
        cout << "enter name: "; getline(cin, stud.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;
    	cout << "grade: " << grade(dummystud.gpa) << endl;
    	cout << "\n\n\n************************************";
    }
    
    //-------------------------------------------------------------
    // grade() definition
    
    char grade(float dummyGpa)
    {
        if (dummyGpa >= 3.5)
            return 'A';
    
    	else if (dummyGpa >= 3.0)
            return 'B';
    
    	else if (dummyGpa >= 2.0)
            return 'C';
    
        else
            return 'D';
    
    }
    
    //----------------------------------------------------
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Hi

    Please help me with the code given below. I'm getting the errors alsp given at the bottom. The code also have two embedded questions. Please help me with them too. Thank you for your help.

    Best wishes
    Jackson

    Code:
    #include <iostream>
    #include <cstdlib>
    #include <string>
    #include <cstring>
    
    using namespace std;
    
    /////////////////////////////////////////////////////
    struct Date {int d; string m; int y;};
    ////////////////////////////////////////////////////
    
    /////////////////////////////////////////////////////
    class Student
    {
    	private:
    		int rollno;
    		string sex;
    		string name;
    		float gpa; //you cannot do, say, "cout << stud[1].gpa;"
    		Date DoB;
    
    	public:
    		void prnt();
    		void read();
    		char grade(float dummyGpa) const; /* what does "const" do here? */
    
        public:
            Student(){int rollno; string& sex, string& name; float gpa; Date& DoB};
    };
    /////////////////////////////////////////////////////
    
    const int L = 2;
    
    int main()
    {
        Student stud[L]; int i;
    
    	for(i=0; i<L; i++)
    	{
    	    cout << "enter student #" << (i+1) << "'s details below:-" << endl;
    		stud[i].read();
    		stud[i].prnt();
    		cout << endl << endl;
    	}
    
    	/* this cannot be executed because "gpa" is
        private, "cout << stud[1].gpa << endl;" */
    
        //cout << stud[1].gpa.read() << endl;// how do I access the GPA of stud[1]?
    
    	system("pause");
    	return 0;
    }
    
    //-----------------------------------------------
    // Student read() definition
    
    void Student::read()
    {
        Student stud;
    
        cout << "enter name: "; getline(cin, name);
    	cout << "enter roll number: "; cin >> rollno;
    	cout << "enter sex: "; cin >> sex;
    	cout << "enter date of birth (e.g. 01 Jan 2000) below:-" << endl;
    	cout << "enter day: "; cin >> DoB.d;
    	cout << "enter month: "; cin >> DoB.m;
    	cout << "enter year: "; cin >> DoB.y;
    	cout << "enter GPA: "; cin >> gpa;
    	cin.ignore();
    }
    
    //-------------------------------------------------
    // void prnt() definition
    
    void Student::prnt()
    {
        cout << "\n\n\n************************************\n\n\n";
    	cout << "roll no.: " << rollno << endl;
    	cout << "name: " << name << endl;
    	cout << "sex: " << sex << endl;
    	cout << "date of birth: " << DoB.d << "-" << DoB.m << "-" << DoB.y << endl;
    	cout << "grade: " << grade(gpa) << endl;
    	cout << "\n\n\n************************************";
    }
    
    //-------------------------------------------------------------
    // grade() definition
    
    char Student::grade(float dummyGpa) const
    {
        if (dummyGpa >= 3.5)
            return 'A';
    
    	else if (dummyGpa >= 3.0)
            return 'B';
    
    	else if (dummyGpa >= 2.5)
            return 'C';
    
        else if (dummyGpa >= 2.0)
            return 'D';
    
        else
            return 'E';
    }
    
    //----------------------------------------------------
    Errors:
    Code:
    |In constructor 'Student::Student()':|
    32|error: 'sex' declared as reference but not initialized|
    32|error: expected initializer before '&' token|
    32|error: expected initializer before '}' token|
    ||=== Build finished: 3 errors, 0 warnings ===|
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  5. #5
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Take a look at our Const correctness tutorial, that should clear up your confusion about const keyword.

    To access private data members, you need to create get and set functions. This is explained in our Class Tutorial.

    Code:
    public:
            Student(){int rollno; string& sex, string& name; float gpa; Date& DoB};
    Your errors are from this line. Do you know how references work? It would appear that you do not. Take a look at our References Tutorial. That should help you understand those errors.
    Last edited by AndrewHunter; 09-27-2011 at 12:35 PM.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  6. #6
    Here we go again...
    Join Date
    Sep 2011
    Location
    San Diego
    Posts
    102
    First, you are missing some semi-colons...

    What is the purpose of this:

    Code:
    public:         
    Student()
    {
       int rollno; 
       string& sex; 
       string& name; 
       float gpa; 
       Date& DoB;
    };

  7. #7
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Quote Originally Posted by rmatze View Post
    First, you are missing some semi-colons...

    What is the purpose of this:

    Code:
    public:         
    Student()
    {
       int rollno; 
       string& sex; 
       string& name; 
       float gpa; 
       Date& DoB;
    } EDIT: Removed ';' 
    Good eyes. Minor correction, no semicolon after the constructor definition.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  8. #8
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Thank you, AdrewHunter, rmatze.

    @AndrewHunter: I do not intend to say that your intention was not to help me and do not wish to offend you. But referring someone to tutorials on very query is not a good practice as far as these learning forums are concerned. Why not one goes to a library instead instead of requesting people here to help them? There are so many books everywhere and internet is full of knowledge. Almost every question has an answer somewhere on the internet. Then, what's the use of these forums? Because time is limited. One has to learn many things at quick rate. Some learn at faster pace and some slowly. If I had simply requested to provide me with a source code for a certain problem, then I do think I didn't deserve any help. But I did attempt to write a proper code with few errors. I love to learn from 'live' people on these forums where I can ask follow-on questions. Most of the things I know I have learned good people on the internet. I hope you get my point. Thank you for reading this.

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

  9. #9
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Quote Originally Posted by jackson6612 View Post
    Thank you, AdrewHunter, rmatze.

    @AndrewHunter: I do not intend to say that your intention was not to help me and do not wish to offend you. But referring someone to tutorials on very query is not a good practice as far as these learning forums are concerned. Why not one goes to a library instead instead of requesting people here to help them? There are so many books everywhere and internet is full of knowledge. Almost every question has an answer somewhere on the internet. Then, what's the use of these forums? Because time is limited. One has to learn many things at quick rate. Some learn at faster pace and some slowly. If I had simply requested to provide me with a source code for a certain problem, then I do think I didn't deserve any help. But I did attempt to write a proper code with few errors. I love to learn from 'live' people on these forums where I can ask follow-on questions. Most of the things I know I have learned good people on the internet. I hope you get my point. Thank you for reading this.

    Regards
    Jackson
    It was not a brush off. Your questions pointed to the fact of a clear understanding problem with some broad topics. The answer I provided pointed you to some good, however short, tutorials that would have answered your questions if you just clicked on them. The alternative would be just to repeat the information and what is exactly the point of that?
    MK27 and Elysia like this.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  10. #10
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by jackson6612 View Post
    referring someone to tutorials on very query is not a good practice as far as these learning forums are concerned.
    Yes it is. If someone recommends a source of information to you, it is because they have considered your problem and are being helpful. You should appreciate that.

    Almost every question has an answer somewhere on the internet.
    Perhaps, but that does not mean everyone knows how to find them. The presumption is that you have made some kind of effort to figure this out for yourself, and are now looking for assistance. That assistance can include pointing you in the right direction for your research.

    Then, what's the use of these forums? Because time is limited. One has to learn many things at quick rate.
    If you can find a solution via google or in a book, that will almost certainly be faster than if you have to wait for someone to reply on a forum. If you come to depend on forums, it is not because your time is limited. It is because you are lazy.

    It is a waste of time for someone to explain const correctness anew each time they perceive it as an issue in a thread, when const correctness is a very straightforward concept that has already been clearly explained somewhere. It is more appropriate to simply indicate the issue has to do with const correctness and point to an explanation.

    If you still do not understand, you can always ask a more specific question. Eg, "With regard to this tutorial (include a link), it says blah blah blah. Does that mean such and such a thing, or am I misunderstanding?". Etc.

    However, saying, "I do not have time to read that material. Please write something else for me to read instead", is ridiculous. If you do not have time to read the pre-existing material or cannot understand it, why would you believe some new version is going to work any better for you?

    Keep in mind that tutorials are often written by people who found themselves saying the same thing over and over again online. Because of that, they tend to be of a higher quality than a quick reply. I would not recommend a source unless I had evaluated it first.

    I love to learn from 'live' people on these forums where I can ask follow-on questions.
    That's great. But "live people" do not have an infinite amount of time and resources. In order to have time to help, we have to make judgment calls about what is worthwhile and what is not. Otherwise we'd be constantly busy with a backlog, and you'd get a ticket telling you someone will be available any day now.

    If that is not good enough, hire a tutor.

    In short: be specific. Be precise. Put in an effort to help yourself first, and appreciate the effort other people put in to help you. If you can do that well, your efficiency in using the forum will improve. OTOH, if you can't do that, you will quickly find yourself becoming unpopular and the forum will become a less efficient resource for you.
    Last edited by MK27; 09-27-2011 at 02:14 PM.
    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

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,033
    @jackson6612:
    It it clear that you do not fully understand what const correctness is due to the nature of your question. So shall we repeat what is already said on the net, then? Worse yet, shall we teach you what the meaning of it without explaining the rest, which without you cannot fully understand what it means and how to apply it? That would quite a long reply, and a big waste of our time if there is an answer somewhere, especially one that is written with care and tweaked over many days. Compare that to quick replies which are written in a hurry and with (often) not so great care.
    Does this make sense to why, as to why you were referred to that tutorial?
    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.

  12. #12
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,657
    Not to worry, jackson has apparently taken his ball and moved here.

  13. #13
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Hi

    As so many people think that there is a problem with my approach, then I think I would try to change it. Anyway, thanks for your advice.

    Regards
    Jackson
    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
    Posts
    23,033
    Jacksson, you should have a read in the crossposting discussion thread. Some people don't care, some people care a lot.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. wrong wrong with my xor function?
    By Anddos in forum C++ Programming
    Replies: 5
    Last Post: 04-26-2009, 02:38 PM
  2. Help! What's wrong here?
    By marco2250 in forum C++ Programming
    Replies: 3
    Last Post: 05-29-2006, 07:54 PM
  3. whats wrong with this? no errors but wrong result
    By InvariantLoop in forum C Programming
    Replies: 6
    Last Post: 01-28-2005, 12:48 AM
  4. Replies: 9
    Last Post: 07-15-2004, 04:30 PM
  5. what is wrong =(
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 07-01-2002, 06:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21