Question

getline in visual c++ 6

Asked by: apolo63

im trying to use getline to get a line of input but on execution the getline code is skiped over???

this is the code...

my includes...
#include <iostream>
#include <string>
#include <conio.h>
#include "List.hpp"

using namespace std;

char cTemp[50];
int nLim=50;
cout<<"Please enter a string... \n";
cin.getline(cTemp, nLim, '\n');

I copied the same code into borland and it worked fine.

any help would be cool...

Thanks all,
Apolo.

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2003-02-28 at 16:05:24ID20534281
Tags

getline

,

visual

,

c

,

6

Topics

C++ Programming Language

,

Microsoft Visual C++

Participating Experts
7
Points
100
Comments
20

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. getline()
    hi, I could run the following code on UNIX but it gives error when I compile it on VC++6.0. 4 errors at getline() function call. Please tell me why it happens. Thank you in advance. /*** Funny program. */ #include <iostream.h> #include <string> int main(){ ...
  2. getline(cin, s);
    This code when compiled in VC++ 6.0 shows a bug in VC++ 6.0, the bug here is that you have to press <enter> twice to enter something with the keyboard, the same code functions correctly with borland c++ builder 5.0. Any idea if this is solved by microsoft? I find search...
  3. Getline is getting two lines!
    //////////////////////////////// // Compile option needed: /GX //////////////////////////////// #include <string> #include <iostream> using namespace std ; void main(){ string s1; while(s1 != "exit"){ cout << "Enter a sentence:\...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: thienpnguyenPosted on 2003-02-28 at 16:10:02ID: 8045527

 

by: apolo63Posted on 2003-02-28 at 16:46:22ID: 8045649

I got it, there was probably something in the input buffer cause i put the line

cin.get(cKillBuf);

right before that line and it started working...

my question now is...

Is there a command i can use to clear the stdin buffer?

 

by: apolo63Posted on 2003-02-28 at 17:02:15ID: 8045711

I got it, there was probably something in the input buffer cause i put the line

cin.get(cKillBuf);

right before that line and it started working...

my question now is...

Is there a command i can use to clear the stdin buffer?

 

by: michaelessaryPosted on 2003-02-28 at 19:16:49ID: 8046119

im kind of unclear as to what you mean, but i thinkyou might be talking about something like:
cin.flush()

 

by: cupPosted on 2003-02-28 at 23:58:05ID: 8046854

Instead of

cout<<"Please enter a string... \n";

Use

cout <<"Please enter a string..." << endl;

endl does a newline and a flush.

 

by: nietodPosted on 2003-03-01 at 04:26:29ID: 8047427

There is a known bug in the VC version of getline().   You can fix it by changing the code for getline in the STL.

If you open up the string #include file, you'll find this statement:
 
    else if (_Tr::eq(_C, _D))
        {_Chg = true;
        _I.rdbuf()->snextc();    
        break; }
 
If you replace the line:
_I.rdbuf()->snextc();    
 
With:
_I.rdbuf()->sbumpc();  

then recompile and getline() will work fine.

 

by: chimaPosted on 2003-03-01 at 10:04:12ID: 8048336

Apolo63,
Cup is correct on his/her comments.  I had the same problem.  What happens is that after the newline, flush, getline see that newline as its input.  If you change '\n' to some other delimiter, like '\'', then getline will accept your input after it see the single quote.  In my code I would of preferred using '\n', so I think I'll go try applying nietod's suggestion.  Using '\n' would be much better.

 

by: LamLapTakPosted on 2003-03-01 at 10:16:26ID: 8048376

Apolo63,
 I have tried the following, it works.



#include <iostream.h>  //your problem maybe here, with '.h'
#include <string.h>

void main()
{
     char cTemp[50];
     int nLim=50;
     cout<<"Please enter a string... \n";
     cin.getline(cTemp, nLim, '\n');
     cout<<cTemp;
}

 

by: nietodPosted on 2003-03-01 at 15:25:10ID: 8049494

>>  What happens is that after the newline, flush, getline see that newline as its input.
No.  The input and output streams are 100% independant of each other!  think about it.  standard output might be to a line printer and standard input might be a file.  How would informatiojn typed to a peice of paper be read in from a file.

>> #include <iostream.h>  //your problem maybe here, with '.h'
No.  that is your problem, not apolo63's.   apolo63 is using <iostream>, which is the standard STL stream include file.   <iostream.h> is at best a tirvial mistake, but more likely a very dangerious mistake.  There is no standard file with this name.  The contents of that file are unpredictable at best and certainly unportable.

 

by: cupPosted on 2003-03-02 at 02:42:13ID: 8051293

nietod's solution will work if you use std::getline with string instead of char[] i.e.

string cTemp;
getline(cin,str,'\n');

It fixes the problem that you'd get having to input the first line twice.  This is a problem that you sometimes get with MSVC.

 

by: nietodPosted on 2003-03-02 at 03:56:20ID: 8051431

AFAIK the fix I posted should fix the global getline AND  the member getline() function.   (The global one is usually a far better one to use, but it should fix both.)  

both version of VC's getline()'s suffer from the same problem.  input streams usually ned to look ahead 1 character.  and buffer this "peeked at" charcter.   VC's problem is that at the end of a line it tries to peek ahead at the next line.  But because of line buffering, it can't obtain the 1st character of the next line until the entire line is entered by the user.  Thus when it tries to peek ahea 1 character, it forces the user to enter an entire line.  This code fixes this problem.

 

by: cupPosted on 2003-03-02 at 09:35:41ID: 8052368

nietod, The member getline doesn't have the same problem as the std::getline because it doesn't use strings.  I used to use the member getline as a fix for the global getline problem.  Your fix works beautifully on std::getline.

 

by: nietodPosted on 2003-03-02 at 15:01:49ID: 8053812

>> nietod, The member getline doesn't have the same problem as the
>> std::getline because it doesn't use strings.
the use of STL strings shoudln't matter one way or the other.  The problem is produced when the code tries to peek ahead a character at the end of the line, and that can happen no matter what.  (It could happen when trying to read an int, however it doesn't.)  apolo63 is reporting the problem for istream::getline(), not the global one, so I assume that this problem does occur for both forms of getline().  Its possible that its only for the global getline(), and the member version is experiencing a different one, but I sort of doubt it.

>> I used to use the member getline as a fix for the global getline problem
Did you use soem character other than \n to mark the end of the line?  Even without the fix, the global one worked fine if you terminate the line with some other character.

 

by: cupPosted on 2003-03-02 at 22:17:33ID: 8055315

Without the fix, you will need to input the first line twice for

string cTemp;
getline(cin,str,'\n');

However, with

cin.getline(cTemp, nLim, '\n');

You only need to input once.  Try reversing the fix and you'll see what I mean.


 

by: bcladdPosted on 2003-12-06 at 04:30:06ID: 9887941

No comment has been added lately, so it's time to clean up this TA. I will
leave a recommendation in the Cleanup topic area that this question is:

Answered: Points to cup: Grade A

Please leave any comments here within the next seven days.

Experts: Silence means you don't care. Grading recommendations are made in light
of the posted grading guidlines (http://www.experts-exchange.com/help.jsp#hi73).

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

-bcl (bcladd)
EE Cleanup Volunteer

 

by: nietodPosted on 2003-12-06 at 07:00:29ID: 9888348

cups answer was not correct.  We suggested usign endl because it flushes the output stream.  1st of all, that is not true.  2nd of all, even if it were it wouldn't explain the behavior.  

The problem is a known bug in the libraries shipped with VC 6.

 

by: bcladdPosted on 2003-12-06 at 07:18:55ID: 9888391

Hate to argue but endl starts a new line and flushes the output stream (http://www.roguewave.com/support/docs/sourcepro/stdlibref/cout.html for starters).

I accept that you pointed out a known bug in the getline implementation but your bug was related to ::getline(istream &, string, char), not istream::getline(char *, int, char) (per http://support.microsoft.com/default.aspx?scid=kb;en-us;240015) and the original poster's code used the latter, not the former.

Since I don't have VC++6.0 installed I cannot test the original poster's code and from his own follow up it is possible that it had nothing to do with the code shown (as shown it would not be possible for anything to be in the input buffer unless there is a previous use of operator>> from cin).

Having explained my answer I will leave it to the moderator who comes through in a week to decide whether this is better deleted, split, or whatever.

-bcl

 

by: nietodPosted on 2003-12-06 at 12:04:42ID: 9889306

You're right about endl calling flush.  I just checked the standard.

But the question still remains...so what?  That would have no effect on getline().  

On the other hand, if there is a bug in VC's getline(), as is reported by dincumware, the author of the library at

http://www.dinkumware.com/vc_fixes.html

that would explain the problem.  It would also explain why the code in question worked find borland and developed the problem under VC.

 

by: bcladdPosted on 2003-12-06 at 12:59:26ID: 9889481

Actually the bug you report has NOTHING to do with the code in question. It is in a different function that has the same name.

It is possible the cup's answer had little to do with the answer, too, now that I reread the original poster's "solution" posting (where he/she solved the problem). Maybe I should just recommend that this question be deleted w/o refund since OP solved it but didn't post the solution.

-bcl

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...