-1

I am getting arduino error here.

struct student{
int num,ac,sp,co, sum = 0;
    if (ac>=30 && sp>=30 && co>=30) 
    ^
        sum = ac+sp+co;
};

I am just learning my way around C++ right now, so it can be a stupid mistake.. Thanks in advance. exact error: line:12 col:6 [Error] expected unqualified-id before 'if'

hiteshn97
  • 97
  • 3
  • 10

1 Answers1

1

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).

Dorin Botan
  • 1,224
  • 8
  • 19