Can someone help me with this problem
I've been trying to do an else statement but it doesn't work
here is the code....

for(int c=0;c<student_max;c++){

if(search_id==id[c]){
location=binary_search(id,student_max,search_id);
cout<<"Student ID \tTotal \tGrade"<<endl;
cout<<"-----------------------------"<<endl;
cout<<search_id<<" \t\t"<<total[location]<<" \t"<<determine_grade(total[location])<<endl;
//break;
}


//}

else{
cout<<"Incorrect id entered"<<endl;
break;
}
}
}

Dani AI

Generated

The code shown prints "Incorrect id entered" inside the loop, so the loop will stop on the first non-match. That explains the symptom: if search_id is not equal to id[0] the else runs immediately and the break exits the loop. and correctly noted the if/else structure looks valid syntactically, but the logic of emitting the "incorrect" message from inside the loop is the likely problem.

A simple, reliable flow is:

  • iterate and only mark a match (or store its index) and then break when found;
  • after the loop, check whether a match was found and either print the student record or print the "incorrect id" message.
    This avoids announcing failure on every non-matching element. As pointed out, either use a linear scan or a binary search — not both — and use break only to stop when the match is found.

If a binary-search-based approach is preferred, remember two important points:

  • std::binary_search returns a bool, not an index. To get the index you can use std::lower_bound and compute the distance from the begin iterator.
  • the range must be sorted before applying binary search.
    See std::binary_search and std::lower_bound for details.

Quick checks to avoid other pitfalls: confirm student_max is the actual number of valid entries, confirm id values and search_id have compatible types, and verify whether the binary_search routine you call is a custom function that returns an index (in which case its contract differs from std::binary_search). The minimal fix is to move the "incorrect id" handling out of the loop and only invoke it after the entire search completes (or when no match flag is set).

Recommended Answers

All 4 Replies

What I can see looks correct. What is the error you're getting?

Also, please use code tags, it makes it easier to read.

for(int c=0;c<student_max;c++)
    {
    if(search_id==id[c])
        {
        location=binary_search(id,student_max,search_id);
        cout<<"Student ID \tTotal \tGrade"<<endl;
        cout<<"-----------------------------"<<endl;
        cout<<search_id<<" \t\t"<<total[location]<<" \t"<<determine_grade(total[location])<<endl;
        }
    else
        {
        cout<<"Incorrect id entered"<<endl;
        }    
    }

As far as I'm able to see, the code is fine?

The blocking and logic structure of the snippet you are showing are correct.

Unfortunately, you aren't being very clear on what exactly isn't working.

Does it simply execute the first/true section every time? Is it ignoring the else section when the test is false?

Please provide more information. Based on what I see, it is likely that there is an issue outside your loop that is causing an error inside it.

Just guessing here, but rethink your break statements. Maybe you want to reorganize to find the matching ID, then break out of the loop and finish the location lookup.

bool found = false;
for ( int c = 0; c < student_max ; ++c )
{
    if ( search_id == id[c] )
    {
        found = true;
        break;
    }
}
if ( found )
{
    location = // etc
}
else
{
    cout << "invalid id;
}

Another possibility: what is binary_search() doing? Isn't it going to return the same index you already discovered in c? Do you even need the for loop? Could you just validate that search_id is in range and then use binary_search to find the location?

if ( search_id >= 0 && search_id < student_max )
{
    location = binary_search(id, student_max, search_id);
    cout << ....
}
else
{
    cout << "invalid";
}

Or is that supposed to be std::binary_search? If it is, you're using it wrong; check your documentation.

Like other people have noted, you'll need to be clearer on what you think is wrong, and possibly include some more of the program leading up to this loop.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.