Thread: why does it do this?

  1. #1
    Unregistered
    Guest

    why does it do this?

    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;	
    }

  2. #2
    I am the worst best coder Quantrizi's Avatar
    Join Date
    Mar 2002
    Posts
    644
    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.

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    200
    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.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >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.

  5. #5
    I am the worst best coder Quantrizi's Avatar
    Join Date
    Mar 2002
    Posts
    644
    Like what I said, use the chars like this:
    char name[20];
    char name2[20];

  6. #6
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    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.

  7. #7
    Registered User
    Join Date
    Apr 2002
    Posts
    200
    Assuming the original poster does want multiple characters inputted, then std::string should be the prefered method.
    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...

    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.

  8. #8
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    >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.

  9. #9
    I am the worst best coder Quantrizi's Avatar
    Join Date
    Mar 2002
    Posts
    644
    Don't you think that using a if statement could check to see if it is more than 20 or so?

  10. #10
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    >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.

Popular pages Recent additions subscribe to a feed