Thread: Problem using getLine()

  1. #1
    Registered User
    Join Date
    Nov 2011

    Problem using getLine()

    I am trying to use getline to read in a string but its not working
    #include <cstdlib>
    #include <iostream>
    using namespace std;
    int main(int argc, char** argv) {
        char *first, last;
        string s;
      cout << "Enter your first anames: ";
      cout << "Your initials are " << first ;
        return 0;

  2. #2
    Registered User
    Join Date
    Oct 2006
    you need to explain exactly in what way it's not working.

    that being said, you're not allocating any space for reading data. you can't just declare a pointer and expect to read data into memory at the address that it represents. you need to allocate memory using new.
    since you're already using string, although you didn't include the header for it, you should just use std::getline instead of cin.getline. it uses string instead of char pointers, and is much safer and less error prone. use of char pointers for storing text strings is a vestigial feature for compatibility with C, and really should not be used in pure C++ code.

  3. #3
    Registered User
    Join Date
    Apr 2010
    It's also (in my opinion) far nicer dealing with string manipulation using the std::string and its functions. Love c++ for this. Once you get used the built in string functions (maybe methods is more appropriate), you will not want to go back.
    W7, Ubuntu -- mingw, gcc, g++, code::blocks, emacs, notepad++

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My problem with getline()
    By Ducky in forum C++ Programming
    Replies: 2
    Last Post: 01-09-2008, 01:24 PM
  2. Getline problem
    By ktar89 in forum C++ Programming
    Replies: 3
    Last Post: 06-24-2007, 06:47 AM
  3. getline problem
    By Steph in forum C++ Programming
    Replies: 7
    Last Post: 01-18-2002, 06:25 PM
  4. Getline Problem
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 12-09-2001, 06:31 PM
  5. getline problem
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 10-06-2001, 09:28 AM