Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am trying to use int number_of_groups return and pass it into djecja_reduta function this is header file and then printing it for first karneval number_of_groups() gives out normal values but void djecja_reduta::number_of_groups() gives very crazy results and random numbers

Example: I enter number_home_groups = 200 and number_foreign_groups = 300 on first call it gives me 500 sum but after when djecja_reduta::number_of_groups() is called it gives me 425232 looks like an address to me


C++
class karneval { 
private:
    char name_carneval[30];
    int number_home_groups;
    int number_foreign_groups;
    int sum;
public:karneval() {
        int number_home_group(0);
        int number_foreign_groups(0);
        int sum(0);
        strcpy(name_carneval,"");
    }
    void input();
    int number_of_groups();
};
void karneval :: input() {
    cout << "Input number home groups" << endl; 
    cin >> number_home_groups;
    cout << "input number_foreign_groups" << endl;
    cin >> number_foreign_groups;
}
int karneval:: number_of_groups() {
    sum = number_home_groupa + number_foreign_groups;
    cout << "Suma je:" << sum << endl;
    return sum;
}
class djecja_reduta:public karneval {
private:
    char mjesto_odrzavanja[30];
    int datum_odrzavanja;
public:
    void  number_of_groups();
};
void djecja_reduta:: number_of_groups(){
    karneval karneval;
    karneval.number_of_groups();
}

This is main file
C++
int n;
    cout << "Inesert number of carnevals" << endl; 
    cin >> n; 
    karneval* a = new karneval[n];
    djecja_reduta* b = new djecja_reduta[n];
    for (int i = 0; i < n; i++) { 
        cout << i + 1 << ".karneval" << endl;
        a[i].input();

    }
    for (int i = 0; i < n; i++) {
        a[i].number_of_groups();
        b[i].number_of_groups();
    }

Output:
Suma je:500
Suma je:4211069

What I have tried:

Tried using void and int to call function
Posted
Updated 24-Jan-20 15:46pm

C++
void djecja_reduta:: number_of_groups(){
    karneval karneval;
    karneval.number_of_groups();
}

This method creates a new karneval object, and then tries to output the total number of groups it contains. But this object does not contain any values for number_home_groups or number_foreign_groups. So the sum produced is just a random number.

Step through the code with the debugger and you will see it in action.
 
Share this answer
 
Comments
Member 14726049 24-Jan-20 18:14pm    
Made it this way now sum is out 0 how to pass these number_home_groups or number_foreign_groups values now into that djecja_reduta:: number_of_groups
class karneval {
private:
    char name_carneval[30];
    int number_home_groups;
    int number_foreign_groups;
    int sum;
public:
    karneval() : number_home_groups(0),
                 number_foreign_groups(0),
                 sum(0)
    {
        strcpy(name_carneval, "");
    }

    void input();
    void number_of_groups();
};
Richard MacCutchan 25-Jan-20 4:18am    
But that is not going to change the basic problem. You cannot get a valid result from a local variable that does not get any data. And the djecja_reduta class does not really serve any purpose.
However as a test you could change your code as follows:
void djecja_reduta::number_of_groups(){
    karneval::number_of_groups();  // call the base method
}

In the main method you need to collect input values for the b array:
for (int  i=0; i < n; i++)
{
    cout << i + 1 << ".karneval" << endl;
    a[i].input();
    cout << i + 1 << ".djecja_reduta" << endl;
    b[i].input();
}
Quote:
Passing values into another class function gives crazy values when printed

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900