Solved

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

Posted on 2003-10-24
2
248 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
maze travler 6 66
Cross compile release version of c++ program for linux 2 139
How to split this in C++ 4 119
passing data from one form to another form in c++ 27 89
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

730 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