Skip navigation

Xcode entering input error help please

735 Views 4 Replies Latest reply: Nov 25, 2012 4:38 PM by quiban92 RSS
quiban92 Level 1 Level 1 (0 points)
Currently Being Moderated
Nov 11, 2012 10:04 PM

When i run Xcode 4.5.2 on Lion and i need to enter some input (ex. cin.getline(array,10)), i need to press enter two times to keep going.

Here is the code:    

 

player1 and palindrom are arrays of char

Sin título.png

and here is the output:

Sin título.png

I enter Juan in player1 then i press [Enter] and i need to press another [Enter] if i want the prgram to continue

and when i enter the palindrom is not stored in the array, instead the second [Enter] is stored on the palindrom array.

 

Help please

i dont know what to do i have looked on the internet but i havent found anything to solve this problem......

Xcode 4.5.2, Mac OS X (10.7.5), input error
  • Jongware Level 2 Level 2 (265 points)
    Currently Being Moderated
    Nov 13, 2012 7:17 AM (in response to quiban92)

    You are mixing up two different ways of getting user input.

     

    1. getline -- a standard <stdio> function which reads user input and a return;

    2. cin -- a C++ <iostream> function which reads user input and a return.

     

    See http://crasseux.com/books/ctutorial/getline.html and http://www.htmlhelpcentral.com/messageboard/showthread.php?4359-C-Tutorial-1-cou t-cin for the proper way to use either one of them; you cannot use both in the same command.

     

    quiban92 wrote:

     

    i dont know what to do i have looked on the internet but i havent found anything to solve this problem......

     

    Those two links were the very first hits on a search for "getline tutorial" and "cin tutorial".

  • Jongware Level 2 Level 2 (265 points)
    Currently Being Moderated
    Nov 14, 2012 3:30 AM (in response to quiban92)

    Apologies about my 'getline' mishap -- it's a valid cin function after all.

     

    Some further research led me to this: cin.getline(dest, count) reads up to count characters and ignores everything that follows. Perhaps your NAME_LENGTH value is 4? In that case, cin.getline is done right after you entered "Juan", and the hard return is left dangling for the next command. (See http://www.cplusplus.com/reference/iostream/istream/getline/ for a better description.)

     

    I found it's commonly suggested to add an 'ignore' right after each getline, to prevent any unwanted characters in the next input line.

Actions

More Like This

  • Retrieving data ...

Bookmarked By (0)

Legend

  • This solved my question - 10 points
  • This helped me - 5 points
This site contains user submitted content, comments and opinions and is for informational purposes only. Apple disclaims any and all liability for the acts, omissions and conduct of any third parties in connection with or related to your use of the site. All postings and use of the content on this site are subject to the Apple Support Communities Terms of Use.