1)
Here you can read about what a struct in c++ is.
Briefly, it's a collection of other data types. All the logics (your if statement) should be placed inside functions.
2)
Looks like you wanted to do something like this:
int num = 0, ac = 0, sp = 0, co = 0, sum = 0;
Instead, you initialized sum
only. Other variables took default values.
I'm not sure I compeltely understand what you are trying to achieve, but try looking at this code (it's not Arduino, but you should get the idea):
struct Student {
int num, ac, sp, co;
int getSum() {
if (ac >= 30 && sp >= 30 && co >= 30)
return = ac + sp + co;
else return 0;
}
};
int main() {
Student student;
student.ac = 30;
student.cp = 40;
student.co = 50;
cout << student.getSum();
return 0;
}
Now, here is the problem.
Before calling student.getSum()
you always have to initialize ac
, sp
and co
(as I did in main()
), otherwise you can get garbage results. To avoid this, give default values to each of your variables (constructor is a good place for this).