2d arrays driving me crazy! help appreciated!

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
  #include <iostream>
#include <stdio.h>
#include <string>

using namespace std;

#define COLUMNS 15
#define ROWS 3

void populateDatabase (string populate [ROWS][COLUMNS])
{
  for (int i = 0; i < COLUMNS; i++)
    {
      cout << "Enter the name of the student: ";
      cin >> populate[0][i];
      cout << "Enter age of student \n";
      cin >> populate [1][i];
      cout << "Enter Prac mark for student\n";
      cin >> populate [2][i];
    }
}

void printDatabase (string populate [][COLUMNS])
{
  for (int k = 0; k < COLUMNS; k++)
    {
      for (int j = 0; j < ROWS; j++)
	{
	  cout << populate[j][k] << "\t";
	}
      cout << endl;
    }

}

void getScore (string populate [][COLUMNS], string name)
{
  for (int i = 0; i < COLUMNS; i++)
    {
      if (populate[i][0].compare(name) == 0)
	{
	  for (int j = 0; j < ROWS; j++)
	    cout << "\n" << populate[j][i] << "\n";
	}
      break;
    }
}


int main() 
{
  string database [ROWS][COLUMNS];
  string searchName  = "";

  populateDatabase (database);

  cout << "Please type the name you wish to search for: ";
  cin >> searchName;
  getScore (database, searchName);
  printDatabase (database);

  return 0;
}
Last edited on
It seems you got columns and rows mixed up a little in your code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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.

http://www.cplusplus.com/reference/string/string/operators/

Note, I have not actually tested your code in a compiler.
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.
Last edited on
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";
}
Ah so basically because I printDatabase( ) immediatley after getScore( ) it jumps the gun (so to speak) and prints the full table out?

I feel so stupid right now. I've tried compiling it and it still won't print the data corresponding to the name:

Please type the name you wish to search for: jess
Searching for: jess.
end of search.
[a1655620@ingb24w033] $

i think i've made a mistake somewhere in my loop inside of getScore( )
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);



Last edited on
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.

Thank you so much! Appreciate it greatly!
Topic archived. No new replies allowed.