Please help with a newbie question

This is a discussion on Please help with a newbie question within the C++ Programming forums, part of the General Programming Boards category; Hello everyone, I just started reading the <jumping into c++>, it's a fantastic book. I got a question about sample ...

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    1

    Question Please help with a newbie question

    Hello everyone,
    I just started reading the <jumping into c++>, it's a fantastic book.
    I got a question about sample Code 9: string_append.cpp
    I made 2 changes with the sample, then the program stopped working properly.
    I better show you:

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    int main ()
    {
    string user_first_name;
    string user_last_name;
    string user_full_name = user_first_name + " " + user_last_name;
    cout << "Please enter your first name: ";
    cin >> user_first_name;
    cout << "Please enter your last name: ";
    //cin >> user_last_name;
    getline(cin, user_last_name, '\n');
    //string user_full_name = user_first_name + " " + user_last_name;
    cout << "Your name is: " << user_full_name << "\n";
    cin.get();
    cin.ignore();
    }
    As you can see, the two "//" commented out lines were originally in the sample, but I put string user full name on top, and used getline instead of cin. Then this happened:
    Name:  result1.jpg
Views: 438
Size:  21.5 KB
    If I only change the position of string user full name, without changing cin to getline, this is what happened: no display
    Name:  result2.jpg
Views: 304
Size:  22.6 KB
    I'm using visual studio 2010, and I have no idea why this is happening, please help
    thanks a lot guys

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    2,898
    This is being caused by mixing the extraction operator>> with getline(). The extraction operator leaves the end of line character in the stream that getline() accepts for it's complete input. You may want to check this boards FAQ, there is an entry on how to solve this problem.

    Jim

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,915
    >>string user_full_name = user_first_name + " " + user_last_name;
    >>cout << "Please enter your first name: ";
    >>cin >> user_first_name;
    Also, clearly, there is no point in putting together the full name before you've actually collected the first and last names.
    Remember: code is executed sequentially, so only before your cout statement that prints the full name do you have the first and last names stored in your variables.
    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. Newbie with Very Newbie Question
    By Jedi_Mediator in forum C++ Programming
    Replies: 18
    Last Post: 07-01-2008, 09:00 AM
  2. newbie question
    By ssjnamek in forum Windows Programming
    Replies: 2
    Last Post: 11-12-2005, 02:25 PM
  3. Newbie question ...
    By SkinneyEd in forum C++ Programming
    Replies: 1
    Last Post: 10-17-2005, 03:52 PM
  4. C++ newbie / linux not so newbie question
    By goldmonkey in forum C++ Programming
    Replies: 7
    Last Post: 12-13-2003, 12:27 PM
  5. newbie question about i/o!!!!!!!!help!!!!11
    By incognito in forum C++ Programming
    Replies: 7
    Last Post: 03-14-2002, 09:48 PM

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