Thread: Getline messing up code

  1. #1
    Registered User
    Join Date
    Jan 2014
    Posts
    17

    Getline messing up code

    Just started in on Allain's book and rather frustratingly ran into issues despite following the book to the letter. I'm trying to wrap my head around how getline works.

    The following piece of code compiles and runs fine.

    Code:
    #include <iostream>
    #include <string>
    
    
    int main()
    
    
    {
        std::string user_first_name = "test";
        
        std::cout << user_first_name << "\n";
        
        return 0;
    }
    But on the other hand this code, while it appears to compile, does nothing. It doesn't even print "test". Note getline @ 11. What am I missing? Thanks

    Code:
    #include <iostream>
    #include <string>
    
    
    int main()
    
    
    {
        std::string user_first_name = "test";
    
         std::getline( std::cin, user_first_name, '\n' );
        
        std::cout << user_first_name << "\n";
        
        return 0;
    }
    Code was edited to reflect that Elkvis first suggestion still doesn't run on my end.
    Last edited by jaxdid; 01-02-2014 at 08:44 AM. Reason: Edited code for clarity for possible new readers

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    getline is in the std namespace, just like cin, and cout. you're not giving it a namespace qualification, and you have no using statement, so it fails to find it.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  3. #3
    Registered User
    Join Date
    Jan 2014
    Posts
    17
    Quote Originally Posted by Elkvis View Post
    getline is in the std namespace, just like cin, and cout. you're not giving it a namespace qualification, and you have no using statement, so it fails to find it.
    Good point unfortunately I had tested that and it still doesn't work. I had previously tried:

    Code:
    std::getline( std::cin, user_first_name, '\n');
    Still no change.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    what operating system version and compiler version are you running?
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  5. #5
    Registered User
    Join Date
    Jan 2014
    Posts
    17
    I'm on OS X 10.9, using XCode 5.0.2. When I run the code the usual output box doesn't show anything. Although debugger reports using 276 KB of memory, it ends up filling up the space with bars.
    Last edited by jaxdid; 01-02-2014 at 08:57 AM.

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    is it possible that osx only supports unicode? what if you try using std::wstring, std::cout, and std::cin, and prefix all your string literals with a capital 'L'?
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  7. #7
    Registered User
    Join Date
    Jan 2014
    Posts
    17
    Sorry do you mind a bit of hand holding here? What would be an example of a string literal in my code where I should stick in the 'L'? Although the compiler did complain when I tried 'std::wstring'.

    Finally XCode appears to give me the option to change the Text Encoding...if not Unicode (UTF-8), what should I change it to? (Allain doesn't make any mention of this in the Mac set up section).

    Thanks for the help.
    Last edited by jaxdid; 01-02-2014 at 09:51 AM.

  8. #8
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    change the code in your first example to:

    Code:
    #include <iostream>
    #include <string>
    
    
    int main()
    
    
    {
        std::wstring user_first_name = L"test";
        
        std::wcout << user_first_name << L"\n";
        
        return 0;
    }
    and your second to:

    Code:
    #include <iostream>
    #include <string>
    
    
    int main()
    
    
    {
        std::wstring user_first_name = L"test";
    
         std::getline( std::wcin, user_first_name, L'\n' );
        
        std::wcout << user_first_name << L"\n";
        
        return 0;
    }
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  9. #9
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499
    I use Xcode all the time

    Code:
    getline( std::cin, user_first_name, '\n');
    drop the std:: when you use getline, you only need it for std::cin

  10. #10
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by jocdrew21 View Post
    drop the std:: when you use getline, you only need it for std::cin
    wrong. getline is still in the std namespace. in the absence of a using directive, you must qualify it with the namespace name.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  11. #11
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499
    Code:
    #include <iostream>
    #include <string>
    
    
    int main(int argc, const char * argv[])
    {
    
        std::string user_first_name = "test";
        
        std::cout << user_first_name << "\n";
        
        std::cout<<"Enter a string: ";
        std::getline( std::cin, user_first_name, '\n' );
        
        std::cout << user_first_name << "\n";
        
        return 0;
    }
    Yes sorry getline is apart of std::

    However I looked at your code again and seen getline was after your string. In other words it was waiting for you to send another string to user_first_name.

    Take at look my my code above, see the difference?

  12. #12
    Registered User
    Join Date
    Jan 2014
    Posts
    17
    Thanks to both of you, jocdrew21 and Elkvis. Problem solved.

    I was the boob in this case...

    Should I look into the wstring, wcout, etc for more info or is that a somewhat complex concept I can tackle later?

    Also interestingly, code still compiled and worked with and without std:: for 'getline' and without declaring a namespace or a using directive.
    Last edited by jaxdid; 01-02-2014 at 03:07 PM.

  13. #13
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by jaxdid View Post
    Should I look into the wstring, wcout, etc for more info or is that a somewhat complex concept I can tackle later?
    the w-prefixed version of cin, cout, string, etc. are useful for handling strings that contain unicode characters that cannot be represented with ascii.

    Also interestingly, code still compiled and worked with and without std:: for 'getline' and without declaring a namespace or a using directive.
    it's possible that the version of GCC that comes with xcode implicitly says "using std::getline;" when you compile your code. try other stuff without the std:: and see what happens.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  14. #14
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Also interestingly, code still compiled and worked with and without std:: for 'getline' and without declaring a namespace or a using directive.
    It's a feature called argument dependent lookup. It is thoroughly explained on this page.
    Argument-dependent lookup, also known as ADL, or Koenig lookup, is the set of rules for looking up the unqualified function names in function-call expressions, including implicit function calls to overloaded operators. These function names are looked up in the namespaces of their arguments in addition to the scopes and namespaces considered by the usual unqualified name lookup.

    Argument-dependent lookup makes it possible to use operators defined in a different namespace
    In this case, the argument std::cin added the std namespace to the lookup, so that when the compiler wanted to know what getline referred to, the compiler was able to resolve it.

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Apart from whiteflags said, while it is not necessary to add std:: to getline, you should consider it good practice to do so anyway.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 12-13-2011, 07:32 AM
  2. CWinThread messing itself up
    By LowlyIntern in forum C++ Programming
    Replies: 7
    Last Post: 01-16-2009, 08:08 AM
  3. Messing with folders
    By Probose in forum C++ Programming
    Replies: 2
    Last Post: 09-21-2006, 03:16 PM
  4. Is someone messing with m$ again?
    By exluddite in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 09-16-2004, 01:56 PM
  5. Do you think this might be messing up my brain?
    By incognito in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 01-21-2002, 08:33 AM