0

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();
    }
}
  • You only call `setFee` on the first element of the `stu` array. Then you print all the (uninitialized) `fee` values of the remaining four elements. – Some programmer dude Apr 08 '22 at 08:03
  • In your `stu[]` array you are setting `fee` only in the first element – Rob Apr 08 '22 at 08:05
  • ok do i have to make fee an array here? – Sagar Mavai Apr 08 '22 at 08:08
  • Why do you have the condition `if(i==0)`? Why do you call `setfee` only once and only on the `stu[0]` object? – Some programmer dude Apr 08 '22 at 08:10
  • because I want to set the same fee amount for every student and then autoprint it in a file for every stu[] array variable. – Sagar Mavai Apr 08 '22 at 08:21
  • @SagarMavai Just remove the `if(i==0)` condition which is unnecessary here. – Jason Apr 08 '22 at 08:25
  • That's not how member variables work. A (non-static) member variable is a member of the *object*, not the class. Perhaps you should reconsider the `setfee` function, to instead of asking the user for input, get the input as an argument? Then you can get the input for the fee once in the `main` function, and use one loop to call `setfee`. Then you can do a second loop where you call `showfee`. – Some programmer dude Apr 08 '22 at 08:27
  • And if this is really the very first C++ program you have written, then you have skipped quite a lot of the early chapters of your beginners book. I recommend you invest in [some good C++ books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282) and read them from the very beginning, not skipping anything. That should help you understand variables and objects better. – Some programmer dude Apr 08 '22 at 08:29

1 Answers1

0

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.
Jason
  • 36,170
  • 5
  • 26
  • 60