Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2015
    Posts
    8
    Rep Power
    0

    Please guide me where i am mistaking


    when i cal grade for input. the grade is not taking input.
    please guide me.

    following is the code i m trying to make work.
    /*You are required to show the concept of multiple inheritance in the following code.
    - Make 2 classes ‘Student’ and ‘Teacher’. You are required to add the attributes in Student which will include [gpa(), course() and pay()],
    and Teacher which include [grade(), teach(course), salary].
    You are required to make a 3rd class named, ‘Teaching_assistant’ which will inherit the attributes of both Teacher and Student.
    The main() program should be able to store and retrieve the required attributes from the constructors in the class Teaching_assistant.
    You are required to add the gpa of a student in function gpa(), the courses they are taking in course(), and the fee he/she pay in pay().
    Next you add the scale/grade of the teacher in grade(), the courses they teach in teach(), and salary in salary().
    The class Teaching_assistant class should include a constructor which simply displays the gpa of the student as well as the course the teacher is teaching.
    - This means you will be required to call Teaching_assistant and execute the constructor in it to retrieve some of the attributes as mentioned.
    */

    #include<iostream>
    #include<string>
    using namespace std;

    class Student
    {
    protected:
    float S_GPA;
    string S_Course[5];
    int S_Pay;

    public:
    void GPA()
    {
    cout << endl << "Enter The GPA: ";
    cin >> S_GPA;
    }

    void Course()
    {
    cout << "Enter The Courses Student Is Taking: " << endl << endl;
    int count = 1;
    for (int a = 0; a < 5; a++)
    {
    cout << "\t" << count << ": ";
    cin >> S_Course[a];
    count++;
    }
    }

    void Pay()
    {
    cout << endl << "Enter The Fee, The Student Is Paying: ";
    cin >> S_Pay;
    }

    };

    class Teacher
    {
    protected:

    string T_Course[3];
    int T_Salary;
    int T_Grade;

    public:


    void Teach()
    {
    cout << endl << "Enter The Courses The Student Is Teaching: " << endl << endl;
    int count = 1;
    for (int a = 0; a < 3; a++)
    {
    cout << "\t" << count << ": ";
    cin >> T_Course[a];
    count++;
    }
    }

    void Salary()
    {
    cout << endl << "Enter The Salary: ";
    cin >> T_Salary;
    }

    void Grade()
    {
    cout << endl << "Enter The Grade: ";
    cout << T_Grade;
    }
    };


    class Teaching_Assistant : public Student, public Teacher
    {
    public:
    Teaching_Assistant()
    {
    Student::Course();
    Student::GPA();
    Student::Pay();

    Teacher::Teach();
    Teacher::Salary();
    Teacher::Grade();




    cout << "The GPA Is: " << S_GPA << endl;
    cout << "The Course, Student Is Teaching: " << endl;

    int count = 1;
    for (int a = 0; a < 3; a++)
    {
    cout << "\t" << count << ": ";
    cout << T_Course[a] << endl;
    count++;
    }

    }
    };

    void main()
    {
    int a;
    cout << "For How Many Persons You Want To Enter Data: ";
    cin >> a;

    Teaching_Assistant **T;
    T = new Teaching_Assistant *[a];

    int count = 1;
    for (int i = 0; i < a; i++)
    {
    cout << endl << "Enter The Data For No. " << count << " Student" << endl << endl;
    T[i] = new Teaching_Assistant;
    count++;
    }
    system("pause");
    }
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,323
    Rep Power
    2226
    In order to preserve your code's formatting here, you need to use code tags. The problem is that HTML strips out extra white space, which includes leading tabs and spaces, so code listings lose their indentation. Code tags tells the forum software to preserve that indentation. Using code tags is very easy:

    [code] < insert your formatted code here > [/code]

    This is what your code looks like with code tags (original formatting retrieved via Reply With Quote button):
    Code:
    #include<iostream>
    #include<string>
    using namespace std;
    
    class Student
    {
    protected:
    	float S_GPA;
    	string S_Course[5];
    	int S_Pay;
    
    public:
    	void GPA()
    	{
    		cout << endl << "Enter The GPA: ";
    		cin >> S_GPA;
    	}
    
    	void Course()
    	{
    		cout << "Enter The Courses Student Is Taking: " << endl << endl;
    		int count = 1;
    		for (int a = 0; a < 5; a++)
    		{
    			cout << "\t" << count << ":  ";
    			cin >> S_Course[a];
    			count++;
    		}
    	}
    
    	void Pay()
    	{
    		cout << endl << "Enter The Fee, The Student Is Paying: ";
    		cin >> S_Pay;
    	}
    
    };
    
    class Teacher
    {
    protected:
    	
    	string T_Course[3];
    	int T_Salary;
    	int T_Grade;
    
    public:
    	
    
    	void Teach()
    	{
    		cout << endl << "Enter The Courses The Student Is Teaching: " << endl << endl;
    		int count = 1;
    		for (int a = 0; a < 3; a++)
    		{
    			cout << "\t" << count << ":  ";
    			cin >> T_Course[a];
    			count++;
    		}
    	}
    
    	void Salary()
    	{
    		cout << endl << "Enter The Salary: ";
    		cin >> T_Salary;
    	}
    
    	void Grade()
    	{
    		cout << endl << "Enter The Grade: ";
    		cout << T_Grade;
    	}
    };
    
    
    class Teaching_Assistant : public Student, public Teacher
    {
    public:
    	Teaching_Assistant()
    	{
    		Student::Course();
    		Student::GPA();
    		Student::Pay();
    
    		Teacher::Teach();
    		Teacher::Salary();
    		Teacher::Grade();
    		
    		
    		
    
    		cout << "The GPA Is: " << S_GPA << endl;
    		cout << "The Course, Student Is Teaching: " << endl;
    
    		int count = 1;
    		for (int a = 0; a < 3; a++)
    		{
    			cout << "\t" << count << ":  ";
    			cout << T_Course[a] << endl;
    			count++;
    		}
    
    	}
    };
    
    void main()
    {
    	int a;
    	cout << "For How Many Persons You Want To Enter Data: ";
    	cin >> a;
    
    	Teaching_Assistant **T;
    	T = new Teaching_Assistant *[a];
    
    	int count = 1;
    	for (int i = 0; i < a; i++)
    	{
    		cout << endl << "Enter The Data For No. " << count << " Student" << endl << endl;
    		T[i] = new Teaching_Assistant;
    		count++;
    	}
    	system("pause");
    }
    The first thing I do is to compile your code with warnings turned on. It would surprise you how many students have no idea how important warnings are and that they must never ignore warnings. Here is what I get using MinGW gcc on Windows 7:
    C:\otros\dcw>g++ -Wall titanium1.cpp
    titanium1.cpp:107: return type for `main' changed to `int'

    C:\otros\dcw>
    main is supposed to be declared to return int and should return zero upon successful completion of the program or a positive non-zero number to indicate failure. Those exit codes can be important for the program's parent process and especially in shell scripts.

    I cannot think of any current C++ compiler that does not complain when you declare main to be void. At first I thought that you were being forced to use that old Turbo C++, but that's not the case since you're using features of the language that were added in 1999. Out of curiosity, what compiler are you using?

    Originally Posted by T Titanium
    when i cal grade for input. the grade is not taking input.
    OK, I'm confused. Here is the code for the Grade method in the Teacher class:
    Code:
    	void Grade()
    	{
    		cout << endl << "Enter The Grade: ";
    		cout << T_Grade;
    	}
    There is no input code in that method.

    Perhaps if you were to also provide us with a small set of input data that you are using to test your program, along with what you expect the output to be.

    Comments on this post

    • markroberts agrees
    • T Titanium agrees
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2015
    Posts
    8
    Rep Power
    0
    I will keep that in mind next time dwise1_aol. I didnot know the proper syntax for displaying code.

    I am using Microsoft Visual Studio 2013 Ultimate. and there are no warnings at all. its my second semester and i have been using it from the first day. main() can be both with void r int i guess. if i use int i just have to use return 0; in end and if i use void system("pause") is enough.

    for this code i have no warnings and no errors according to this compiler.

    and for void grade(), i pasted wrong it was cin >> T_Grade;

    I just dont know why but i was told to close compiler and then rebuild again then run.
    And now its working perfectly.
  6. #4
  7. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,449
    Rep Power
    1874
    > main() can be both with void r int i guess.
    There is a big difference between what the C++ standard requires every compiler should support (int main), and what your current compiler will let you get away with (void main).

    If you learn a bunch of bad habits based on what your current compiler will allow (even after being told by those who are better informed that it's wrong), then you're in for a world of pain if you have to move to a different compiler.

    Comments on this post

    • markroberts agrees
    • T Titanium agrees
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2015
    Posts
    8
    Rep Power
    0
    Got it Salem, I have been using void because my compiler let me do it without any warnings.
    I will use int from now. Dont want to learn bad habits.
  10. #6
  11. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,785
    Rep Power
    4300
    void main() is legal in the C standard (because the wording of the standard is somewhat vague and allows it), but not in the C++ standard (where the wording is explicit in what the return type of main() should be). With that said, you'd probably better stick to int main, because your code may not work in the next version of the Visual C++ compiler, if they decide to stick to the C++ standard.

    Comments on this post

    • T Titanium agrees
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2015
    Posts
    8
    Rep Power
    0
    I have alot to learn :)
    Thank Scorpions4ever, I will try to stick with C++ standards from now.
    other than void main() is there any thing else in this code, about which you could say 'If it has been this way, it would have been better'
  14. #8
  15. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,785
    Rep Power
    4300
    Yes, one more thing. I would take out that using namespace std; line from the top and explicitly qualify the classes and types. E.g.
    Code:
    #include<iostream>
    #include<string>
    // using namespace std; <--- COMMENTED THIS OUT 
    
    class Student
    {
    protected:
    	float S_GPA;
    	std::string S_Course[5]; // <-- NOTE: See how std::string is explicitly qualified
    ...
    ...
    
    	void GPA()
    	{
    		std::cout << std::endl << "Enter The GPA: "; // NOTE: See how cout, endl and cin are explicitly qualified to std:: namespace
    		std::cin >> S_GPA;
    	}
    and so on. A lot of textbooks say to put that using namespace std; line at the top of the code, so that you avoid extra typing to qualify the classes explicitly. However, this destroys the entire purpose of namespaces. That's why you should avoid putting "using namespace std;" at the top of your file. If you don't want to keep typing std:: in front of every object, you could also do this instead:
    Code:
    #include<iostream>
    #include<string>
    // using namespace std; <--- COMMENTED THIS OUT 
    
    class Student
    {
    protected:
    	float S_GPA;
    	std::string S_Course[5]; // <-- NOTE: See how std::string is explicitly qualified
    ...
    ...
    	void GPA()
    	{
                    using std::cout;
                    using std::endl;
                    using std::cin;
    		cout << endl << "Enter The GPA: "; 
                    cout << endl << "Enter The GPA if you please: "; 
    		cin >> S_GPA;
    	}
    As you can see, if you are using the object multiple times within a function (e.g. cout and endl), you could explicitly import specific selected objects that your code uses, instead of importing the entire namespace into your code. Then you can use the objects without typing std:: in front of each one.
    See https://isocpp.org/wiki/faq/coding-s...-namespace-std for more details as to why you should avoid "using namespace std;".

    Also, once you get further down in your studies, you should learn how to separate your classes into separate source and header files. That's how larger projects are written. Right now, if you change one character in your source file, the compiler has to recompile the entire source for all your classes, because it doesn't know which one changed. If you separate the classes into separate files, then if you change one file, the compiler knows to only recompile that file and then links the previously compiled files for the other classes to your final executable. This vastly improves your compile time.

    Comments on this post

    • markroberts agrees
    Last edited by Scorpions4ever; May 11th, 2015 at 02:10 PM.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2015
    Posts
    8
    Rep Power
    0
    Thank you Scorpions4ever for another point to learn. I will do study and practice on std. And will adopt it :)
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2015
    Location
    Tryon, NC
    Posts
    10
    Rep Power
    0
    Just a question...What if any are the negative aspects to using
    Code:
    using namespace std;
    in your code? why is it frowned upon by many members of the community?
  20. #11
  21. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,785
    Rep Power
    4300
    Originally Posted by alteclansing110
    Just a question...What if any are the negative aspects to using
    Code:
    using namespace std;
    in your code? why is it frowned upon by many members of the community?
    If you look at the link I posted a couple of posts above, you'll see the answer there.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  22. #12
  23. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,785
    Rep Power
    4300
    One more very important skill to learn is to become comfortable with using a debugger. Visual Studio Ultimate Edition does come with a debugger (as do other C++ compilers as well), so learn how to use it and you'll save yourself countless hours of debugging.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  24. #13
  25. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2015
    Posts
    8
    Rep Power
    0
    Sorry Scorpions4ever, I have not gotten your point. Please elaborate a bit.
  26. #14
  27. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,323
    Rep Power
    2226
    I assume you are asking why using a debugger is so important. Here is a real-life instance.

    I mainly do embedded programming without an In-Circuit Emulator (ICE). The code for our products is contained in more than a hundred source files. Debugging that code involves a lot of staring at code and inserting debug messages (AKA "trace statements") that output certain variable values through the serial port at various places in the code. Very time consuming.

    We have some Windows-GUI test utilities that we developed with Visual Studio 2008. These projects have fewer source code files, like several tens of files. For those, I start the program running in the debugger and set the breakpoints to places in the code where we're apparently generating the wrong output. Then I can step through the code line by line and observe the values of the variables. That way, I can very quickly see what's happening and what's going wrong, which enables me to start writing the solution after having spent a very short amount of time discovering the problem. This approach is especially helpful when the bug is that the program crashes. In that case, I start running the program in the debugger and when it crashes I then look at the Call Stack to see which of our functions was involved, I go back to the offending line in our code, and I observe variable values at that point in the program at which time the offending variable value that caused the crash is usually very obvious. Without the debugger, trying to figure out what caused the program to crash would be very difficult to determine.

    You don't really learn how to write programs in school, but rather while working in the real world -- in school you mainly just learn the fundamental concepts and the languages, but not how to actually develop and maintain software. In school, most of your programs are very short, almost never more than one source file, and you can keep the entire program inside your mind where you can think about the entire program at one time, so you cannot see any reason for spending the time to organize the project in any systematic manner. And you are the only one who works on that program, so you cannot see any reason for proper formatting and commenting. In the real world, your programs are very large and consist of a very large number of source files, even hundreds of files. You cannot hold the entire program inside your mind, but rather all you can think about is the general way in which the different sections of the program work with each other and about the details of one or two of those sections. Therefore, you need to organize the project in a systematic manner, clearly defining what each section of the code does and how it communicates with the other sections of the code (for "section", you can think "object"). And you will be part of a team of programmers working on that project, so your ability to communicate to them what your code is doing becomes extremely important (ie, you will need to follow standards for formatting and commenting your code so that anybody can pick it up and understand quickly what it's doing).

    You don't learn any of that in school. Your first day on your first programming job is when your education really begins. Learning to use a debugger is part of that education.

    Comments on this post

    • markroberts agrees : Thanks for sharing your experience!
    • alteclansing110 agrees
    • T Titanium agrees
    • hexman agrees
    Last edited by dwise1_aol; May 15th, 2015 at 10:07 AM.
  28. #15
  29. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2015
    Posts
    8
    Rep Power
    0
    Thanks alot for such a detailed answer.
    That's right we are just beginners for now. To taste how multiple header files are used. We had a project in the end of semester. But that was just a small code of 457 lines. But there is still a long way to go :)

    Ok so i have understood that debugger helps you alot to identify your mistakes.
    But i didnot get what scorpions4ever meant by "become comfortable with using a debugger"
    means i should always run code with debugging it. and should avoid selecting the option of 'without debugging' ?
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo