weird problem it skips the second cin... wtf?
Code:void GetPlayerNames(char &Player1, char &Player2) { cout<<"Player one's name: "; cin>>Player1; cout<<"Player two's name: "; cin>>Player2; }
weird problem it skips the second cin... wtf?
Code:void GetPlayerNames(char &Player1, char &Player2) { cout<<"Player one's name: "; cin>>Player1; cout<<"Player two's name: "; cin>>Player2; }
1. try making them seperate char's
(char name 1[20];
char name 2[20];
for example
2. Don't use the void with pointers, that might also be a problem.
Yeah, your problem is that you are using chars, and not char arrays or strings. When you enter player1's name as multiple chars, such as "fyodor", it reads the "f" into Player1, and the "y" into Player2. At least I think that's whats happening. And make sure you're not doing something wrong when calling the function, remember it will change the value of Player1 and Player2.
PS. does anybody else have problems telling some cap letters from their lowercase counterpoints in Notepad?
I go to encounter for the millionth time the reality of experience and to forge in the smithy of my soul the uncreated conscience of my race.
Windows XP consists of 32 bit extensions and a graphical shell for a 16 bit patch to an 8 bit operating system originally coded for a 4 bit microprocessor, written by a 2 bit company, that can't stand 1 bit of competition.
>At least I think that's whats happening.
Correct, Player1 and Player2 can only hold one character each. If you want to hold an entire name in each variable, either use the string class, or char arrays.
-Prelude
My best code is written with the delete key.
Like what I said, use the chars like this:
char name[20];
char name2[20];
Assuming the original poster does want multiple characters inputted, then std::string should be the prefered method.Like what I said, use the chars like this:
char name[20];
char name2[20];
Perhaps he would like to avoid the class overhead, perhaps he would like to keep it simple and learn the basics before he moves on to more advanced material like strings, perhaps he might need to read and modify C code...Assuming the original poster does want multiple characters inputted, then std::string should be the prefered method.
Would you recommend using vectors in place of arrays to a newbie?
I go to encounter for the millionth time the reality of experience and to forge in the smithy of my soul the uncreated conscience of my race.
Windows XP consists of 32 bit extensions and a graphical shell for a 16 bit patch to an 8 bit operating system originally coded for a 4 bit microprocessor, written by a 2 bit company, that can't stand 1 bit of competition.
>Perhaps he would like to avoid the class overhead
Perhaps they should learn not to prematurely optimise.
>perhaps he would like to keep it simple and learn the basics before he moves on to more advanced material like strings
Using std::string is keeping it simple. Using char name[20], means that the code they've written has the potential to crash the program (or maybe o/s), as well as being insecure. In order to enter a 'name' safely (and properly) the original poster would need to use some form of bounds checking and/or dynamic allocation. This is provided by std::string.
>Would you recommend using vectors in place of arrays to a newbie?
I wouldn't be the first.
Don't you think that using a if statement could check to see if it is more than 20 or so?
>Don't you think that using a if statement could check to see if it is more than 20 or so?
Yes if you extracted each character individually using something like cin.get(), which reads into a temporary char which is then copied into the array (did someone mention overhead?), otherwise by the time the length is tested it's too late, the memory is clobbered.