String problem after space
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: String problem after space

  1. #1
    Join Date
    Feb 2010
    Posts
    14

    String problem after space

    Hello,

    In some functions of my program this code doesn`t work normally

    Code:
            string w1;
            string w2;
    
            cout<<"1st msg: Enter 2 words: ";
            getline(cin,w1);
            cout<<endl;
    
            cout<<"2nd msg: Enter 2 words: ";
            getline(cin,w2);
            cout<<endl;
    
            cout<<endl<<"w1: "<<w1<<endl;
            cout<<"w2: "<<w2<<endl<<endl;
    But when i run it is some functions(except main and some others) of my program shows other output.
    Code:
    1st msg: Enter 2 words:
    2nd msg: Enter 2 words: word1 word2
    
    
    w1:
    w2: word1 word2
    My variables are again local, any ideas whats the problem ?

  2. #2
    Join Date
    Aug 2005
    Location
    San Diego, CA
    Posts
    1,054

    Lightbulb Re: String problem after space

    Please post the entire program. There is probably some other stdin operation that you didn't post. We can only guess as to what the problem is but it sounds like it is one of those asked and answered about a million times questions. The below example is using cin but you get the same problem if you use cin for an integral value followed by getline for a string. There are certain types of input operations that leave a '\n' in the input stream which causes the next getline to read a blank line.

    http://www.parashift.com/c++-faq-lit....html#faq-15.6

  3. #3
    Join Date
    Feb 2010
    Posts
    14

    Re: String problem after space

    I send you the code because is very long to post it here.

  4. #4
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,571

    Re: String problem after space

    I don't know whether kempofighter has exposed his e-mail address on his property page, but regardless of that I think you should prefer to attach the .cpp file to a post, what you can do unless it's larger than 97.7 kB.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  5. #5
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,560

    Re: String problem after space

    It is difficult to tell without seeing your code, but you are probably
    using operator >> somewhere before the getline ... The '\n' from
    the operator >> input is still in the stream buffer.

    Example:

    Code:
    int n;
    string s;
    
    cin >> n;   // '\n' still in buffer
    getline(cin,s);  // reads the '\n'
    The simplest fix:

    Code:
    cin >> n;   // '\n' still in buffer
    cin.ingore(100,'\n');
    getline(cin,s);

  6. #6
    Join Date
    Aug 2005
    Location
    San Diego, CA
    Posts
    1,054

    Lightbulb Re: String problem after space

    The bottom line is that anytime you use operator>> you have to clear up the input stream when you are finished, otherwise you will experience unexpected behavior in other parts of the program. You should also read other parts of that FAQ on error checking. If you are properly reading from the input stream and handling errors in each and every case this problem shouldn't occur.

    You don't have to post the whole thing. Simply create a small example that demonstrates the problem. During that process you may discover the problem for yourself. If not, post the example. That is the best way to use these forums. Search forum for similar problem, duplicate with small example, and then post example plus question.

  7. #7
    Join Date
    Feb 2002
    Posts
    4,640

    Re: String problem after space

    Quote Originally Posted by 1024mbyte View Post
    Hello,

    In some functions of my program this code doesn`t work normally

    Code:
            string w1;
            string w2;
    
            cout<<"1st msg: Enter 2 words: ";
            getline(cin,w1);
            cout<<endl;
    
            cout<<"2nd msg: Enter 2 words: ";
            getline(cin,w2);
            cout<<endl;
    
            cout<<endl<<"w1: "<<w1<<endl;
            cout<<"w2: "<<w2<<endl<<endl;
    But when i run it is some functions(except main and some others) of my program shows other output.
    Code:
    1st msg: Enter 2 words:
    2nd msg: Enter 2 words: word1 word2
    
    
    w1:
    w2: word1 word2
    My variables are again local, any ideas whats the problem ?
    What is the problem? It looks like you just hit {ENTER} at the first prompt, and "word1 word2{ENTER}" at the second prompt. The output confirms that this is what happened.

    Viggy

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center