String getting cut off

This is a discussion on String getting cut off within the C++ Programming forums, part of the General Programming Boards category; Hello, I'm just learning C++, and I was working with C++-style strings when something stumbled me. Code: #include <iostream> using ...

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    3

    String getting cut off

    Hello, I'm just learning C++, and I was working with C++-style strings when something stumbled me.

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        string szOriginal, szPlease;
    
        cout<<"Please type a message for me to reprint with the word 'please' at the end!\n";
        cin>>szOriginal;
        getline(cin, szOriginal, '\n');
        szPlease = szOriginal + szPlease;
        cout<<szPlease;
        szOriginal[0] = '\0';
        szPlease[0] = '\0';
        cin.get();
    }
    Basically, what it's supposed to do is add 'please.' to the end of the phrase. However, this code always cuts out the first word.

    e.g. "Give me some toast" becomes " me some toast please."

    Assistance would be appreciated!

  2. #2
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,147
    That is basically what you've told it to do. What's the problem?

    Soma

  3. #3
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,648
    Code:
    cin>>szOriginal; // gets first word of sentence into szOriginal
    getline(cin, szOriginal, '\n'); // overwrites first word with the rest of the sentence

  4. #4
    Registered User
    Join Date
    Aug 2010
    Posts
    3
    Thank you very much! This problem is solved!

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Wow, what a way to confuse readers of your code!
    The 'sz' prefix of Hungarian notation refers to a C-style string, but you use it on std:: strings. That's one reason I stopped using Hungarian notation a long time ago.

    BTW, you also forgot to #include <string>
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  3. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM
  5. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 01:45 PM

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