Like Tree2Likes
  • 2 Post By Elkvis

Problem using getLine()

This is a discussion on Problem using getLine() within the C++ Programming forums, part of the General Programming Boards category; I am trying to use getline to read in a string but its not working Code: #include <cstdlib> #include <iostream> ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    118

    Problem using getLine()

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

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    2,138
    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.
    Salem and Ocifer like this.

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    79
    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, SL6 -- 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, 12: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, 05:25 PM
  4. Getline Problem
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 12-09-2001, 05:31 PM
  5. getline problem
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 10-06-2001, 09:28 AM

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