Thread: getline under Xcode on a Mac

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

    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.
    Mainframe assembler programmer by trade. C coder when I can.

  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
    28,413
    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
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    Chappell Hill, Texas
    Posts
    2,332
    Jeff, Memloop and I are using the global getline that is part of the string class. You are using the stream flavor.
    Mainframe assembler programmer by trade. C coder when I can.

  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
    Chappell Hill, Texas
    Posts
    2,332
    Laserlight might have meant free, as in "no charge - you get it for free".
    Mainframe assembler programmer by trade. C coder when I can.

  8. #8
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    639
    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, 12:13 PM
  4. mac os x xcode
    By magus2500x in forum C++ Programming
    Replies: 2
    Last Post: 11-03-2005, 12:12 AM
  5. getline help
    By ProjectsProject in forum C++ Programming
    Replies: 3
    Last Post: 06-14-2004, 11:12 AM