-2

Im using ofstream to get an input type string from user and put it in a file called question.txt. It doesnt show the first 4 characters in the file..I also tried without putting cout<<question and just getline(cin, question), If i do that, it doesnt let user put input at all

string question; 
cout<<"Enter Your Question: "<<endl;
cin>>question;
getline(cin, question);
cout<<question<<endl;
ofstream g("question.txt",ios::app);
g<<question<<endl;

This code above ignores the first 4 characters of the string and then prints the rest of the code.

string question; 
cout<<"Enter Your Question: "<<endl;
getline(cin, question);
cout<<question<<endl;
ofstream g("question.txt",ios::app);
g<<question<<endl;

This code above(without cin>>question) doesnt allow the user to input at all..this code is in an if else statement...when u put the same code somewhere else it works, but it doesnt work here for some reason

whole code paste bin link: https://pastebin.com/yQrzZXxJ code starts from line 83

  • Firewalls are preventing me from following the link to your code. Instead, post an [mcve]. – Thomas Matthews Jul 28 '21 at 22:12
  • What are the first 4 characters in your file? Are they ASCII, UTF-8 or UTF-16? Please edit your post with a hex dump of the first 16 bytes of the file. – Thomas Matthews Jul 28 '21 at 22:14
  • 1
    `cin >> question` means to read 1 word – M.M Jul 28 '21 at 22:14
  • just edited the question.. also with the whole code with paste bin link – Abdul Rahman Jul 28 '21 at 22:19
  • @ThomasMatthews I just edited the code...if i put input "How are you?" ,,it outputs "are you?" – Abdul Rahman Jul 28 '21 at 22:21
  • @M.M I know, but if i dont put it before getline, it doesnt allow user to input at all – Abdul Rahman Jul 28 '21 at 22:22
  • you can emulate input from a file read via a `fstream` with hardcoded `stringstream` to make it a [mcve] – 463035818_is_not_an_ai Jul 28 '21 at 22:28
  • the code works in a different program, if i just take that part of the code,, but the same code doesnt work in the project,, the code of the project has been linked – Abdul Rahman Jul 28 '21 at 22:33
  • Try entering "cheeseburger in paradise" to your program. Is it still skipping 4 characters or is it missing "cheeseburger"? – Thomas Matthews Jul 28 '21 at 22:39
  • @ThomasMatthews You are right. It is the first word which gets ignored instead of the first 4 characters.. I think because i am putting ```cin>>question;``` before ```getline(cin, question);```, but if i only put ```getline(cin,question)```, it doesnt allow me user to input at all and exits the program... – Abdul Rahman Jul 28 '21 at 22:45
  • @AbdulRahman that mean you are likely not reading in *earlier* input correctly, so there is nothing for `getline()` to read in as you are expecting. See the answer I just posted. – Remy Lebeau Jul 28 '21 at 22:46
  • @RemyLebeau I think putting ```cin>>question``` is the problem why the first word gets avoided,,, but if i dont put ```cin>> question``` , it doesnt allow me to put input at all and ends the program – Abdul Rahman Jul 28 '21 at 22:47
  • @AbdulRahman I addressed that in my answer. Please read it – Remy Lebeau Jul 28 '21 at 22:54

1 Answers1

2

In this code:

string question; 
cout<<"Enter Your Question: "<<endl;
cin>>question; // <-- WRONG
getline(cin, question);
cout<<question<<endl;
ofstream g("question.txt",ios::app);
g<<question<<endl;

cin >> question is reading in only the 1st word of the user's input into question, and then getline(cin, question) is reading in the rest of the input into question again, overwriting its current value. This is why you are losing the beginning characters of the input.

You need to get rid of the >> here, eg:

string question; 
cout<<"Enter Your Question: "<<endl;
getline(cin, question); // <-- USE BY ITSELF!
cout<<question<<endl;
ofstream g("question.txt",ios::app);
g<<question<<endl;

However, you claim you already tried this and it doesn't work at all. That means the cin stream is not in the state you are expecting to begin with, thus std::getline() is ignoring the input you want.

For instance, if you had used cin >> ... to read in an earlier input, and the user had pressed Enter after that input, then there would be a '\n' character in cin's input buffer. If you do not read in that '\n' character beforehand, std::getline() will not work in this code.

For example: let's say you prompt the user for a number, and then they press Enter. And then you prompt for the question. Thus:

cin >> number;
...
cin >> question;
getline(cin, question)

Would work as expected (just not with the result you want). cin >> number would read in the number, leaving the '\n' in the input buffer. Then cin >> question would skip the '\n' as leading whitespace, and read in the 1st word of the question. Then getline(cin, question) would read in the rest of the question up to the next '\n'.

Whereas:

cin >> number;
...
getline(cin, question)

Would not work as expected. cin >> number would read in the number, leaving the '\n' in the input buffer. Then getline(cin, question) would stop reading immediately when it reads in the '\n', leaving the whole question in the input buffer.

That would explain the behavior you are seeing.

See Why does std::getline() skip input after a formatted extraction?. To solve this, you would need to do this instead:

cin >> number;
cin.ignore(numeric_limits<streamsize>::max(), '\n'); // <-- ADD THIS!
...
getline(cin, question);

Now std::getline() will read in the whole question correctly, as expected.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770