Two Dimensional char Array - Input Problems

Hello!
I am trying to build a 2D char array as a part of a bubble sort program I'm working. Something is really hokey with my code, but I'm not sure what; I've searched a while for some applicable examples but have only found a couple (and they don't seem to be working for me).
Anyway, when I input the number of names I want to read-in, the program keeps reading for names passed the user's specification. It should stop, and then recount the names back to the user.

I've taken some breaks from looking at it, but when I come back, I can't seem to make progress passed this point! Can someone please give it a look? :)

I've included main and ReadNames, though ReadNames is specifically where I've hit a bump:

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>

using namespace std;

#include <string.h>

const int MaxNames = 20;
const int MaxChars = 15;

void main ()
	{
	char Chart [MaxNames + 1][MaxChars + 1];	
	int NumNames;
	
	cout << "How many names do you wish to enter?" << endl;
	cin >> NumNames;
	
	while (NumNames > MaxNames)
		{
		cout << "Wow! That's too many names! The limit is 20. Try again.\nHow many names? ";
		cin >> NumNames;
		}
	while (NumNames == 0 || NumNames < 0)
		{
		if (cin.fail())
			{
			cin.clear();
			char c;
			cin >> c;
			}
		else
			{
			cout << "ERROR: Input contained invalid numbers or characters. You must have a number from 1 - 20. Try again.\nHow many names? ";
			cin >> NumNames;
			}
		}
		
		ReadNames (Chart, NumNames);		
	}



void ReadNames (char Chart [MaxNames + 1][MaxChars + 1], int NumNames)
	{
	int i, j;

	do
	{
	char Name [MaxChars] = "";
	cout << "Enter a name: ";
	cin >> Name;
	for ( i = 0; i < NumNames; i++)
		for ( j = 0; j < MaxChars; j++)
			Chart [i][j] = Name [j];
	} while (i < MaxChars);

	cout << endl;
	for ( i = 0; i < NumNames; i++)
		for ( j = 0; j < MaxChars; j++)
			cout << Chart [i][j];
			
	cout << endl;
		
	}

Line 52 condition and line 55. Variable i is limited to NumNames (for example, I only wanted 3 names) but on line 55, i is compared to MaxChars, which is 15?
Thanks! That made the loop print the specified NumNames! :)

But now, it reads-in the first name and prints that name over again, NumNames amount of times. This was my original debacle before I started getting frustrated and making mistakes. I intended for the do-while loop to allow the user to enter a different name each time it loops, not the first name over again. How can I fix this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void ReadNames (char Chart [MaxNames + 1][MaxChars + 1], int NumNames)
	{
	int i, j;

	do
	{
	char Name [MaxChars] = "";
	cout << "Enter a name: ";
	cin >> Name;
	for ( i = 0; i < NumNames; i++)
		for ( j = 0; j < MaxChars; j++)
			Chart [i][j] = Name [j];
	} while (i < NumNames);

	cout << endl;
	for ( i = 0; i < NumNames; i++)
		for ( j = 0; j < MaxChars; j++)
			cout << Chart [i][j];
			
	cout << endl;
		
	}
Use getline instead of cin to get the string from the console and see what happens.
I replaced cin with getline, and also tried initializing Names as a string (I went to the example page for using getline; this is all very new for me). It cleaned up without any errors, but it doesn't take any names when run. I don't think line 12 can assign properly... I took the array attribute off of Name briefly, but it gave me errors that it could not convert string to char [], so I ended up leaving it...
I commented out what I originally had:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void ReadNames (char Chart [MaxNames + 1][MaxChars + 1], int NumNames)
	{
	int i, j;

	do
	{
	string Name; //char Name [MaxChars] = "";
	cout << "Enter a name: ";
	getline (cin, Name); // cin >> Name;
	for ( i = 0; i < NumNames; i++)
		for ( j = 0; j < MaxChars; j++)
			Chart [i][j] = Name [j];
	} while (i < NumNames);

	cout << endl;
	for ( i = 0; i < NumNames; i++)
		for ( j = 0; j < MaxChars; j++)
			cout << Chart [i][j];
			
	cout << endl;
		
	}
Line 10 looks redundant because of line 13? Line 12 looks fine to me.
I got it! I was just confused about how to work a two dimensional array, I guess. Thanks for helping, y'all. This is what I've ended with; it works perfectly:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void ReadNames (char Chart [MaxNames + 1][MaxChars + 1], int NumNames)
	{
	int i;

	for (i = 0; i < NumNames; i++)
		{
		cout << "Enter a name: ";
		cin >> Chart [i];
		}

	cout << endl;
	for ( i = 0; i < NumNames; i++)
		{
		cout << Chart [i] << endl;
		}
		
	}
Topic archived. No new replies allowed.