Using a function to change the data in a struct

Before I begin, I would like to say I'm very much a beginner to C++. I have searched fruitlessly thus far for a solution.

I have been up all night trying to figure out how to use a function to change the data in my struct. Here is my struct:


struct patient{
int ID;
double Weight;
double Height;
bool IsMale;
} Trialist[9];


I want to create a function that tests Trialist.IsMale and if the trialist is male, reduce their weight by 10%, and if female, adjust their weight by 20%. The function can only have 1 parameter which is a pointer to a patient in the struct.

I apologise if this isn't very clear, but hopefully someone out there will know what I'm getting at!

Thank you for looking- RSVP C++ Geniuses!
you access members of a structure by the syntax StructName.ElemName.

so i am guessing you want to establish the variable in the main function (maybe through user input?) and then you want to call the function you are talking about. it would be something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void changeWeight(patient*);   //prototype of the function

int main(void)
{
	Trialist[0].Weight = 10;   //set value for weight
	Trialist[0].IsMale = true;   //lets suppose the person is male
	
	changeWeight(&Trialist[0]);    //you wanted pointer, so send the address
	
	return 0;
}

void changeWeight(patient *Person)   //stores the address in a pointer
{
	if((*Person).IsMale == true)   //be sure to dereference with (*Person)
	{
		(*Person).Weight *= 0.9;    
	}
}


hope that made some sense.
closed account (z05DSL3A)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <stdio.h>


struct patient
{
    int ID;
    double Weight;
    double Height;
    bool IsMale; 
} Trialist[9];

void changeWeight(patient* thePatient);   //prototype of the function

int main(void)
{
    Trialist[0].ID = 12345;
    Trialist[0].Weight = 100;  
    Trialist[0].Height = 1.82;
    Trialist[0].IsMale = true;   
	
    changeWeight( &Trialist[0] );   
	
    return 0;
}

void changeWeight(patient * thePatient)   
{
    // Use the -> member Access Operator as thePatient is a pointer
    // it is neater than (*thePatient).IsMale ...
    if( thePatient->IsMale )  
    {
        thePatient->Weight *= 0.9;
    }
    //else
    // ....
}
Last edited on
In C++, use a reference parameter
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
...

struct patient
  {
  int ID;
  ...
  };


void changeWeight( patient& p )
  {
  p.Weight *= 0.9;  // or whatever
  }

int main()
  {
  patient Triallist[ ... ];

  ...

  changeWeight( Triallist[ ... ] );

  ...
  }
Last edited on
closed account (z05DSL3A)
The OP dose state that
The function can only have 1 parameter which is a pointer to a patient in the struct
Thank you guys so much- I'll sift through now and see where I get. akimatsu123 , my data is read in through ifstream from a text file.
UPDATE: It works! Cheers to akimatsu, grey wolf and duoas!! thank you guys so much for the input- it got me there in the end with a working program!
Ack! Sorry!
Topic archived. No new replies allowed.