-1

When I am executing this, a garbage value is coming with account balance info. Can anyone help me why?

#include<bits/stdc++.h>
using namespace std;
class Bankaccount
{
  public:
  int accnumber,accbalance;
  int display()
    {
    cout<<"Account number is: "<<accnumber;
    cout<<"\nAccount balance is: "<<accbalance;
    }
};
int main() {
Bankaccount a;
Bankaccount b;

a.accnumber = 123456;
a.accbalance =50;

b.accnumber = 67890;
b.accbalance = 2000;
cout<<"Account details of A\n\n"<<a.display()<<endl;
cout<<"\nAccount details of B\n\n"<<b.display();
return 0;
}
Guillaume Racicot
  • 39,621
  • 9
  • 77
  • 141
deba98
  • 23
  • 7

4 Answers4

1

The function display() should return void in this case. Your version has in its signature that it returns int, but then you don't return anything. This leads to undefined behavior.

Also it is bad practice to use using namespace std and #include<bits/stdc++.h>.

Read up here Why is "using namespace std;" considered bad practice?

And here How does #include <bits/stdc++.h> work in C++?

#include <iostream>

class Bankaccount
{
public:
    int accnumber, accbalance;

    void display()
    {
        std::cout << "Account number is: " << accnumber << "\n";
        std::cout << "Account balance is: " << accbalance << "\n";
    }
};

int main()
{
    Bankaccount a;
    Bankaccount b;

    a.accnumber = 123456;
    a.accbalance =50;

    b.accnumber = 67890;
    b.accbalance = 2000;
    std::cout<<"Account details of A\n\n";
    a.display();                             // this is how to use display

    std::cout<<"\nAccount details of B\n\n";
    b.display();
    return 0;
}
Guillaume Racicot
  • 39,621
  • 9
  • 77
  • 141
Rick Pat
  • 789
  • 5
  • 14
0

You are inserting to std::cout, among others, result returned from function display which should be int, but, considering that your function has no return statement everything is possible, basically you are sending undefined value to ostream cout, and that is what garbage is by definition.

Boki
  • 637
  • 3
  • 12
0

Your int display() function doesn't return an int so you'll have undefined behaviour after that function has been called. If it had returned an int, that number had been printed, but I suspect that's not what you wanted.

The garbage you see is an int picked from the stack (because display() was supposed to put an int there). It was put the for a reason by some other function, but now it's gone, so anything can happen. To avoid this, you could declare your function void display() - but then you wouldn't be able to stream it, which is what it looks like you want to do.

If you want to be able to stream your objects, you need to define streaming operators to do the job. I've replaced your display() function with an out stream operator (operator<<) here:

#include <iostream>
//#include<bits/stdc++.h> // non-portable, don't use it

// using namespace std; // brings in too much in the namespace
using std::cout; // prefer this or just write std::cout everywhere

class Bankaccount {
public:
    int accnumber, accbalance;

    friend std::ostream& operator<<(std::ostream& os, const Bankaccount& ba) {
        return os << "Account number is : " << ba.accnumber
                  << "\nAccount balance is: " << ba.accbalance << "\n";
    }
};

int main() {
    Bankaccount a;
    Bankaccount b;

    a.accnumber = 123456;
    a.accbalance = 50;

    b.accnumber = 67890;
    b.accbalance = 2000;
    // std::endl is approx. the same as "\n" + std::flush. You don't need flushing.
    cout << "Account details of A\n\n" << a << "\n";
    cout << "\nAccount details of B\n\n" << b << "\n";
}
Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
0

Most probably this is what you wanted to achieve:
https://wandbox.org/permlink/bpPth9WutHaiU5jQ

#include <bits/stdc++.h>
using namespace std;
class Bankaccount {
public:
    int accnumber, accbalance;
    std::ostream& display(std::ostream& out) const
    {
        out << "Account number is: " << accnumber;
        return out << "\nAccount balance is: " << accbalance;
    }
};

std::ostream& operator<<(std::ostream& out, const Bankaccount& acc)
{
    return acc.display(out);
}

int main()
{
    Bankaccount a;
    Bankaccount b;

    a.accnumber = 123456;
    a.accbalance = 50;

    b.accnumber = 67890;
    b.accbalance = 2000;
    cout << "Account details of A\n" << a << endl;
    cout << "\nAccount details of B\n" << b << endl;
    return 0;
}
Marek R
  • 32,568
  • 6
  • 55
  • 140