getline with opening and saving txt files

I have been experimenting with opening previously saved txt files to the console, and also saving console input to a text file at the users desired location. This is working fine but I encountered a getline problem when using it twice within the code. I assume its down to different input types...

Code:

#include <iostream>
#include <fstream>
#include <string>

using std::cout;
using std::cin;
using std::endl;
using std::string;
using std::getline;
using std::ofstream;
using std::ifstream;

int main () 
{
	string text, location, iName, file;
	int makeOpen;

	cout << "1. Create a text file. \n2. Open a text file.";
	cout << endl << endl << "Selection: ";
	cin >> makeOpen;
	cout << endl << endl;

	if (makeOpen == 1)
	{
		cout << " - i.e. This is a save file test!\n";
		cout << "Enter text for the save file: ";
		getline(cin, text);

		cout << endl << " - i.e. C:\\Users\\Admin\\Desktop\n";
		cout << "Enter file save location: ";
		cin >> location;

		cout << endl << " - i.e. saveTest\n";
		cout << "Enter file name: ";
		cin >> iName;

		file = location + "\\" + iName + ".txt";

		ofstream myfile;
		myfile.open (file);

		cout << endl;

		if(myfile.good())
		{
			cout << "Aces!";
		}
		else
		{
			cout << "Boo!"; 
			return 1;
		}

		myfile << text;
		myfile.close();
	}

	else if(makeOpen == 2)
	{
		cout << " - i.e. C:\\Users\\Admin\\Desktop\\openTest.txt\n";
		cout << "Enter file location and file name: ";
		cin >> file;
		cout << endl;

		ifstream myfile (file);

		if (myfile.is_open())
		{
			while ( myfile.good() )
			{
				getline (myfile, text);
				cout << text << endl;
			}

			myfile.close();
		}

		else 
		{
			cout << "Unable to open file."; 
			return 1;
		}
	}

	else
	{
		cout << "Boo!";
		return -1;
	}

	cout << endl << "Enter to exit...";
	cin.ignore(2);
	return 0;
}


The error occurs on this line of code:
getline(cin, text);


The error states:

- IntelliSense: no instance of overloaded function "getline" matches the argument list
- IntelliSense: too few arguments in function call

Any help would be apprieciated. Thanks

I'm not getting any errors with your code.

But, this is a problem:
1
2
3
4
5
while ( myfile.good() )
{
  getline (myfile, text);
  cout << text << endl;
}


getline might not be successful, but you still output what the result is. Better to have the succes of getline be your condition:
1
2
while (getline(myfile, text))
  cout << text << endl;


Also, remove the various returns in the middle of the code that you have. They make it so that you can't see the error reports (assuming you are running this through an IDE).
Last edited on
Ahhh, thanks for the feedback.

The program compiled and ran- even with errors. If I choose option 1 (create a new text file) it seems to skip the getline and move straight to the next cout and cin without taking input for the input text.

However, I have reopened the project and the errors have gone, but the problem of skipping the getline still occurs.

Thanks for reply- I was assuming the myfile.good() as a condition for the while was okay as I thought it meant the file was opened correctly without error; then the code within the while would be fine to execute. I'll read into the good function a little more.
I understand why the while loop is incorrect now. Thanks :)

- but the getline is still playing up
I'm on my phone at the minute, so Google cin.ignore().

Hopefully that will help! (:

Edit:
Or just change 'makeOpen' a string too, and use getline to take input for your choice.
Last edited on
Lynx thats brilliant. I've never looked into cin.ignore() in great detail- I was only using it to pause the program before exiting (should be using system("pause"); I guess). However I researched and actually came across a similar problem to mine on the forum which was resolved via the ignore function.

http://www.cplusplus.com/forum/beginner/39549/

darkestfright:
I didn't compile or run your code, but I can tell just from looking that it is because you are mixing 'cin' with 'getline'. std::cin leaves the newline character in the buffer after pressing enter, and getline just grabs it and keeps going, that's why getline doesn't block to wait for input.


Followed by...

Zhuge:
cin>> will leave newlines in the buffer when the user presses enter. getline() reads this as the user having pressed enter to "skip" the input.

You can use cin.ignore() to get rid of those extra characters before using getline().


Thanks for your help guys. Great, rapid responses to my first post.

Edit: Everything is working as excepted. Thanks
Last edited on
No problem. I came across this problem ages ago. As of then, I never use cin anymore, ahaha, just getline. (:
Topic archived. No new replies allowed.