Problem With std::getline And C++

kuphryn

Senior member
Jan 7, 2001
400
0
0
Hi,

I find there is a slight inconvenience with the std::getline function in C++. Here is an example:

-----------------------------------------------------------
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
using std::getline;

#include <string>
using std::string;

int main()
{
string strTemp;

cout << "Enter a sentence: ";
getline(cin, strTemp);

cout << "\nYou entered: "
<< strTemp << endl;

return 0;
}

-----------------------------------------------------

If you run the code above, and hopefully I did not leave anything out, out will find that after you entered a sentence and press Enter, the "cursor" will go to the next line, but the program does not move onto "You have entered: " until you press Enter the second time. In other words, I have had problems with the getline function in term of having to press Enter twice to get it to move onto the next algorithm.

I have tried for example:

getline(cin, strTemp, '\n');

It has the same effect.

Is there a way to get around having to press Enter twice?

Thanks,
Kuphryn
 

Elledan

Banned
Jul 24, 2000
8,880
0
0
Replace
---------------------------------------
cout << "Enter a sentence: ";
getline(cin, strTemp);

cout << "\nYou entered: "
<< strTemp << endl;
---------------------------------------

With

---------------------------------------
cout << "Enter a sentence: \n";
getline(cin, strTemp);

cout << "\nYou entered: " << strTemp << endl;
---------------------------------------
 

kuphryn

Senior member
Jan 7, 2001
400
0
0
Thanks.

So the only input change is this line:

cout << "Enter a sentence: \n";

You added "\n" right?

How does that effect the code in general?

Kuphryn
 

Elledan

Banned
Jul 24, 2000
8,880
0
0


<< Thanks.

So the only input change is this line:

cout << "Enter a sentence: \n";

You added "\n" right?

How does that effect the code in general?

Kuphryn
>>

It should solve your problem, although I didn't really see anything wrong your code.

Tell me, which compiler do you use?
 

Elledan

Banned
Jul 24, 2000
8,880
0
0


<< I am using VC++ 6 under XP.

Someone from cprogramming.com posted something about a bug in VC++:

http://www.cprogramming.com/cboard/...7e8f0dc4d9e68fb761087c&postid=48172#post48172

Maybe that is causing the problem.

Kuphryn
>>


From what I've seen, VC++ can possibly be called the most buggy compiler in existance. It's the least ANSI-compliant of all compilers.

Personally, I prefer Borland and gcc.

BTW, you can download the Borland (5.5) compiler for free from community.borland.com. Perhaps you'll have better luck with that compiler :)
 

Elledan

Banned
Jul 24, 2000
8,880
0
0


<< Thanks.

Yes, I agree that VC++ 6 is buggy. I am waiting for VC++ 7.

Kuphryn
>>

Why? There are many compilers 'out there' which are much better than VC++ can ever hope to become, yet are free or quite cheap.
 

kuphryn

Senior member
Jan 7, 2001
400
0
0
Interesting.

I posted a topic on the best compiler for windows a few months ago. Most people recommended VC++ 6 over Borland C++ Builder 5.5.

I think gnu C++ is the best overall, but it is a linux C++ compiler.

Kuphryn
 

Elledan

Banned
Jul 24, 2000
8,880
0
0


<< Interesting.

I posted a topic on the best compiler for windows a few months ago. Most people recommended VC++ 6 over Borland C++ Builder 5.5.
>>


And you trust the people who commented in that thread? How about the opinion of professional programmers and other experts?

It's a well-known fact that if you're looking for an ANSI-compliant compiler, VC++ would be the last compiler to consider.



<< I think gnu C++ is the best overall, but it is a linux C++ compiler.

Kuphryn
>>

gcc rules and so does Linux :)
 

kuphryn

Senior member
Jan 7, 2001
400
0
0
You made a good point. I know VC++ is not fully compliant, but I am using it anyways for some reason it is the preferred win32 compiler at school and workplace.

Why are people using VC++ 6 instead of Borland C++ builder 5.5 if VC++ is that bad?

Kuphryn
 

Elledan

Banned
Jul 24, 2000
8,880
0
0


<< You made a good point. I know VC++ is not fully compliant, but I am using it anyways for some reason it is the preferred win32 compiler at school and workplace.

Why are people using VC++ 6 instead of Borland C++ builder 5.5 if VC++ is that bad?

Kuphryn
>>


Why is Windows installed on 95% of all desktop PC's even though it's technically speaking inferior to just about any other major OS?

Marketing.
 

BFG10K

Lifer
Aug 14, 2000
22,709
3,000
126
Why? There are many compilers 'out there' which are much better than VC++ can ever hope to become, yet are free or quite cheap.

Such as? And no, GCC is a pile of crap. Visual C++ absolutely owns GCC.

Why is Windows installed on 95% of all desktop PC's even though it's technically speaking inferior to just about any other major OS?

<rolleyes>
 

Elledan

Banned
Jul 24, 2000
8,880
0
0


<< Why? There are many compilers 'out there' which are much better than VC++ can ever hope to become, yet are free or quite cheap.

Such as? And no, GCC is a pile of crap. Visual C++ absolutely owns GCC.
>>


gcc is more ANSI-compliant than VC++, in case you didn't know yet.

Another good compiler is Mingw.
 

MustPost

Golden Member
May 30, 2001
1,923
0
0
VC++ is an exellent compiler, i usually use GCC instead, but on Windows I use VC++.

But it has a bug with the getline functions.
When I tried to learn how to use getline, I searched google and like half the entrys were about the VC++ bug.

Search google and you'll find a solution.
 

kuphryn

Senior member
Jan 7, 2001
400
0
0
I hope Microsoft have made the same quality programming with .NET as they did with XP.

Kuphryn
 

BFG10K

Lifer
Aug 14, 2000
22,709
3,000
126
gcc is more ANSI-compliant than VC++, in case you didn't know yet.

Perhaps the initial version. Also how about in code optimisation, error messages and a general IDE? It beats GCC hands down in that respect.

Also I assume you guys are all running the latest Service Pack #5 for Visual Studio? Talking about the initial version makes little sense. If you're having any problems the first thing you should do is go and download the latest version.
 

HigherGround

Golden Member
Jan 9, 2000
1,827
0
0


<< Perhaps the initial version. Also how about in code optimisation, error messages and a general IDE? It beats GCC hands down in that respect. >>



ANSI: gcc has full ANSI C support, which can be enabled with -ansi flag

Optimisation: the following are flags to gcc that pretend to code optimisation: -O1, -O2, -O3, -ffloat-store, -fsave-memoized, -fno-default-inline, -fno-defer-pop, -fforce-mem, -fforce-addr, -fomit-frame-pointer, -finline-functions, -fcaller-saves, -fkeep-inline-functions, -fno-function-cse,, -ffast-math, -fstrength-reduce, -fthread-jumps, -funroll-loops, , -fcse-follow-jumps, -fcse-skip-blocks, -frerun-cse-after-loop, -felide-constructors, -fdelayed-branch, -fschedule-insns, -fschedule-insns2 ... need i go on? VC++ has some of those, but those are available only in Pro or Enterprise editions.

Error Messages: Are you implying that GCC has no error messagaes and the compiler fails silently after a failure? I didn't think so. Want to be anal and see every warning/error message spit out, try passing in -Wall and -pedantic directives. Having problems understanding them? Maybe it's time to go back to happy land of Windows programming.

IDE: no question here, having integrated editor and debugger is definitly a huge plus, however I've learned to live with nedit/xemacs + ddd.
 

Armitage

Banned
Feb 23, 2001
8,086
0
0


<< ANSI: gcc has full ANSI C support, which can be enabled with -ansi flag >>



And if you don't think that's important, you've never ported anything significant to another architecture or compiler!



<< Optimisation: the following are flags to gcc that pretend to code optimisation: <snip> >>



The gcc compiler does have some distance to go on the optimization front, particualrly for newer cpus.
Lots of options, but it is certainly not the fastest compiler out there.
But then, neither is VC++ (I think the crown would go to the intel compilers or maybe Kai).
Hopefully that will start getting more attention as gcc 3.0 stabilizes.



<< Error Messages: Are you implying that GCC has no error messagaes and the compiler fails silently after a failure? I didn't think so. Want to be anal and see every warning/error message spit out, try passing in -Wall and -pedantic directives. Having problems understanding them? Maybe it's time to go back to happy land of Windows programming.

IDE: no question here, having integrated editor and debugger is definitly a huge plus, however I've learned to live with nedit/xemacs + ddd.
>>



The only question is which IDE to use.
There is KDevelop, SNiFF+, Code Forge, Code Crusader/Medic, and lots more. You can choose the one that fits your style, instead of being locked into the MS idea of what an IDE should be.
There is no reason your choice of compilers should tie you to a particular IDE.
 

BFG10K

Lifer
Aug 14, 2000
22,709
3,000
126
Optimisation: the following are flags to gcc that pretend to code optimisation:

I know what the flags are. The question is how fast the final executable runs when the speed optimisations are turned on. GCC is no match for Visual C++ in that respect.

Error Messages: Are you implying that GCC has no error messagaes and the compiler fails silently after a failure?

Don't be any idiot. I never said such a thing.

I was mentioning the quality of the error messages. For example, if you miss a semi-colon GCC spits out a half-ass "parse error" while Visual C++ will tell you that a semi colon is missing. In otherwords, you actually get useful error messages.

Having problems understanding them? Maybe it's time to go back to happy land of Windows programming.

You're the one that has a problem understanding English sentences. And don't put words in my mouth.
 

HigherGround

Golden Member
Jan 9, 2000
1,827
0
0


<< I know what the flags are. The question is how fast the final executable runs when the speed optimisations are turned on. GCC is no match for Visual C++ in that respect. >>



you have yet to provide a concrete example where VC++ "beats gcc hands down", but that shouldn't be do difficult since comparing a technically superior compiler to a "pile of crap" should consistantly yield laughable results on the GCC side, shouldn't it? Regardless, FSF delivered a free multiplatform compiler, which performs just fine in real world conditions, putting down the efforts of people involved in the GCC project by comparing their software to a "pile of crap" is not only uncalled for, but it also speaks volumes about yourself ...



<< Don't be any idiot. I never said such a thing. >>



name calling is what people who run out of arguments use in their defence, i hope you didn't.



<< I was mentioning the quality of the error messages. For example, if you miss a semi-colon GCC spits out a half-ass "parse error" while Visual C++ will tell you that a semi colon is missing. In otherwords, you actually get useful error messages. >>



would you also like the parser to correct your errors? both tell you where the error occured, it requires zero effort to find a fix a problem like that. Besides, VC++ tries to be to smart for its own good since it assumes that all tokens must end with a semicolon, so it will put that error message for all statements that do not end with semicolon, sometimes it guesses the intention of the user, sometimes it doesn't.



<< You're the one that has a problem understanding English sentences. And don't put words in my mouth. >>



lol, amusing, but keep 'em comming ...