C++ Strings with spaces

This is a discussion on C++ Strings with spaces within the C++ Programming forums, part of the General Programming Boards category; Ok, i found this that works with spaces on the FAQ: Code: #include <iostream> #include <string> int main() { std::string ...

  1. #1
    Registered User
    Join Date
    Aug 2007
    Location
    Springfield, MO
    Posts
    4

    C++ Strings with spaces

    Ok, i found this that works with spaces on the FAQ:

    Code:
    #include <iostream>
    #include <string>
    
    int main()
    {
      std::string line;
    
      std::cout<<"Enter a string: ";
    
      if ( getline ( std::cin, line ) )
        std::cout<<"You entered \""<< line <<"\""<<std::endl;
    }
    and it works fine, but, i wanted to use it without the std:: for this program
    (Simple i know):

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    string pass;
    string user;
    
    int main()
    {
        cout<<"What is your username?(12 characters max): ";
        cin.getline ( user, 12, '\n' );
        cin.ignore();
        cout<<"What is your password?(12 characters max): ";
        cin.getline ( pass, 12, '\n' );
        cin.ignore();
        if (user == "The Master" && pass == "I am master")
        {
                 cout<<"Access is granted.\n";
                 cout<<"Press enter to end user authentication.";
                 cin.get();
        }
        else
        {
        cout<<"Access denied";
        cin.get();
    }
    }
    however when i do that, i get the following error from my compiler(Dev-C++):
    11 C:\Dev-Cpp\Tutorial\Inproved user authentication.cpp no matching function for call to `std::basic_istream<char, std::char_traits<char> >::getline(std::string&, int, char)'

    Along with a similar error for line 14, (The 11 is the line number).
    Both these errors seem to pertain to cin.getline which of course works fine if i use C strings instead. Ok, please tell me if there is a shorter code for the strings with spaces code, and if so how to use it. Thanks

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,758
    Notice that in your first example, you call std::getline() but in your second example, you call std::cin.getline(). The former is for std::string the latter for null terminated strings.
    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
    Apr 2007
    Posts
    51
    I think to save it to a string vs a char* you want to use

    Code:
    getline(cin,pass,'\n');
    vs
    Code:
    cin.getline()

  4. #4
    Registered User
    Join Date
    Aug 2007
    Location
    Springfield, MO
    Posts
    4
    Ok, thanks this works perfectly:
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    string pass;
    string user;
    std::string line;
    int main()
    {
        cout<<"What is your username?: ";
        getline ( cin, user, '\n' );
        cout<<"What is your password?: ";
        getline ( cin, pass, '\n' );
        if (user == "The Master" && pass == "I am master")
        {
                 cout<<"Access is granted.\n";
                 cout<<"Press enter to end user authentication.";
                 cin.get();
        }
        else
        {
        cout<<"Access denied";
        cin.get();
    }
    }

    Thanks alot, i'm new to programming or i probably would have figured that out by myself... well back to the tutorials lol

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,758
    As in your first code example, std::getline() does not need the third argument if it is to be the newline character. Also, I recommend avoiding global variables. I might write the code as:
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main()
    {
        string pass;
        string user;
    
        cout << "What is your username?: ";
        getline(cin, user);
    
        cout << "What is your password?: ";
        getline(cin, pass);
    
        if (user == "The Master" && pass == "I am master")
        {
            cout << "Access is granted.\n"
                    "Press enter to end user authentication.";
        }
        else
        {
            cout << "Access denied";
        }
        cin.get();
    }
    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

  6. #6
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    I'd also put the std:: back in.

    It's a bad habit to have "using namespace std;", that's more of a throwback to a time when pre-standard code was in wide use and it was necessary to quickly make older code compile on standard compilers. If you're writing new code, use std::string, std::cin, etc.

    Namespaces are a key benefit of the language, don't throw them away.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  7. #7
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,801
    Quote Originally Posted by Mordacai
    Ok, i found this that works with spaces on the FAQ:

    Code:
    #include <iostream>
    #include <string>
    
    int main()
    {
      std::string line;
    
      std::cout<<"Enter a string: ";
    
      if ( getline ( std::cin, line ) )
        std::cout<<"You entered \""<< line <<"\""<<std::endl;
    }
    To be a stickler, that first example you posted would require a std:: in front of the getline to actually "work" as you put it.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reading strings with spaces
    By dezz101 in forum C Programming
    Replies: 10
    Last Post: 10-29-2008, 06:02 AM
  2. reading strings with spaces using sscanf
    By kotoko in forum C Programming
    Replies: 2
    Last Post: 06-14-2008, 05:06 PM
  3. Strings allowing spaces?
    By Dan17 in forum C++ Programming
    Replies: 13
    Last Post: 02-04-2006, 02:15 PM
  4. Getting scaf to accept strings with spaces in them?
    By Wiretron in forum C Programming
    Replies: 4
    Last Post: 11-12-2005, 08:57 AM
  5. accepting long strings with spaces
    By trang in forum C Programming
    Replies: 2
    Last Post: 01-05-2004, 03:17 PM

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