Can't print user input for 2D Array

Hello, I'm having trouble with my assignment. We're supposed to create a string type 2D Array of 4 columns and 3 rows. The user has to enter 12 names or words then it's supposed to display it back to the user in a spreadsheet table style.
string01 string02 string03
string04 string05 string06
string07 string08 string09
string10 string11 string12

I'm having trouble displaying the table back to the user. All I get is
"The 12 names/words you entered were: "

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
  #include<iostream>
#include <string>

using namespace std;


void userInputValues();
void printStrings();

int main(int argc, char** argv)
{
  userInputValues();
  printStrings();

  return 0;
  
} 

void userInputValues()
{
  int index;
       
  string myNames[4][3];

  cout << endl << endl;
  cout << "Enter 12 names or words" << endl;
  for(int row = 0; row < 4; row++) 
  {
      for(int column = 0; column < 3; column++) 
		{
        cout << index ++ << ". Enter a name/word: ";
        cin >> myNames[row][column];
		}  
  }
  
}

void printStrings()
{
  
  string myNames[4][3];
  
  cout << "The 12 names/words you entered were:";

   for(int row = 0; row < 4; row++)
   {
    for(int column = 0; column < 3; column++)
     {
        cout<<" "<<myNames[row][column]<<" ";
     }
    }
    return;
}


FYI: We have two use two functions, one for userinput and the other to print the array back. Any help is appreciated.
Hello av16352,

The problem you have defined the array in both your functions and when the functions end so the the variables defined in each function.

You need to define the array in "main" and pass it to the functions.

It is also helpful to post the full instructions given to you , so everyone will know what you have to do and what you can and can not use. Otherwise you will get answers that are beyond what you have learned. Or suggestions that go against what you have learned.

In int main() if you are not going to use "argc" and "argv" you do not need to put them in. It is a waste of storage space if you do not need them.

Give me a few minutes and I will give you an example.

Andy
Hello av16352,

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
#include<iostream>
#include <string>

using namespace std;

constexpr int MAXROWS{ 4 }, MAXCOLS{ 3 };

using StrArray = std::string[MAXROWS][MAXCOLS];

void userInputValues(StrArray& words);
void printStrings(const StrArray& words);

int main()
{
    string myNames[MAXROWS][MAXCOLS];

    userInputValues(myNames);
    printStrings(myNames);

    return 0;  // <--- Not required, but makes a good break point.

}

void userInputValues(StrArray& myNames)
{
    int index{ 1 };  // <--- ALWAYS initialize your variables.

    cout << "\n\n";
    cout << "Enter 12 names or words\n";

    for (int row = 0; row < MAXROWS; row++)
    {
        for (int column = 0; column < MAXCOLS; column++)
        {
            cout << index++ << ". Enter a name/word: ";
            cin >> myNames[row][column];
        }
    }

}

void printStrings(const StrArray& myNames)
{
    cout << "\nThe 12 names/words you entered were:";

    for (int row = 0; row < MAXROWS; row++)
    {
        for (int column = 0; column < MAXCOLS; column++)
        {
            cout << " " << myNames[row][column] << " ";
        }
    }
    
    return;
}

I only ran it once, but it worked.

Prefer to use the new line (\n) over the "endl"s. These days the (\n) will empty the buffer without the function call to "endl".

Nice job on the for loops using "row" and "col".

Andy
I only ran it once, but it worked.


Just displays all the values on one line - no line breaks!

Return with no value at end of function is not needed.

Where possible, favour pre-inc rather than post-inc.

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
#include <iostream>
#include <string>

constexpr int MAXROWS {4}, MAXCOLS {3};

using StrArray = std::string[MAXROWS][MAXCOLS];

void userInputValues(StrArray& words);
void printStrings(const StrArray& words);

int main()
{
	StrArray myNames;

	userInputValues(myNames);
	printStrings(myNames);
}

void userInputValues(StrArray& myNames)
{
	size_t index {};

	std::cout << "\nEnter 12 names or words\n";

	for (size_t row = 0; row < MAXROWS; ++row)
		for (size_t column = 0; column < MAXCOLS; ++column) {
			std::cout << ++index << ". Enter a name/word: ";
			std::cin >> myNames[row][column];
		}
}

void printStrings(const StrArray& myNames)
{
	std::cout << "\nThe 12 names/words you entered were:\n";

	for (size_t row = 0; row < MAXROWS; ++row) {
		for (size_t column = 0; column < MAXCOLS; ++column)
			std::cout << myNames[row][column] << ' ';

		std::cout << '\n';
	}
}


Last edited on
Where possible, favour pre-inc rather than post-inc.
Just saying -- it really doesn't matter for ints as far as performance is concerned. In fact, the assembly will be exactly the same if you start index at 1 but post-increment. But in this case with the index variable, I'd agree it's slightly cleaner to use pre-increment.
https://godbolt.org/z/4ThfPYW85
https://godbolt.org/z/bM1Gb7fqb
Agree, but it does matter for performance for some classes etc - so IMO get into the habit of using pre-inc for when it does matter.
Topic archived. No new replies allowed.