Thread: getline command

  1. #1
    Registered User
    Join Date
    Jul 2019
    Posts
    2

    Question getline command

    I'm a newbe to c++ and have been researching the getline command
    in Xcode I have gotten to this stage



    std::string handle, title;


    std::cout << "Enter your name: " << std::endl;
    std::getline (std::cin , handle ) ;

    std::cout << "Enter your favourite movie: " << std::endl;
    std::getline ( std::cin , title) ;

    std::cout << handle << "'s favourite movie is " << title << std::endl;

    but the


    std::cout << "Enter your name: " << std::endl;
    std::getline (std::cin , handle ) ;

    doesn't give a prompt for user input but


    std::cout << "Enter your favourite movie: " << std::endl;
    std::getline ( std::cin , title) ;

    does. Can anyone tell me why






  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Is there any code that you aren't showing us? Put it another way: is this the entire content of the main function?
    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

  3. #3
    Registered User
    Join Date
    Jul 2019
    Posts
    2
    Thankyou all for your input but I have solved my problem using this format
    and example I found in a book hope this helps someone else out::

    Code:
    #include <iostream>
    #include <string>
    
    
    usingnamespacestd;
    
    
    int main(int argc, const char * argv[]) {
    // insert code here...
    
    string greatstring ("Hello std::string ! ");
        cout << greatstring << endl;
    
    cout << " Enter a line of text " << endl;
        string firstLine;
        getline (cin, firstLine);
    
    cout << " Enter another " << endl;
        string secoundLine;
        getline (cin, secoundLine);
    
    cout << " Result of concatenation: " << endl;
        string concatString = firstLine + " " +secoundLine;
        cout << concatString << endl;
    
    cout << " Copy of concatenated string: " << endl;
        string aCopy;
        aCopy = concatString;
        cout << aCopy << endl;
    
        cout << " Length of concat string: " << concatString. length() << endl;
    
    
    
    return0;
    }
    
    

  4. #4
    Registered User
    Join Date
    Jul 2019
    Posts
    2
    the code in the first post is working! without any problems .
    there are some typo errors in the second code .
    like
    Code:
    usingnamespacestd;
    
    should be
    Code:
    using namespace std;
    and
    Code:
    return0;

    should be
    Code:
    return 0;

    happy coding .

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 05-15-2017, 07:24 AM
  2. Replies: 3
    Last Post: 12-13-2011, 07:32 AM
  3. Hi, I'm having some difficulty with the getline command:
    By kocmohabt33 in forum C++ Programming
    Replies: 2
    Last Post: 09-26-2010, 07:57 AM
  4. difference between getline and cin.getline
    By bartinla in forum C++ Programming
    Replies: 3
    Last Post: 11-13-2004, 09:47 AM
  5. getline/substr command help
    By ProLin in forum C++ Programming
    Replies: 2
    Last Post: 01-17-2002, 12:57 PM

Tags for this Thread