0

Here is the problem i Designed a program to collect blood from users after collecting the data i give them the output. one of the output is retrieved from outside function which is getTotalAmount but i am getting error: too few arguments to function 'float getTotalAmount'. code attached below :

class BloodDonator{
private:
          string bloodtype ,name;
          float amount ;
public:
        void set_details(){

                cout<<"Enter Name :";
                cin>>name;
        cout<<"Please select a blood type ...\n";
       cout<<" Enter [1] for A positive\n";
       cout<<" Enter [2] for A negative\n";
       cout<<" Enter [3] for B positive\n";
       cout<<" Enter [4] for B negative\n";
       cout<<" Enter [5] for O positive\n";
        cout<<"Enter [6] for O negative\n";
        cout<<"Enter [7] for AB positive\n";
      cout<<"Enter [8] for AB negative\n";
            cin>>bloodtype;
        cout<<"Enter Amount (ml) :";
        cin>>amount;
        };
        float getAmount(){
        return amount;
        };
        string getName(){
        return name;
        };
        string getBloodType(){
        return bloodtype;
        };
        getTotalAmount(float &amount);
        string n(){return name;}
};
float getTotalAmount(BloodDonator *ps)
{
static float totalamount=0;
totalamount = totalamount + ps->getAmount();
return totalamount;
};
int main()
{
BloodDonator p1 ;
//float getTotalAmount();
//getTotalAmount();

for(int i=0;i<3 ;i++){
            cout<<"         Input Details\n";
            cout<<"------------------------------\n";

        p1.set_details();
        //p1.getTotalAmount(*p1.getAmount());
            cout<<"------------------------------\n";
            cout<<"         output Details\n";
            cout<<"------------------------------\n";

        cout<<"name           :"<<p1.n()<<endl;
        cout<<"blood type     :"<<p1 .getBloodType()<<endl;
        cout<<"Blood Amount   :"<<p1.getAmount()<<endl;
        cout<<"Total Amount of Blood Donated so far :\n"<<getTotalAmount()<<endl;//here is total amount of blood
}
    return 0 ;
}
2
  • getTotalAmount takes one parameter: BloodDonator*`. You're calling it with o parameters. Aug 8, 2020 at 7:18
  • 1
    You declare a member function getTotalAmount(float &amount); taking a float reference. You later define a general function float getTotalAmount(BloodDonator *ps) taking a pointer to BloodDonator and then you attempt to use cout ... <<getTotalAmount() -- None of those match (or make much sense) Aug 8, 2020 at 7:19

3 Answers 3

1

You may have to change the following segments of your code:

// it's ok
cout<<"name           :"<<p1.n()<<endl;

// removed space between "p1." and "getBloodType()"
cout<<"blood type     :"<<p1.getBloodType()<<endl;

// it's ok
cout<<"Blood Amount   :"<<p1.getAmount()<<endl;

// passed pointer to p1, cause getTotalAmount() expects a pointer to BloodDonator object.
cout<<"Total Amount of Blood Donated so far :\n"<<getTotalAmount(&p1)<<endl;

Your code has several more problems like:

  1. What is the job of getTotalAmount(float &amount); inside BloodDonator class.
  2. if BloodDonator has already getTotalAmount(float &amount);, then, why there is a float getTotalAmount(BloodDonator *ps) doing outside...
  3. you're hardcoding blood donars detials, you should inject details through constructor... etc...
1

Look at the declaration in the class:

getTotalAmount(float &amount);

And the call in main() (last cout statement):

getTotalAmount();

You need to pass a required pointer variable p1 (since that's relevant and compatible) to the getTotalAmount().


Also the explicit type is missing in the declaration, it defaults to int, beware of that. To prevent it, declare it correctly:

float getTotalAmount(float &amount);

Another problem, which is the definition of function. You've just declared a function getTotalAmount (non class member function), you actually didn't wrote definition of the class member function getTotalAmount, i.e. you've defined the same named functions in two places.

It's up to you to code only for the one required by you and delete the another. In the current code provided by you, the class member function getTotalAmount has no meaning.

0
0

There are a few problems but the fundamental issue is bad program design. Any time you use a static variable you should think hard whether you really need it. Especially as a beginner you will almost never really need to use static variables.

In your code you have a static variable inside getTotalAmount. Instead this variable should be in main (and non-static). And the calculation to add the total blood donated should also be in main. Like this

int main()
{
    BloodDonator p1;
    float totalAmount = 0.0f;

    for (int i=0; i<3; i++) {
       ...
       cout << "Blood Amount   :" << p1.getAmount() << endl;
       totalAmount = totalAmount + p1.getAmount(); //here is total amount of blood
       cout << "Total Amount of Blood Donated so far :\n" << totalAmount << endl; 
    }

The function getTotalAmount can be deleted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.