Home > Software design >  Printing value of private variables of a class in a loop using member functions
Printing value of private variables of a class in a loop using member functions

Time:04-08

I'm building my first program in C and I'm stuck where I'm trying to print the value of fee multiple times using for loop after giving value once. By running the loop it is giving garbage value every time after giving right value at first time. I'm new to the class topic in C . Please tell me how can I print the same value of private variable fee every time the loop runs.

#include<iostream>
using namespace std;
class FEE
{
    private:
    int fee;
    public:
    void setfee()
    {
        cout<<"Enter the monthly fee= ";
        cin>>fee;
    }
    void showfee()
    {
        cout<<"Monthly fee is "<<fee<<endl;
    }
};

int main()
{
    FEE stu[5];
    for(int i=0;i<5;i  )
    {
        if(i==0)
        stu[i].setfee();
        stu[i].showfee();
    }
}

CodePudding user response:

The problem is that you're calling the setfee member function only for the first object in the array stu. And since the array stu was default initialized meaning its elements were also default initialized, the data member fee of each of those elements inside stu has an indeterminate value. So, calling showfee on the elements on which setfee was not called, is undefined behavior since for those elements fee has indeterminate value and you're printing the value inside showfee.

because I want to set the same fee amount for every student and then autoprint it in a file for every stu[] array variable.

To solve this and do what you said in the above quoted statement, you can ask the user for the fee inside the main and then pass the input as an argument to the setfee member function. For this we have to change setfee member function in such a way that it has an int parameter. Next, using a 2nd for loop we could print the fee of each of the elements inside stu using showfee member function. This is shown below:

#include<iostream>

class FEE
{
    private:
    int fee = 0; //use in-class initializer for built in type
    public:
    void setfee(int pfee)
    {
        fee = pfee;
        
    }
    void showfee()
    {
        std::cout<<"Monthly fee is "<<fee<<std::endl;
    }
};

int main()
{
    FEE stu[5];   //default initiailized array 
    
    int inputFee = 0;
    for(int i=0;i<5;i  )
    {
        std::cout<<"Enter the monthly fee= ";
        std::cin>>inputFee;
        
        //call setfee passing the inputFee
        stu[i].setfee(inputFee);
        
    }
    for(int i = 0; i<5;   i)
    {
        stu[i].showfee();
    }
}

Demo

Also, note that using a std::vector instead of built in array is also an option here.

Some of the changes that i made include:

  1. Added a parameter to the setfee member function.
  2. Used in-class initializer for the fee data member.
  3. The input fee is taken inside main which is then passed to the setfee member function for each of the element inside stu.
  4. The member function showfee is called for each of the element inside stu using the 2nd for loop.
  • Related