Thread: Single quotes or double re: lesson from Jumping in to c++

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

    Single quotes or double re: lesson from Jumping in to c++

    Hi.First time poster, please be gentle with me lol.

    I'm trying to work out why this code fails at
    Code:
     getline( cin, username, "\n");
    if I use double quotes instead of the single quotes which was used in the example from the book.

    Here is the full code that works with single quotes at each "getline line". I guess what I'm asking is if there is a difference or am I using the wrong header? I was sure that you could use either but I cant find where I read that now.

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    int main ()
    {
        string username;
        string password;
        cout << "Enter your username: " << "\n";
        getline( cin, username, '\n');
        cout << "Enter your password: " << "\n";
        getline ( cin, password, '\n');
        if (username == "root" && password == "xyzzy" )
        {
            cout << "Access Allowed" << "\n";
        }
    else
    {
        cout << "Bad username or password.   Access Denide!" << "\n";
        return 0;
    }
    }
    Thanks for any assistance.
    Oh I'm using Code::Blocks 13.12! if that makes any difference.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    getline (string) - C++ Reference
    There are two versions of the function.

    > I'm trying to work out why this code fails at
    > getline( cin, username, "\n");
    Neither of which take a string as the third argument.

    Single quotes are used to specify a single character.

    Double quotes are for an array of characters (often called a string) with a \0 character at the end to mark the end of the string.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jan 2014
    Posts
    4
    Thanks very much mate.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 10-05-2011, 02:17 PM
  2. single vs double quotes
    By innuendo in forum C++ Programming
    Replies: 17
    Last Post: 11-03-2008, 05:38 PM
  3. multiple characters within single quotes
    By BattlePanic in forum C Programming
    Replies: 4
    Last Post: 05-06-2008, 02:59 AM
  4. Multi line quotes used on single lines
    By CBUK in forum C Programming
    Replies: 7
    Last Post: 02-03-2005, 08:11 AM
  5. outputing quotes(') and double quotes(")
    By lostboy in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 02-26-2002, 06:17 PM