[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

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

Posted on 2003-10-24
2
Medium Priority
?
271 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 4

Accepted Solution

by:
dhyanesh earned 200 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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 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…
Suggested Courses

649 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