Marco Faulhaber
Marco Faulhaber

Reputation: 23

Return Object of Class to Main Program

I am not so familiar with C++ and currently trying to code a class 'fraction'. I want to code a method 'Input' where a user gives values to a fraction. These values should be available throughout the program. This is what I have so far:

Call:

BRUCH t1;
   t1.Eingabe();

Method:

    BRUCH BRUCH::Eingabe()
{
    int _z, _n;
    BRUCH bruch;
    cout << "Bitte einen Zaehlerwert eingeben: " << endl;
    cin >> _z;
    cout << "Bitte einen Nennerwert eingeben: " << endl;
    cin >> _n;
    while(_n==0)
    {
        cout << "Bitte einen gültigen Wert eingeben!" << endl;
        cin >> _n;
    }
    bruch.z = _z;
    bruch.n = _n;
    return bruch;
}

When I return the object the values vanish and the constructor creates a new object with default values.

What must I do to get the correct return?

Bruch - Fraction (math.) | Eingabe - Input

Upvotes: 0

Views: 31

Answers (2)

Marco Faulhaber
Marco Faulhaber

Reputation: 23

I solved the problem.

I changed the return bruch; to return *this; and removed the 'bruch' obj. from the code. The assignment is now z = _z, n = _n;. This returns the current object I am working with.

Sorry for any inconvenience.

Upvotes: 1

Cory Kramer
Cory Kramer

Reputation: 117856

It doesn't look like you ever assigned the new value to anything

BRUCH t1;
BRUCH new_value = t1.Eingabe();

Upvotes: 1

Related Questions