I'm trying to have it display the error message when the name is not found. But coding it as below will have many error messages displayed. i know it's because i loop it but how should i do it otherwise? please help.
example:
Error. Name not found.
Error. Name not found.
Error. Name not found.
Error. Name not found.
for( i = 0; i < 15; i++ )
{
compare = strcmp( s1.name, stud[i].name );
if( compare == 0)
{
printf( "The average grade of %s is %.2f\n", s1.name, stud[i].average );
}
int count(0);
for( i = 0; i < 15; i++ ) {
compare = strcmp( s1.name, stud[i].name );
if( compare == 0) {
printf( "The average grade of %s is %.2f\n", s1.name, stud[i].average );
++count;
}
}
if (count == 0)
printf("Error. Name not found.\n" );
I am not sure I am getting what you are asking... What I think you want to do is check a container of some type to see if there is a certain name in there. If there is that name in the container you want to print out the students average and his name, if not print a error message saying that name is not found.
Why don't you use the find() function in the algorithm header? It is made for stuff like this :).
Warning: This code uses C++11, so if you don't have a compiler that doesn't support that this code won't work.
// find example by Zereo
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
usingnamespace std;
int main ()
{
vector<string> names = {"Brandon", "Joe", "Jane", "Bob"};
// Declare a iterator to hold what is returned from find()
vector<string>::iterator it;
cout << "What name would you like to look for?" << endl;
string name;
cin >> name;
// It will hold the first instance of what we searched for
// if we found it, otherwise it will hold a iterator to the
// names.end();
it = find(names.begin(), names.end(), name);
// If it is equal to names.end() it means that the name was not found
if (it != names.end())
cout << "We found " << *it << endl;
else
cerr << "That name was not found!" << endl;
return 0;
}
Edited in some code for you to look at showing how find() works.
Basically find() works like this.
1st Parameter: A iterator to the first element you want it to search in.
2nd Parameter: A iterator to the last element you want it to search in.
3rd Parameter: The thing you want to search for, in the example above I got the user's input on which name they wanted to search for and stored it in a string that I then passed in as the 3rd parameter.
What It Returns: It will return a iterator to the first element that has what you are looking for, if it doesn't find anything in the container that matches what you are looking for it will return a iterator to whatever you entered in as the 2nd parameter which in the example above would be names.end();
The find() function also works on arrays. It is a very handy function as with a lot of things in the algorithm header. :)
Dear Zereo,
Unfortunately I just started learning C and I can't really understand the information you provided.
But I really appreciate it that you took such efforts to help me. Thank you so much ya! :D
I will definitely refer back to this in the future. :D