Solved

Need help to fix a problem regarding cin.getline()

Posted on 2003-10-24
2
243 Views
Last Modified: 2010-04-02
I have program which takes the input from a user and append the string of characters to the output file.
The code is as follows

//SIZE is defined as 256.

case 2:
      /* Menu choice 2 will ask the user for an output
         file name and expect the user to enter a line
         of characters to be appended to the file. Of
         course, the line of character cannot exceed 256
         characters.
      */
      cout << "Enter the name of the output file.\n";
      cin >> OutputFile;
      OpenFileAppend (outFile, OutputFile);
                  
      cout << "Input the line you want to write to the file.\n"
             << "Note that the length of the line cannot exceed 256 characters.\n";

      char temp[SIZE];
      cin.getline (temp, SIZE);

      /* Write the string of characters to the output file, then
         close the file.
      */
      outFile << temp;
      outFile.close();
                        
      break;

This switch statement is inside a while loop. So after the break, the program will display the menu again.

Now, the problem is when I try to run this code, the program will display the menu right after I have entered
the output file name. It seems to somehow ignore the       cin.getline (temp, SIZE); and goes to the next line right after
it.

Here is what the output looks like.

***********************************************************
Enter your choice:
 1 to copy one text file to another.
 2 to open a text file for input, text will be appended to the file.
 3 to end this program.

Your choice is ?

2
Enter the name of the output file.
output.txt
Input the line you want to write to the file.
Note that the length of the line cannot exceed 256 characters.


Enter your choice:
 1 to copy one text file to another.
 2 to open a text file for input, text will be appended to the file.
 3 to end this program.

Your choice is ?

***********************************************************
It does not wait for me to enter the line.  I cannot find out what is wrong with my code.
So please help me debug it.

Thanks!
0
Comment
Question by:UltimateApocalypse
2 Comments
 
LVL 4

Accepted Solution

by:
dhyanesh earned 50 total points
ID: 9618886
hi

use cin.ignore(1) after you accept the file name in OutputFile.

This will ignore the '\n' character the user enters when entering the file name.

This is what is stored in temp when you try to use cin.getline()

Dhyanesh
0
 

Author Comment

by:UltimateApocalypse
ID: 9618961
Thanks for your help!!
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now