Array Display Issue

I am having an issue with displaying a certain array. I take in input from the user for six separate arrays, and then write them to a another array to be used for display. Here is my code for the function:
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
void createTeams()
{
	cout << "ENTER THE TEAM NUMBER FOR EACH TEAM\nRED TEAM 1: ";
	cin >> redTeamOneName; //here?
	cout << "RED TEAM 2: ";
	cin >> redTeamTwoName;
	cout << "RED TEAM 3: ";
	cin >> redTeamThreeName;
	cout << "\nBLUE TEAM 1: ";
	cin >> blueTeamOneName;
	cout << "BLUE TEAM 2: ";
	cin >> blueTeamTwoName;
	cout << "BLUE TEAM 3: ";
	cin >> blueTeamThreeName;

	for (int i = 4; i != 10; i += 2)
	{
		for (int x = 0; x != 4; x++)
		{
			if (i == 4)
				screen[i][11 + x] = redTeamOneName[x];//or here?
			else if (i == 6)
				screen[i][11 + x] = redTeamTwoName[x];
			else if (i == 8)
				screen[i][11 + x] = redTeamThreeName[x];
		}	
	}

	for (int i = 4; i != 10; i += 2)
	{
		for (int x = 0; x != 4; x++)
		{
			if (i == 4)
				screen[i][45 + x] = blueTeamOneName[x];
			else if (i == 6)
				screen[i][45 + x] = blueTeamTwoName[x];
			else if (i == 8)
				screen[i][45 + x] = blueTeamThreeName[x];
		}
	}
}

My issue coincides with the display of redTeamOneName[4] array. I declare it in the begining of my program as:

static char redTeamOneName[4];

The problem is that when I try to display the screen[20][69] array, it only shows three of the digits from redTeamOneName[4]. In place of the first digit is a blank, then the other three display normally. The other five coloured team name arrays work perfectly.

I inserted comments about where the problem may be taking the place, but I have no real idea. This is where I first thought the issue may be. If you can help, that would be great.
To read a string of characters, use the member getline function.

 
cin.getline(redTeamOneName, 4);


Better yet, use std::string and a different getline, so you don't have to worry about length.

1
2
3
4
5
std::string teamNames[6];
for(size_t i(0); i < 6; ++i){
    std::cout << "Please enter the name of team " << (i+1) << ": ";
    std::getline(cin, teamNames[i]);
}
I can't use a string efficiently to copy this as then I would have to take each position in the string to copy it to the other array, so that veto's the second option. I tried using the cin.getline as you suggested, but then it just skips the other five inputs. I know there is some sort of clear member function to do this, but I think it seems inefficient. And in the end, it kind of reversed the issue. Instead of skipping the first number and displaying the last three, it displayed the first three then skipped the last digit. The way I was doing it with cin was at least working for the other five teams. If you have any other suggestions or questions about the program, I would be happy to try them or answer them to the best of my ability.
I'm really sorry about that. I neglected to mention that character arrays must be large to hold the string plus 1. That extra space is for the null-terminated character to indicate the end of a string. According to this: http://www.cplusplus.com/reference/istream/istream/getline/?kw=istream%3A%3Agetline
istream::getline also reads only up to n-1 characters, saving '\0' as the last. So my previous code snippet really should have been:
 
cin.getline(redTeamOneName, 5);

What probably happened was that a 4 character string was entered, but because of my faulty example, the function only expected three, setting the failbit flag which caused it to ignore the rest of the input.
Hope this helps.

Just out of curiosity:
Why can you not use cin.clear()? All it does is set some value to 0, which shouldn't be inefficient in any way. Even when the flags are set, bitwise operators are used, which are also not inefficient.
Is there a particular reason you cannot switch to using std::string instead of character arrays?
I just feel that it would be ridiculous to have to call cin.clear() five or six times when my stategy at the present time works for all of them. I fixed up the getline() function and it now takes in all six names like before, but I end up with the same issue. I have included a link to a pastebin file that has the entirety of the program in it. It doesn't have much function right now and is very incomplete, I was dealing with the grunt work of the display first, until I ran into this issue. There aren't very many comments, but you should be able to find my function in there pretty easily. And like I have mentioned before, I do not desire to use std::string as I am copying characters from the small name array to a larger array. I know that I can take certain characters from a string to accomplish this, but just using a character array is a) more lightweight and b) simply easier to do. Like I said again, my current strategy works for the other five names. It just seems to neglect the first one. I appreciate all the help you have given my so far.

http://pastebin.com/6mmgdahZ
As I have mentioned before, a character array must have enough space for the C-string and the null-terminating character. If you have a C-string "TEAM", the character array must have a size of 5, e.g.
 
const char teamname[5] = "TEAM\0";


Without making suggestions that will alter the majority of your code design, I can't really help you any further. Perhaps another, more experienced programmer can chime in.

Observations:
-I am confused by the inclusion of both the windows and the (deprecated) conio headers. Is this program going to be a windowed or console application? The rest of the program seems to indicate it is going to be a console application.
-Conio is no longer standard. There are libraries better suited for console manipulation such as pdcurses. The downside is that another library does mean learning how to use the API.
-There are a lot of magic numbers that are wreaking havoc with readability. Instead of placing comments to clarify the ASCII codes, you can simply use char values, e.g.
1
2
3
4
5
int ch = cin.get();
if(ch == '!')
   cout << "You pressed an exclamation mark!";
else
   cout << "I do not know what character " << ch << " is...";

-Also remember that you do not have to care if something is uppercase or lowercase. <cctype> has handy-dandy tolower() and toupper() functions (they are easy to write yourself, too).
-I don't if it would help you, but there is something called a switch-case that can ease all those if statements, e.g.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
char ch = cin.get();
tolower(ch);
switch(ch){
    case 'w': cout << "Up, up and away!\n"; break;
    case 'a': cout << "To the left now!\n"; break;
    case 's': cout << "Why so down?\n"; break;
    case 'd': cout << "Always right with you, isn't it?\n"; break;
    case 'm': //Note here how I can have multiple cases activate the same action
    case 'p':
    case 'q':
    case 'z':
        cout << "Doing some special, secret action Daleth was too lazy to implement.\n";
        break;
    default: cout << "What now?\n"; break;
}

-Considering you can treat std::string as a character array anyway and that it has capabilities making it much more powerful than a simple C-array... I don't see much of a reason not to use it in your situation.

I do not want to discourage you, but I think you are making this program much harder than it needs to be. Believe me, I ran into the same frustrations not too long ago when I tried tackling a rogue game despite having almost no experience.
I suggest reading up on the rest of this website's (or one you are comfortable with) tutorial, paying close attention on Classes and Object-Oriented Programming (OOP). OOP will help you eliminate copy-pasting while improving readability and, simply put, make programming games a whole lot easier.
I also suggest tackling very simple, small games and build your way up back to this one. Perhaps then, you will find yourself questioning why you struggled over such an easy program (maybe).

~Daleth~
Topic archived. No new replies allowed.