Solved

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

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

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

809 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