getline under Xcode on a Mac

This is a discussion on getline under Xcode on a Mac within the C++ Programming forums, part of the General Programming Boards category; I've been beating my head against a wall for an hour, wondering why getline was not working for my program. ...

  1. #1
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309

    getline under Xcode on a Mac

    I've been beating my head against a wall for an hour, wondering why getline was not working for my program. Pretty simple use case:

    Code:
    getline(cin, mystring) ;
    It would work fine when I typed a single character, but when I pasted in a string, it would NOT read it in - I would get an empty string.

    Then, I figured it out. Xcode must have a bug or restriction so that when you paste in a value for an input request in the Console, it ignores it. Ugg.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  2. #2
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main ()
    {
    	string mystring;
    	getline(cin, mystring);
    	cout << mystring << endl;
    	return 0;
    }
    I can't even type in a single character:
    Code:
    Untitled(58970) malloc: *** error for object 0x1000041c0: pointer being freed was not allocated
    *** set a breakpoint in malloc_error_break to debug
    Program received signal:  “SIGABRT”.
    The strange thing is that the program works normally in both Terminal.app and gdb, so I don't know what the Debugger Console in Xcode is doing to produce the crash.

  3. #3
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875

    cin and string objects

    I didn't think the STL worked like that (getline() is looking for an array):
    Code:
    // istream getline
    #include <iostream>
    using namespace std;
    
    int main () {
      char name[256], title[256];
    
      cout << "Enter your name: ";
      cin.getline (name,256);
    
      cout << "Enter your favourite movie: ";
      cin.getline (title,256);
    
      cout << name << "'s favourite movie is " << title;
    
      return 0;
    }
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,436
    Quote Originally Posted by jeffcobb
    I didn't think the STL worked like that (getline() is looking for an array):
    Well, now you know that there is also a free function version that works for std::basic_string
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Jeff, Memloop and I are using the global getline that is part of the string class. You are using the stream flavor.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  6. #6
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Quote Originally Posted by laserlight View Post
    Well, now you know that there is also a free function version that works for std::basic_string
    See? I learn something new everyday. Most of my C++ know-how is more than 15 years old...question: at one point it was common knowledge that you did not mix malloc/free and new/delete....but now you say there is a C++ version of free() that works with strings...so is the "no-mixing" rule still valid?

    Happy Holidays all..

    J.
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  7. #7
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Laserlight might have meant free, as in "no charge - you get it for free".
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  8. #8
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    611
    Quote Originally Posted by Dino View Post
    Laserlight might have meant free, as in "no charge - you get it for free".
    I think it was meant more in a "not a member function" context.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mac OS X Users/C programmers?
    By petermichaux in forum C Programming
    Replies: 16
    Last Post: 04-18-2011, 06:36 AM
  2. How to Send Mac Address From Client to Server
    By Lieyza197 in forum C Programming
    Replies: 2
    Last Post: 05-27-2009, 09:58 AM
  3. Newbie Question - fflush(stdin) & fpurge(stdin) on Mac and PC
    By tvsinesperanto in forum C Programming
    Replies: 34
    Last Post: 03-11-2006, 11:13 AM
  4. mac os x xcode
    By magus2500x in forum C++ Programming
    Replies: 2
    Last Post: 11-02-2005, 11:12 PM
  5. getline help
    By ProjectsProject in forum C++ Programming
    Replies: 3
    Last Post: 06-14-2004, 11:12 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21