issues with getline()

Hey all,

Im having issues with using getline() to write a sentence into a file. It is printing out the entire sentence but not the first word for some reason.

1
2
3
4
5
6
7
8
9
void write(string input)
{
	ofstream biology_flash;
	biology_flash.open("C:/Users/Jack/Desktop/biology_flash_cards.txt");	//sort folder is created and opened.
	cin >> input;
	getline(cin, input);
	biology_flash << input << endl;
	biology_flash.close();
}


OUTPUT:
love pie
wont this work!!
Don't combine

cin >>

and getline() in the same code. It gets... wonky. Also, your line 5 and 6 do the exact same thing, hence why you probably lose the first line.
If I take out the cin>> it doesn't allow me to enter anything at all =/
Have you used the extraction operator somewhere previous to this function? If so you have you tried to ignore() what is in the buffer?

Why is this function named write when you're getting information from the user (reading). You should probably try to separate the input from the output.

The goal of this program is to have the user enter a sentence and save that sentence into a file. This is the whole code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include<iostream>
#include<string>
#include<fstream>

using namespace std;

void write(string input);

int main()
{
	string input;
	int user;

	cout << "Enter a number" << endl
		<< "1.) Enter questions" << endl
		<< "2.) Enter anwsers" << endl
		<< "3.) Quit program" << endl;
	cin >> user;
	switch (user)//user selects
	{
	case 1:
		write(input);
		//cout.setf(ios::fixed);
		//cout.setf(ios::showpoint);
		//cout.precision(2);
		break;
	}

	return 0;
}

void write(string input)
{
	ofstream biology_flash;
	biology_flash.open("C:/Users/Jack/Desktop/biology_flash_cards.txt");	//sort folder is created and opened.
	cout << "Enter a question: " << endl;
	getline(cin, input);
	biology_flash << input << endl;
	biology_flash.close();
}


As this code is now it wont let me input any text, phrase, curse word, ect.
Last edited on
Since you used the extraction operator with the cin stream there is a end of line character that you need to ignore(). Any time you switch between using the extraction operator and getline() you need to make sure the end of line character is removed from the stream before you switch to getline().

There is a pinned topic "Console Closing Down" that you may want to read, since the first or second post shows you how to ignore() things left in the input buffer.

Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <fstream>
#include <iomanip>
#include <iostream>
#include <string>

using namespace std;

void get_and_store_question();

int main()
{
    int user;

    cout << "Enter a number" << endl
        << "1.) Enter questions" << endl
        << "2.) Enter anwsers" << endl
        << "3.) Quit program" << endl;
    cin >> user;
    switch (user)//user selects
    {
    case 1:
        get_and_store_question();
        break;
    }
}

void get_and_store_question()
{
    // you don't give input a value before you call the function, so why does
    // the function need it as an argument?

    const char* fname = "C:/Users/Jack/Desktop/biology_flash_cards.txt";

    std::ofstream out(fname, std::ios_base::app);
    if (!out)
    {
        std::cout << "Unable to open file \"" << fname << "\"\n";
        return;
    }

    std::cout << "Enter a question:\n> ";

    std::string question;
    std::getline(std::cin >> std::ws, question);
    // http://en.cppreference.com/w/cpp/io/manip/ws

    out << question << '\n';

    // out is automagically closed here
}
Last edited on
Ya that was the issue. The cin needed to be flushed. Im going to have a lot of reading ahead of me lol. Thanks for the good info!
Topic archived. No new replies allowed.