getline() Problem

I have created a demo file I/O program for school that takes in a string and writes it to files in various ways. However, it doesn't properly take in the string (in fact, it completely skips over the taking-in part altogether). Can anyone tell me what is wrong? I'd like to send the source code and associated text files, so e-mail me if you can to get them from me. It will be easier to help that way.

Thank you,
Sean

yokokingdommusic@sbcglobal.net
I tried that, but it won't work. Here's how my program works:



void main()
{
int iOPTION;

fstream io1, io2, io3;

cout << "/\\/\\/\\/\\/\\/\\/\\ Welcome to the File Writer! /\\/\\/\\/\\/\\/\\/\\\n\n";

cout << "\t1) Run the program normally\n";
cout << "\t2) Clean out files\n\n";
cout << "Which option would you like? ---> ";
cin >> iOPTION;

if (iOPTION==1)
{
WRITE();
}
else if (iOPTION==2)
{
ERASE();
}
}

void WRITE()
{
string sIN_PHRASE;

fstream io1, io2, io3;

cout << "Please enter a word to write to the files ---> ";
getline (cin, sIN_PHRASE);

io1.open("io1.txt");
io1 << sIN_PHRASE;
io1.close();
system("\"C:\\Users\\Sean\\Documents\\Visual Studio 2008\\Projects\\FileIOTest\\FileIOTest\\io1.txt\"");

io2.open("io2.txt", ios::app); //This opens the file and asociates it with the variable 'io2'. Changes will be ADDED TO THE END of the file.
io2 << sIN_PHRASE;
io2.close(); //Closes the file so that the system can open it to show the user.
system("\"C:\\Users\\Sean\\Documents\\Visual Studio 2008\\Projects\\FileIOTest\\FileIOTest\\io2.txt\"");

io3.open("io3.txt", ios::out);
io3 << sIN_PHRASE;
io3.close();
system("\"C:\\Users\\Sean\\Documents\\Visual Studio 2008\\Projects\\FileIOTest\\FileIOTest\\io3.txt\"");
}

void ERASE()
{
fstream io1, io2, io3;

io1.open("io1.txt", ios::out); //This opens the file and associates it with the variable 'io3'. Changes will OVERWRITE THE ENTIRE FILE.
io1.close();
system("\"C:\\Users\\Sean\\Documents\\Visual Studio 2008\\Projects\\FileIOTest\\FileIOTest\\io1.txt\""); //Calls the system to open the file.

io2.open("io2.txt", ios::out); //This opens the file and associates it with the variable 'io3'. Changes will OVERWRITE THE ENTIRE FILE.
io2.close();
system("\"C:\\Users\\Sean\\Documents\\Visual Studio 2008\\Projects\\FileIOTest\\FileIOTest\\io2.txt\""); //Calls the system to open the file.

io3.open("io3.txt", ios::out); //This opens the file and associates it with the variable 'io3'. Changes will OVERWRITE THE ENTIRE FILE.
io3.close();
system("\"C:\\Users\\Sean\\Documents\\Visual Studio 2008\\Projects\\FileIOTest\\FileIOTest\\io3.txt\""); //Calls the system to open the file.
}
Re-Read that link I sent you.

Re:
1
2
cout << "Which option would you like? ---> ";
cin >> iOPTION;


The cin >> doesn't clear the newline character from the buffer. So when you try to use getline() (which is a more correct solution). The newline in the stream immediately submits it. A classic problem with cin >>. Change it to getline.
1
2
3
4
5
6
7
8
9
10
11
12
13
// How to get a number.
 int myNumber = 0;

 while (true) {
   cout << "Please enter a valid number: ";
   getline(cin, input);

   stringstream myStream(input);
   if (myStream >> myNumber)
     break;
   cout << "Invalid number, please try again" << endl;
 }
 cout << "You entered: " << myNumber << endl << endl;

Besides the while statement, what is the difference between that and mine below? it is not allowing me to do this (giving me error C2784 and C2780).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
cout << "/\\/\\/\\/\\/\\/\\/\\ Welcome to the File Writer! /\\/\\/\\/\\/\\/\\/\\\n\n";

cout << "\t1) Run the program normally\n";
cout << "\t2) Clean out files\n\n";
cout << "Which option would you like? ---> ";
getline (cin, iOPTION);

if (iOPTION==1)
{
	WRITE();
}
else if (iOPTION==2)
{
	ERASE();
}
getLine() can't load directly into an integer. That's why you see the stringstream object that converts the input into myNumber.
Ok. I can accept that :P However, I don't understand what is going on with the stringstream. I'm trying to replicate it, but it is not working.
Well, I got it to work. I didn't add the <sstream> header. Now that I have that in, it is working great. But I still don't understand why it works :(
Think of input as a queue of characters.

After each line of input the user presses the Enter key --> which leaves a newline character '\n' in the input queue.

Getting input with cin >> whatever; doesn't read that newline.
Getting input with getline( cin, a_string ); does read the newline.

Mixing the way you get input causes problems.

Hope this helps.
I get that, but I don't understand what stringstream is doing.
Topic archived. No new replies allowed.