cout << // Aka. command never to use with Input?!

Started by
12 comments, last by Sarlok 22 years, 3 months ago
Hey. What''s the story?? ------------- #include #include /*is unsure of the right lib. for _getch, and can''t be bothered checing right now */ void main(void) { cout << "Aha. This won''t display, will it!?"; char cWhatEverYouWant = _getch(); } ------------- Why will that just output nothing untill *after* I''ve pressed a key? I''ve worked out that using printf fixes the problem... but... printf is terrible, clumsy, and shouldn''t have been allowed into any library in the first place. But then Why should my program be prioritizing _getch() over cout in the first place? Thanks.
Four-thousand, six-hundred, and ninety-one irradiated haggis.
Advertisement
quote:Original post by Sarlok
I''ve worked out that using printf fixes the problem... but... printf is terrible, clumsy, and shouldn''t have been allowed into any library in the first place.

You shouldn''t say that, since you make yourself look less knowledgable than you already do. You do know that there was a little known language called C (heh), and it used printf (and family) because there was no cout. Guess what? C++ aims for backwards compatibility. The printf function is included (thankfully).
quote:Original post by Sarlok
But then Why should my program be prioritizing _getch() over cout in the first place?

Ever heard of buffering? Well, that''s what''s happening. Call "cout.flush()" before you use "getch".



[Resist Windows XP''s Invasive Production Activation Technology!]
quote:Original post by Null and Void You shouldn''t say that, since you make yourself look less knowledgable than you already do. You do know that there was a little known language called C (heh), and it used printf (and family) because there was no cout . Guess what? C++ aims for backwards compatibility. The printf function is included (thankfully).


Yes. I know of it''s reason for existing. that doesn''t mean I like it.

quote:Original post by Null and Void Ever heard of buffering? Well, that''s what''s happening. Call "cout.flush()" before you use "getch".


Nope. That''s why I''m here.
Thanks!

"When you kill one, it is a tragedy. When you kill ten million, it is a statistic."
- Sarlok
Four-thousand, six-hundred, and ninety-one irradiated haggis.
quote:Original post by Sarlok
Yes. I know of it''s reason for existing. that doesn''t mean I like it.

When you said it shouldn''t be part of any library, it kind of annoyed me . I still like the C style I/O functions (I do a lot of C programming as well as OO C++, so I''m a cross-style programmer, heh).

[Resist Windows XP''s Invasive Production Activation Technology!]
Yep

I programming in C++ but dislike cout and cin I prefer the C way (printf) but then I was a C man before C++
That is not dead can eternal lie, and even in sleep death may die.
I don''t find one better than the other. Cout / cin is a little easier to use and is definately more flexable with the << operator when applied to classes. (So you can do things like "Class r; cout << r;") However, the stdio stuff is great for formating, AND if you''re not outputing to the console, sprintf is far better than trying to figure out how to get a char * from some of the C++ class streams and functions. If just depends on what you''re doing.

P.S. Used to hate printf cause I thought it was harder... then I had to learn how to use sprintf so I could create debug strings in win32. Anyway, printf is cool in its own way.
-Warden
quote:Original post by Warden
However, the stdio stuff is great for formating, AND if you're not outputing to the console, sprintf is far better than trying to figure out how to get a char * from some of the C++ class streams and functions. If just depends on what you're doing.

P.S. Used to hate printf cause I thought it was harder... then I had to learn how to use sprintf so I could create debug strings in win32. Anyway, printf is cool in its own way.

I think streams are much easier to use than the messy and un-typesafe sprintf.

    int var1 = 12;float var2 = 4.354f;char var3[] = "hmmm";// C waychar buffer[120];sprintf(buffer, "%d %.3f %s", var1, var2, var3);std::cout << buffer << std::endl;// C++ waystd::ostringstream sstream;sstream << var1 << " " << var2 << " " << var3;std::cout << sstream.str() << std::endl;  


Using streams over sprintf is:

1. Totally typesafe
2. Easier to use (you don't need to remember those pesky % parameters, or get your parameters in the right order)
3. Easier to read (you don't need to remember those pesky % parameters, and you don't need to keep looking at the parameters to see what variable is being displayed where)
4. You never need to worry about how large to make your buffer (notice the 'char buffer[120]' max string size)
5. You can use your own classes with it.


However, I will concede that doing heavy duty formatting using streams will require more coding than the sprintf way. But it still has the other benifits mentioned above.


- Houdini

Edited by - Houdini on December 20, 2001 2:06:43 PM
- Houdini
I find formating as easy.

For formating with streams you use manipulators.
Printf had to be included as there was no reasonable other way for C to have such printing capabilities. It''s ugly, but it has its uses.

As for forming debug strings with it Warden, couldn''t you use ostringstream? Getting a char* from a string is c.str(), getting a string from a stringstream is .str(). It''s quite simple Formatting is quite easy to do with streams too, and probably more memorable since you''re using English words instead of remembering all the %-2.4d madness. Slightly more verbose though.

[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost ]
In answering your question, which no one else seems to be doing, it''s not displaying anything before you press a key because

cWhatEverYouWant = _getch();

the program is stopping at this line. The program won''t continue until _getch() has something to return to cWhatEveryouWant.


----
Herb M. (mdfmKoRn)
s3202@attbi.com

This topic is closed to new replies.

Advertisement