#can someone give me hints

24 messages · Page 1 of 1 (latest)

cobalt talon
#

i want to make a part of my program go through my vector and find the highest mark and then take that and display it to the user but to do that i would need to compare one integer to the others when i try it with an if statment it doesnt like it


int a = 1;
int b = 0;

for ( int i = 0; i < studentsVec.size(); i++) {

    if (studentsVec[i].overall > studentsVec[b].overall){
        a = i;
    }

    else {
        a = b;
    }
}
royal mossBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

cobalt talon
#

i just need a few pointers on where im going wrong

valid marlin
#

"when i try it with an if statment it doesnt like it" what do you mean by this? are you getting a compiler error?

cobalt talon
valid marlin
#

those errors are from a switch statement, not an if statement

#

if you were working with a switch statement before you can fix those errors with {} brackets to scope the code in the case labels it was complaining about

cobalt talon
#

ahhh thanks#

#

its not working properly

valid marlin
#

what did you try?

cobalt talon
#

well i fixed the switch issue

#

well the current code i have is ``` int a = 1;
int b = 0;

for (int i = 0; i < studentsVec.size(); i++) {

if (studentsVec[i].overall > studentsVec[b].overall) {
    a = i;
}

else {
    a = b;
}

} ```

and
in the switch statment

cout << studentsVec[a].overall << endl;
cout << studentsVec[b].overall << endl;
#

but it isnt grabbing the highest number in the vector

royal mossBOT
#

@cobalt talon Has your question been resolved? If so, type !solved :)

valid marlin
#

you should try adding some print statements in your code (output both values being compared, output when a's value is being changed, etc). a debugger would be even better but if you don't know how to use a debugger yet print statements should work out ok for snippets of code like this. see if anything obvious stands out once you have some info printing.

cobalt talon
#

i found the issue

#

it was comparing the same thing twice

#

because i made it be comparing to the b int not the a one

#
int a = 1;
int b = 0;

for (int i = 0; i < studentsVec.size(); i++) {

    if (studentsVec[i].overall > studentsVec[a].overall) {
        a = i;
        cout << "I Wins" << endl;
        cout << studentsVec[i].overall << " " << studentsVec[a].overall << endl;
        
    }
    
    else {
    
        cout << "B Wins" << endl;
        cout << studentsVec[i].overall << " " << studentsVec[b].overall << endl;

    }
}
valid marlin
#

nice job. print statements are always helpful for logic bugs like this.

#

you should spend some time figuring out how to use a debugger as well since that can make stuff like this solvable in a few minutes of inspecting variables/values of your program line by line while it executes

cobalt talon
#

thanks :)#

#

!solved