Solved

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

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MSVCR80.dll crash 2 141
Getting IP address 8 89
Using Diagnostic tools in VS2015: Unresoved allocations 19 106
sorting efficency of sorting algorithm 30 121
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

827 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