Having trouble with streams and strings

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • massdeletion101
    New Member
    • Mar 2007
    • 23

    Having trouble with streams and strings

    I'm trying to write a string I entered into the program to a file. I won't explain it though, it's probably easier if I just post the code:

    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    int main()
    {
    string input;
    cin >> input;
    fstream myupdate ("G:\\Documents \\C++\\myfile.t xt", ios:: out | ios::app);
    myupdate << update << endl << endl << endl;
    myupdate.close( );
    return 0;
    }

    Problem is, if I type in a string, the first space I hit is where it ends. For instance, if I type the words in the program window "Work you piece of crap!!!" it would just write "Work" to the file and that's it. Apparently that's all that gets written to the string 'input' and so that's all that gets written. Anyone know how to get everything I type in the program window including spaces put into the variable?
  • sicarie
    Recognized Expert Specialist
    • Nov 2006
    • 4677

    #2
    Yeah, that's actually the way cin is supposed to behave (check out Ganon11's tutorials at the top of this forum), it stops at the first whitespace. I think you would be more happy with the getline() function. Try googling that, and see if its what you're looking for.

    Comment

    • massdeletion101
      New Member
      • Mar 2007
      • 23

      #3
      Originally posted by sicarie
      Yeah, that's actually the way cin is supposed to behave (check out Ganon11's tutorials at the top of this forum), it stops at the first whitespace. I think you would be more happy with the getline() function. Try googling that, and see if its what you're looking for.
      Okay, thanks a a ton!:)

      Comment

      Working...