   Help with a file i/o question
  Hey, I am writing a small program that saves the text the user enters and then displays it when they choose the load option next time they run the program. My problem is this:ofstream myOutFileStream("save1.txt");myOutFileStream << "I like cheese";myOutFileStream.close();ifstream myInFileStream("save1.txt");string save1((istreambuf_iterator<char>(myInFileStream)), istreambuf_iterator<char>());cout << save1;myInFileStream.close();That works fine, load the whole "I like cheese" thing no problem. However when I went to make my user version I found I could only get it to open the first word of the string entered. When I tried using cin.getline() it would not even let me enter the text. Here is my current code:#include <iostream>#include <string>#include <fstream>using std::cin;using std::cout;using std::string;using std::ofstream;using std::ifstream;using std::istreambuf_iterator;int main(){int choice;cout << "Hello user! I was told to greet you in a nice and polite way! Lets be friend?\n";cout << "So, do you want to create a new file or load the last one?\n";cout << "1)new 2)load: ";cin >> choice;if (choice == 1){    char text[50];    cout << "Enter the text: ";    cin >> text;    ofstream myOutFileStream("save1.txt");    myOutFileStream << text;    myOutFileStream.close();    }if (choice == 2){    ifstream myInFileStream("save1.txt");    string save1((istreambuf_iterator<char>(myInFileStream)), istreambuf_iterator<char>());    cout << save1;    myInFileStream.close();    }}using cin.getline(text,50); did not work as I said. Kind of at a loss. I want the user to be able to type a sentence, save that sentence to a file, then load it and print it.I am pretty sure the problem is on the line "cin >> text" and I know that cin stops at a space (" "), so I know that it is only getting the first word. When I Google'ed getline and such I found http://www.cplusplus.com/reference/iostream/istream/getline/ but for some reason it won't work for me. Any ideas?   
  Use std::getline instead of istream::getline instead.  Also, it could be that the newline is left in the input buffer when the user enters a number (i.e. "1\n"); try clearing the input -- cin.ignore(numeric_limits<streamsize>::max(),'\n');  -- before asking the user for the line of text.   ;Quote:Original post by _fastcallUse std::getline instead of istream::getline instead.  Also, it could be that the newline is left in the input buffer when the user enters a number (i.e. "1\n"); try clearing the input -- cin.ignore(numeric_limits<streamsize>::max(),'\n');  -- before asking the user for the line of text.I tried using cin.ignore(numeric_limits<streamsize>::max(),'\n'); but it says numeric_limits not delcared in scope.The problem when using getline is it does not even let me enter text. I will try the std::getline though.   ;  my code is:#include <iostream>#include <string>#include <fstream>using std::cin;using std::cout;using std::string;using std::ofstream;using std::ifstream;using std::istreambuf_iterator;using std::getline;int main(){int choice;cout << "Hello user! I was told to greet you in a nice and polite way! Lets be friend?\n";cout << "So, do you want to create a new file or load the last one?\n";cout << "1)new 2)load: ";cin >> choice;if (choice == 1){    string text;    cout << "Enter the text: ";    getline (cin,text);    ofstream myOutFileStream("save1.txt");    myOutFileStream << text;    myOutFileStream.close();    }if (choice == 2){    ifstream myInFileStream("save1.txt");    string save1((istreambuf_iterator<char>(myInFileStream)), istreambuf_iterator<char>());    cout << save1;    myInFileStream.close();    }}It compiles and runs, but it will not even let me enter the string. I feel like I am missing something obvious/doing something dumb.. but I don't see it.Edit: As a side note, if I enter text in the file before hand it reads it fine, even if there are multiple words...   ;Quote:Original post by Mr CI tried using cin.ignore(numeric_limits<streamsize>::max(),'\n'); but it says numeric_limits not delcared in scope.Oops, I should have mentioned that you need to include <limits> in order to use std::numeric_limits.EDIT:Yeah, you need to clear out the extra input left when the user enters the integer before asking for a line of text.  (Entering "1Hello world!" works as expected; the integer is read, then the remainder is read by getline, and saved to the file.)   ;Quote:Original post by _fastcallQuote:Original post by Mr CI tried using cin.ignore(numeric_limits<streamsize>::max(),'\n'); but it says numeric_limits not delcared in scope.Oops, I should have mentioned that you need to include <limits> in order to use std::numeric_limits.EDIT:Yeah, you need to clear out the extra input left when the user enters the integer before asking for a line of text.  (Entering "1Hello world!" works as expected; the integer is read, then the remainder is read by getline, and saved to the file.)Well, I tried doing #include <limits> but that was a no go. main.cpp|26|error: `numeric_limits' was not declared in this scope| as well as streamsize and max having the same issue... At this point my issue is I can't even enter a string... before I had it so I could enter "The brown dog jumped" but only "The" would be saved...   ;  I had originally assumed you had "using namespace std", and as you posted, my assumption is incorrect.  Try this:std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );(Or alternately:  using std::numeric_limits; using std::streamsize)   ;Quote:Original post by _fastcallI had originally assumed you had "using namespace std", and as you posted, my assumption is incorrect.  Try this:std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );(Or alternately:  using std::numeric_limits; using std::streamsize)Jesus Fish, I love you.   