-1

I've been having issues in a program involving cin.

My problem is that the first word of everything I input appears to be skipped, possibly because of the way the buffer is handled. I have seen similar posts regarding this but trying to apply their fixes to my code have so far failed. What is supposed to happen is the user inputs a name and that name gets stored in a text file with other entered data. However, it always drops the first word.

#include "string"
#include "stdafx.h"

string _name;    

int main()
{
    cout << "Choose a name" << endl;

    getline(cin, _name);
    cout << _name;

    ofstream dat;
    dat.open("data.txt");
    dat << _name;
    dat.close();
        return 0;
    }

This code is where the problem appears to be. I just can't get it to take the first word.

2 Answers2

1

If you want to read a name from cin, then your code should look something like this:

#include <iostream>
#include <string>
using namespace std;

int main()
{
    string _name;
    cout << "Choose a name : ";

    getline(cin, _name);
    cout << _name << endl;

    // Do something with _name - write to file etc..
    // ..
}
Vinod
  • 356
  • 3
  • 6
  • I think you are getting a problem using cin and getline() one after the other. cin>> leaves the newline character in the iostream, and if getline is used after cin>>, the extra newline causes the first word to be missed. Try adding a dummy getline() after cin and before the real getline(). – Vinod Aug 14 '16 at 02:18
  • I will try that but its strange I did a copy paste of your code to see if it would work and it did but after putting it in with the rest of my program, the program just skips the getline and returns 0. – cooperg2001 Aug 14 '16 at 02:21
  • cin/getline() issue is also discussed here :http://stackoverflow.com/questions/5739937/using-getlinecin-s-after-cin?rq=1 – Vinod Aug 14 '16 at 02:28
  • Okay Ill look and see if that answers my question, but as of now I'm putting it down to a bug in my VS install cause copy pasting the same code has broken my program – cooperg2001 Aug 14 '16 at 02:32
0
cin >> _name;

This reads the first word on the first line of input into _name.

getline(cin, _name);

This will read the rest of the line into _name. This overwrites the existing contents of name.

Because this overwrites the existing contents of _name, which contains the first word read, this ends up reading all except the first word of the line, as you described.

If you just want to read the entire line into _name, the only thing that needs to be done is to remove the cin >> _name.

Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148
  • I've tried removing cin >> _name doing so causes nothing to be inputted. – cooperg2001 Aug 13 '16 at 02:14
  • Works for me. Define "nothing to be inputted". – Sam Varshavchik Aug 13 '16 at 02:18
  • I mean that none of what I type is read into _name but I just fixed that and it just drops the first word as before – cooperg2001 Aug 13 '16 at 02:23
  • How do you know that "it just drops the first word as before"? There's nothing in the shown code, in the question, that shows the contents of `_name`. If it did, it would've showed that it works as expected. We're sorry, but all our psychics are out to lunch now, and there's nobody left in the office that knows how to operate the magical mind ray-beam machine that's needed to beam into your head and see the actual code you're running, instead of the fantasy code that's shown in the question, which works just fine. – Sam Varshavchik Aug 13 '16 at 02:37
  • The code in my question is the entirety of the code that is the issue not " fantasy code " and the way I can tell is ive already written the code that takes _name and puts it in a text file. But as I know that the code for the text file is fine I did not include it – cooperg2001 Aug 13 '16 at 02:40
  • This question cannot be salvaged. It will need to be closed as off-topic due to a lack of a [mcve]. – Sam Varshavchik Aug 13 '16 at 02:41