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~