My program needs two functions. One to get user input for a student name and search for it in a string array. Another one to display the scores for that student. The data is coming from a text file and I was able to store the names and scores in arrays,
The problem is that how would I make a search function make sure it finds a valid name and display a specific row of scores for that particular student? Any help is appreciated!
My source code is below,
#include <iostream>
#include <string>
constint NUM_STUDENT = 10;
constint NUM_SCORE = 5;
// search for a student name in the names array.
// return position in the array if found, -1 if not found
int search( const std::string& name, const std::string student_names[NUM_STUDENT] )
{
for( int i = 0 ; i < NUM_STUDENT ; ++i ) if( name == student_names[i] ) return i ;
return -1 ; // not found
}
// display the scores for that student
void display_scores( const std::string& name, const std::string student_names[NUM_STUDENT],
constint scores[NUM_STUDENT][NUM_SCORE] )
{
constint pos = search( name, student_names ) ;
if( pos == -1 ) std::cout << "student name '" << name << "' not found\n" ;
else
{
std::cout << "student name '" << name << "' scores: " ;
for( int s : scores[pos] ) std::cout << s << ' ' ;
std::cout << '\n' ;
}
}