Two name into one string variable problem

This is a discussion on Two name into one string variable problem within the C++ Programming forums, part of the General Programming Boards category; Hello my friends, I'm Panos and this is my first post here in CBoard Forums Take a look to my ...

  1. #1
    Registered User Dr.Paneas's Avatar
    Join Date
    Nov 2009
    Location
    GR
    Posts
    12

    Two name into one string variable problem

    Hello my friends, I'm Panos and this is my first post here in CBoard Forums

    Take a look to my C++ code:

    Code:
    #include <iostream>
    #include <string>
    
    int main()
    {
        std::cout << "What is your name? ";
        std::string name;
        std::cin >> name;
        std::cout << "Hello, " << name << std::endl << "And what is yours? ";
        std::cin >> name;
        std::cout << "Hello, " << name << " nice to meet you too!" << std::endl;
        return 0;
    }
    No errors. Let's launch this:
    Name:  test1.jpg
Views: 126
Size:  14.3 KB

    Everything work perfect. Now let's launch it again giving two names (firstname & lastname) as input.
    Name:  test2.jpg
Views: 77
Size:  15.4 KB

    As you can see, it doesn't prompt me to insert the second name because it uses the my lastname (Georgiadis) and treat it like this way.

    Is there any way to avoid this happening ?

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    You should use std::getline to read in an entire line of text. Using std::cin >> stops at the first whitespace, but std::getline will read the entire line including spaces.

  3. #3
    Registered User Dr.Paneas's Avatar
    Join Date
    Nov 2009
    Location
    GR
    Posts
    12
    Quote Originally Posted by Daved View Post
    You should use std::getline to read in an entire line of text. Using std::cin >> stops at the first whitespace, but std::getline will read the entire line including spaces.
    Thank you Daved

  4. #4
    Registered User
    Join Date
    Sep 2009
    Posts
    68
    Is there a reason you don't just use

    Code:
    using namespace std;
    I think it cleans it up a lot and makes it much easier to read.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    >> I think it cleans it up a lot and makes it much easier to read.

    I prefer the way the OP does it. It makes things clearer (IMO) and more consistent, especially when programs get larger. Since it's bad practice to use the using directive in header files, for example, you end up typing std::string in the header and string in the source file. Plus I prefer to see that the name comes from the standard library as opposed to from my code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C/C++ String Problem.
    By Jaken Veina in forum C++ Programming
    Replies: 7
    Last Post: 07-09-2005, 10:11 PM
  2. string pattern search problem
    By goron350 in forum C Programming
    Replies: 6
    Last Post: 11-25-2004, 07:50 AM
  3. Another overloading "<<" problem
    By alphaoide in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2003, 10:32 AM
  4. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-26-2003, 11:03 PM
  5. ........ed off at functions
    By Klinerr1 in forum C++ Programming
    Replies: 8
    Last Post: 07-29-2002, 09:37 PM

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