Thread: getline problem

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    11

    getline problem

    3. Assume the array name is defined as follows:

    char name[25];

    A) Using a stream manipulator, write a cin statement that will read a string into name, but will read no more characters than name can hold.

    b) Using the getline function, write a cin statement that will read a string into name but that will read no more characters than name can hold.

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    
    int main()
    {
    	char name[25];
        getline(cin, name);
    	
    cout << name << endl;
    	
    	
    	
    	
    system("pause");
    return 0;
    }

    The error is in the way I used getline, what did I do wrong ?

  2. #2
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Take a look at the definition of std::getline again. Pay particular attention to the type of arguments it expects.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    There are two different getline() functions. One works with std::string, which is the version you are trying to use, the other works with a C-string.

    Jim

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. Think I have a getline problem....
    By sunburnbyRA in forum C++ Programming
    Replies: 8
    Last Post: 04-08-2003, 03:44 PM
  3. cin.getline problem...
    By davidvoyage200 in forum C++ Programming
    Replies: 10
    Last Post: 02-18-2003, 07:14 PM
  4. getline problem
    By Steph in forum C++ Programming
    Replies: 7
    Last Post: 01-18-2002, 06:25 PM
  5. Getline Problem
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 12-09-2001, 06:31 PM