Like Tree1Likes
  • 1 Post By Elkvis

Why can't use std::getline() in Xcode???

This is a discussion on Why can't use std::getline() in Xcode??? within the C++ Programming forums, part of the General Programming Boards category; Hi! I'm a very new freshman without any programming experience. Not sure if I posted my question in the right ...

  1. #1
    Registered User
    Join Date
    Sep 2013
    Location
    Kota Kinabalu, Malaysia
    Posts
    2

    Question Why can't use std::getline() in Xcode???

    Hi! I'm a very new freshman without any programming experience. Not sure if I posted my question in the right space. Sorry. Recently I was stuck in Xcode while trying to perform the std::getline(). Here's my code:

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
    string username;
    string passwords;
    cout<<"Username:"<<"\n"; getline(cin, username, "\n"); cout<<"Passwords:"<<"\n"; getline(cin, passwords, "\n") if (username=="Root" && passwords=="xyzz")
    {cout<<"Welcome Root.";}
    else
    {cout<<"Please re-check Username&Passwords.";}
    }
    So is there any mistakes? Cause I couldn't run the program, because right after I typed in "getline()", Xcode display a small Exclamation Mark next to it and written "get line() file not found" and "No matching function for call to getline". So can somebody tell me why I fail to run it and provided with solutions. Is there any setting in Xcode need to be done before using getline()? Thank you so much.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,461
    The problem is that you passed a string literal as the third argument to getline. You should pass a char instead, e.g.,
    Code:
    getline(cin, username, '\n');
    or allow it to default to '\n':
    Code:
    getline(cin, username);
    By the way, you are missing a semi-colon.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Sep 2013
    Location
    Kota Kinabalu, Malaysia
    Posts
    2
    Thanks for your answer^^ I'm using Xcode, I didn't do any setting. I just simply create a new project, cause I bought a book and learning. So there's a chapter teaching "getline()" and I stucked there even I used both "\n" and '\n' as you suggest and also no missing semicolon, but the result same. Xcode won't run and showing "getline file not found" and "no matching function for call to getline". Do I need to include something in order to use getline ()?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,461
    Quote Originally Posted by Marvin_MBA
    So there's a chapter teaching "getline()" and I stucked there even I used both "\n" and '\n' as you suggest and also no missing semicolon, but the result same.
    This program compiles for me:
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
        string username;
        string passwords;
    
        cout << "Username:" << "\n";
        getline(cin, username, '\n');
        cout << "Passwords:" << "\n";
        getline(cin, passwords, '\n');
        if (username == "Root" && passwords == "xyzz")
        {
            cout << "Welcome Root.";
        }
        else
        {
            cout << "Please re-check Username&Passwords.";
        }
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    2,305
    in the past, apple used '\r' as its newline character. not sure if this is still true. if you leave out the third parameter, it will default to the platform's standard newline character. try getline with just the first two parameters.
    Code:
    namespace life
    {
        const bool change = true;
    }

  6. #6
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by Elkvis View Post
    in the past, apple used '\r' as its newline character. not sure if this is still true. if you leave out the third parameter, it will default to the platform's standard newline character. try getline with just the first two parameters.
    Doesn't '\n' default to the platforms end-of-line character (or character sequence) anyway on output?
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    2,305
    Quote Originally Posted by oogabooga View Post
    Doesn't '\n' default to the platforms end-of-line character (or character sequence) anyway on output?
    no. if you specify a delimiter to use with getline, it uses that literal character.
    oogabooga likes this.
    Code:
    namespace life
    {
        const bool change = true;
    }

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,461
    getline(cin, username) would be equivalent to getline(cin, username, cin.widen('\n')). I don't think the aim of widen is to change the '\n' to the newline sequence appropriate to the OS. Rather, the fact that the input stream used with std::getline should be in text mode means that '\n' would be converted to whatever is the appropriate newline sequence when necessary.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    @Elkvis:

    I want my like back!!!

    What you say suggests that the following shouldn't work correctly on Windows since the eol convention there is \r\n\ ? Are you saying that the \r is left at the end of the string? It doesn't seem to be.
    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    
    int main()
    {
        std::ifstream fin("some.txt");
        std::string line;
    
        // Read one line and print it's length:
        std::getline(fin, line, '\n');
        std::cout << line.size() << '\n';
    
        return 0;
    }
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  10. #10
    Registered User
    Join Date
    Oct 2006
    Posts
    2,305
    Quote Originally Posted by oogabooga View Post
    What you say suggests that the following shouldn't work correctly on Windows since the eol convention there is \r\n\ ?
    what I said suggests no such thing. since the \n is after the \r, it may grab the string, including the \r, and discard the \n.

    Edit: this is assuming that \r\n is in fact the newline sequence, and that the windows implementation of getline doesn't automatically also trim \r characters.
    Last edited by Elkvis; 09-04-2013 at 11:34 AM.
    Code:
    namespace life
    {
        const bool change = true;
    }

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,461
    Quote Originally Posted by Elkvis
    since the \n is after the \r, it may grab the string, including the \r, and discard the \n.
    As I implied in post #8, I believe that will only be the case if the input stream is opened in binary mode, but I don't think that's the norm when you intend to use std::getline.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    May 2010
    Posts
    2,698
    The only time you need to worry about the new line character sequences in text files is when the files were written by one operating system and being read by another operating system. For example if you write your file on Windows then take that file to Linux you must manually convert the line endings to the proper format, or take the differences into account when you read the file. The C++ streams use the operating systems line endings.

    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with Xcode
    By Zach Sisk in forum C++ Programming
    Replies: 2
    Last Post: 09-26-2012, 05:29 AM
  2. Replies: 3
    Last Post: 12-13-2011, 06:32 AM
  3. getline under Xcode on a Mac
    By Dino in forum C++ Programming
    Replies: 7
    Last Post: 12-26-2009, 10:37 AM
  4. Need help from you Xcode users
    By Memloop in forum Tech Board
    Replies: 7
    Last Post: 09-22-2009, 10:27 AM
  5. mac os x xcode
    By magus2500x in forum C++ Programming
    Replies: 2
    Last Post: 11-02-2005, 11:12 PM

Tags for this Thread


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