So I've been piecing this together bit by bit very slowly. I'm only allowed to use
multi dimensional arrays so no vectors, structs (which would be the logical way), etc. Basically there are 15 students and their names, age and score get stored into a 2d array, you should be able to print it to the screen and then search for a name which will return the user age and score. I've created a populateDatabase function that stores the student data and i've created a printDatabase function that prints out all the student info. But i'm struggling with the getName function. It will only return the age and score of a student if it is the FIRST student in the array. Otherwise It will return only the name and no age or score. I'm stumped. Any help or direction would be much appreciated.
void populateDatabase (string populate [ROWS][COLUMNS])
{
for (int i = 0; i < COLUMNS; i++)
{
cout << "Enter the name of the student: ";
cin >> populate[0][i];
// so names will be stored in populate[0][xyz]
}
}
// ... later...
void getScore (string populate [][COLUMNS], string name)
{
for (int i = 0; i < COLUMNS; i++)
{
// if (populate[i][0].compare(name) == 0)
// can be rewritten more naturally as (see link below):
if (populate[i][0] == name) // this doesn't look much like populate[0][xyz]
// using populate[0][i] is the fix
{
for (int j = 0; j < ROWS; j++)
cout << "\n" << populate[j][i] << "\n";
}
break;
}
}
operator== is overloaded for std::string as a global function (it is outside std::string).
You may be forgiven for thinking it does not exist, if you only checked inside std::string.
Hmmm. That makes much more sense. I read through the relational operators and sorted out my mix up with rows and columns. I made the genius decision prior to go with [columns][rows]. silly really. Tried compiling.
1 2 3 4 5 6 7 8 9 10 11 12 13
void getScore (string populate [][COLUMNS], string name)
{
for (int i = 0; i < COLUMNS; i++)
{
if (populate[0][i]==name)
{
for (int j = 0; j < ROWS; j++)
cout << "\n" << populate[j][i] << "\n";
}
break;
}
}
it now ignores my getScore function entirely.
This is what my compiler is giving me:
Please type the name you wish to search for: josh //i've searched fro the name
joel 1 2
jessie 3 6
james 1 2
josh 1 3
benji 1 2
returns everything.
If I search for the first name input into the array 'joel' I get this:
Please type the name you wish to search for: joel
joel
1
2
joel 1 2
jessie 3 6
james 1 2
josh 1 3
benji 1 2
I don't think i fully understand what's happening here.
Do you forget that in main() you printDatabase() in the end?
So when you search for "josh", nothing actually gets printed for that.
When you search "joel", the data for "joel" is printed, then the entire database is printed right after. Remove the call to printDatabase() if you don't want this.
You can make the printing by getScore() prettier to avoid confusion:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
void getScore (string populate [][COLUMNS], string name)
{
cout << "Searching for: " << name << ".\n";
for (int i = 0; i < COLUMNS; i++)
{
if (populate[0][i]==name)
{
for (int j = 0; j < ROWS; j++)
cout << populate[j][i] << ' ';
}
break;
}
cout << "Search ended.\n";
}
i think i've made a mistake somewhere in my loop inside of getScore( )
You are right.
The break needs to be put inside the if() otherwise it will break the loop after just the first name is searched.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
void getScore (string populate [][COLUMNS], string name)
{
cout << "Searching for: " << name << ".\n";
for (int i = 0; i < COLUMNS; i++)
{
if (populate[0][i]==name)
{
for (int j = 0; j < ROWS; j++)
cout << populate[j][i] << ' ';
cout << '\n';
break;
}
}
cout << "Search ended.\n";
}
Edit: also, you may want to start using std::getline() to read std::string's. This way you can read names with spaces in them.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
void populateDatabase (string populate [ROWS][COLUMNS])
{
for (int i = 0; i < COLUMNS; i++)
{
cout << "Enter the name of the student: ";
getline(cin, populate[0][i]);
cout << "Enter age of student: ";
getline(cin, populate [1][i]);
cout << "Enter Prac mark for student: ";
getline(cin, populate [2][i]);
}
}
// ...
cout << "Please type the name you wish to search for: ";
getline(cin, searchName);
It's finally working! Thank you so much! I'll be more careful in future when it comes to making sure statements are part of the function they need to be and not outliers.
Also thanks for the 'get line' tip. I was wondering why I couldn't use spaces when I was inputting the names.