-1

To calculate total in void display, I need the values of part1marks, part2marks and score. Hence I have created return functions (This is how I have to do it as per my assignment). However, I am getting undesirable output as shown below.

#include<iostream>
using namespace std;
class student
{
    int rollno;
public:
    void getnumber()
    {
        cout << "Enter roll number: ";
        cin >> rollno;
        cout << endl;
    }
    int putnumber()
    {
        return rollno;
    }
};
class test : virtual public student
{
    float part1marks;
    float part2marks;
public:
    void getmarks()
    {
        cout << "Enter Marks(Parts 1 and 2): ";
        cin >> part1marks >> part2marks;
        cout << endl;
    }
    float putmarks()
    {
        float marks = part1marks + part2marks;
        return marks;
    }
};
class sports : virtual public student
{
    float score;
public:
    void getscore()
    {
        cout << "Enter score: ";
        cin >> score;
        cout << endl;
    }
    float putscore()
    {
        return score;
    }
 };
class result : public test, public sports
{
    float total;
public:
    void display()
    {
        test t;
        sports s;
        float sc = s.putscore();
        float ms = t.putmarks();
        total = sc + ms;
        cout <<"Total marks= "<< total;
    }

};
int main()
{
    result obj;
    obj.getnumber();
    obj.getmarks();
    obj.getscore();
    obj.display();
    system("pause");
    return 0;
}

Expected Output

Enter roll number: 21

Enter Marks(Parts 1 and 2): 22.2 22.2

Enter score: 22.2

Total marks= 66.6 Press any key to continue . . .

Actual Output

Enter roll number: 21

Enter Marks(Parts 1 and 2): 22.2 22.2

Enter score: 22.2

Total marks= -3.22123e+08Press any key to continue . . .

iz_
  • 15,923
  • 3
  • 25
  • 40
Soham Ray
  • 21
  • 1
  • 3
    It sounds like you may need to learn how to use a debugger to step through your code. With a good debugger, you can execute your program line by line and see where it is deviating from what you expect. This is an essential tool if you are going to do any programming. Further reading: [How to debug small programs](http://ericlippert.com/2014/03/05/how-to-debug-small-programs/) and [Debugging Guide](http://idownvotedbecau.se/nodebugging/) – NathanOliver Jul 31 '19 at 18:45
  • @NathanOliver can I do that somehow in Visual Studio? – Soham Ray Jul 31 '19 at 18:46
  • Sure. Set a breakpoint on the first line in `main` and then press either the green arrow "Local Windows Debugger" or press F5 – NathanOliver Jul 31 '19 at 18:48
  • Visual studio has one of the best debuggers out there. [Start reading somewhere around here.](https://learn.microsoft.com/en-us/visualstudio/debugger/debugger-feature-tour?view=vs-2019) – user4581301 Jul 31 '19 at 18:48
  • [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/5910058) - *Please* read [the accepted answer](https://stackoverflow.com/a/25385174/5910058). – Jesper Juhl Jul 31 '19 at 18:49
  • Look at your code line by line with your debugger and pay close attention to the data types you are using. – Grayson Jul 31 '19 at 18:52
  • 1
    `int putnumber() { return rollno; }` - How quaint. A getter named `put`foo.. which even returns a variable not initialized in-class, nor by the constructor, so very easily an uninitialized variable (the reading of which is Undefined Behaviour).. – Jesper Juhl Jul 31 '19 at 18:53
  • `class test : virtual public student` - why `virtual` inheritance? *Why*? Virtual inheritance should be used *extremely* sparingly, and only when there is a *very* good reason. Definitely "expert only" territory... – Jesper Juhl Jul 31 '19 at 18:57
  • `system("pause");` - *Not* portable. – Jesper Juhl Jul 31 '19 at 18:58
  • `using namespace std;` - *Usually* a bad idea. – Jesper Juhl Jul 31 '19 at 18:59
  • @Yksisarvinen Whoops. Right you are. So there is. But still, why is that? For something like this it looks over complicated for no real reason. – Jesper Juhl Jul 31 '19 at 19:00
  • `void getscore()` - I would expect a function named `getscore` to `return` me a score that I could then print or do calculations on or whatever. That that function returns `void` and has the side effect of *printing* the score is surprising (and looks like bad design to me). – Jesper Juhl Jul 31 '19 at 19:03
  • @JesperJuhl most of these are limitations put on by my teacher. I have to use virtual class. I have to use namespace std. I got the problem by debugging, in putscore and putmarks, the values for part1marks, part2marks and score are not the user inputted values, but rather default weird values. So how can I make it so that in putscore and putmarks, input values( the ones taken in getscore and getmarks) are returned? – Soham Ray Jul 31 '19 at 19:08
  • Sounds like a poor teacher *to me*, if that's where this design came from. – Jesper Juhl Jul 31 '19 at 19:11
  • @JesperJuhl I think it's pointless to do code review for school assignments. People are not gonna learn proper coding style unless they start working on real projects and see for themselves how bad is bad code. We repeat under every single question that `using namespace std` is bad, but it's school that teaches it, so it's school that gives the rules. I guess for school assignment it's more important to show some concept, rather than anything else - if you know the concept, you can reapply it with good code or bad code, doesn't matter. – Yksisarvinen Jul 31 '19 at 19:12
  • @Yksisarvinen It's late and I'm bored, so I'm just commenting on random questions (hopefully still sensibly). If this was an *actual* code review at work, I'd have a lot more to say about this code.. – Jesper Juhl Jul 31 '19 at 19:17
  • Have you tried to debug your code yet? Check `display()` and the values of `t` and `s` when you use them. – Yksisarvinen Jul 31 '19 at 19:19
  • "maybe help me" - I think the comments that have been made about your code and the links to tools you should investigate, *very much counts as* help.. Don't expect anyone to debug the code for you and just post a solution. You are the one who has to do that work. – Jesper Juhl Jul 31 '19 at 19:21
  • @Yksisarvinen I did, and I saw the values not being the user inputted values. I still cant figure out how to fix it. – Soham Ray Jul 31 '19 at 19:21
  • Can you find any place where the values inputted by user are present? How could you retrieve them? – Yksisarvinen Jul 31 '19 at 19:22
  • should cin>>score store a value in score which should be returned in putscore? I have no clue... – Soham Ray Jul 31 '19 at 19:23
  • Well, morotspaj already gave you the answer. – Yksisarvinen Jul 31 '19 at 19:26
  • 1
    @SohamRay *NathanOliver can I do that somehow in Visual Studio?* -- Seriously, there is a big "Debug" menu on the main task bar in Visual Studio. I am surprised you weren't curious as to what that menu item did. – PaulMcKenzie Jul 31 '19 at 19:46
  • Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackoverflow.com/rooms/197314/discussion-on-question-by-soham-ray-required-output-is-66-6-but-i-am-getting-gib). – Samuel Liew Aug 01 '19 at 01:37

1 Answers1

0

You are creating new objects with uninitialized values in display().

    void display()
    {
        test t;   // <- This is a new object with uninitialized members variables
        sports s; // <- This is a new object with uninitialized members variables
        float sc = s.putscore(); // Replace with this->putscore() or simply putscore()
        float ms = t.putmarks(); // Replace with this->putmarks() or simply putmarks()
        total = sc + ms;
        cout <<"Total marks= "<< total;
    }

Multiple inheritance is almost never a good idea. Is your assignment really forcing you to do that?

morotspaj
  • 1,400
  • 11
  • 26