tags:

views:

1063

answers:

11

This is totally an opinion question. More for chatter. I'm taking some c++ tests and they are littered with cin and cout's.
Do people acutally still use these. I mean I've not seen one in an actual public application ever.

The last question I answered in a test was

int c1;
cout << "Enter numbers: " << flush;
for(int n = 0; cin >> c1; ++n){
   cout << c1 << endl;
}
When does this end..

The correct answer was "when a user hits ctrl+Z". Where in the heck would I press control+Z? I'm assuming in the terminal, I suppose. But honestly. I've not seen the terminal for ages and ages. And I'm sure as heck not going to attempt to program anything for it.

Are questions like this still relevant, in any of our lives?

+3  A: 

Yes, you might want to process or transform an input text file. It'll prove handy.

Mehrdad Afshari
Excellent answer.. I'd not likely use cin to do that.. but excellent answer. Ps CFile::read || fread(FILEPTR,...) would be my likely choice. But that's just because of my exprience. c++ is awesome.
baash05
+7  A: 

Yes, as long as console applications exist, cout and cin will exist.

Bharani
+2  A: 

Do people acutally still use these. I mean I've not seen one in an actual public application ever.

Define public application. What do you think the command line tools on any *nix use? And yes, we too use them, for our SDKs (at least cout and cerr). cin is often not the best when you have complex enough input -- you are left to write a lexer and a parser of sorts.

dirkgently
+1  A: 

It would be more acceptable, from a language standpoint, if the answer to "when will this program end" was "when EOF is received". Just because "Control-Z" is EOF in DOS, does not mean Ctrl-Z is the right answer.

Edit Edited with comment info.

Doug T.
What do you mean?
Edouard A.
@Doug: No, it would be more acceptable to answer "When the standard input stream reaches end-of-file." End-of-file is a language concept.
j_random_hacker
Looking at the "for", it ends when "cin >> c1" evaluates to false. But I never use expressions which combine command and query in a boolean condition.
Daniel Daranas
^Z is the DOS style of sending an end of file marker. It effectively terminates 'cin', causing the loop to break by returning false.
spoulson
Well.. If I saw this code. I'd want to smack the person who wrote it.. for's are counters, whiles are undefined. And agree about the EOF. Sadly it was multiple choice.
baash05
@Daniel: That's a good rule of thumb as it usually improves code clarity. But IMHO there are times when combining "action" and "query" leads to clearer code, and (again IMHO) this is one of them.
j_random_hacker
On a Linux system, EOF is Ctrl+D, if I recall it correctly. Plus if you're using a pipe or a redirection as that mini-app's input, it will end when the input stream is closed (there's nothing more to read).
Joe Pineda
(The last comment applies for any system which supports redirection, which includes even most versions of DOS)
Joe Pineda
+5  A: 

So you want to say that you are a programmer and never used or wrote console application?

Alex Reitbort
Not once since university. Mind you I'm mostly in the PDA world. But I don't even remember the last time I went to the console in windows. Not to do anything serious.
baash05
+14  A: 

Terminals are widely used, and will be used. The reason is that, when used skillfully, they're far more powerful than any GUIs.

Joonas Pulakka
"The reason is that, when used skillfully, they're far more powerful than any GUIs." - I dispute that.
unforgiven3
It was an generalization and with a hint of flamebait ;-) Obviously in some cases GUIs are more appropriate - for example, in graphic design work. But then, a well-written shell command combination typed in few seconds can replace dozens of mouse clicks and drags in some cases.
Joonas Pulakka
I take the bait. :) to that.. a well written menu and short cut keys in a GUI can remove the clicks all together.. ctrl+s saves in most apps. pressing f7 in VS saves all and compiles. do that in one key stroke in any terminal.
baash05
I guess you could create a shell alias (e.g. 'a+enter') that does that, and whatever else you wish. How about (copied from elsewhere) creating a list of all the unique package names imported by your Java code?" grep '^import ' *.java | sed -e's/.*import *//' | -e's/;.*$//' | sort -u >list ;-)
Joonas Pulakka
+9  A: 

No, such questions are not very relevant, but yes, people use std::cin and std::cout all the time. Even the ones who design graphical interfaces may use cout for debugging !

Benoît
Good on you. I use TRACE because it's not compiled in the release.. but it's good to see why others would use it.
baash05
+5  A: 

Of course it's relevant.

Many automation and admin scripts on a variety of servers rely on text output/input, for example. This is especially true on *nix systems, not as much on windows now that Powershell has come about with its fancy object support.

Then there are the ones of us (a dying breed, I admit) that pretty much LIVE in the terminal. I use the terminal for about 70-80% of my work. I just find it more natural, faster and more powerful than most related GUI apps.

Daniel Bruce
+1 I'm glad I'm not the only one of that dying breed.
Chris Lutz
+2  A: 

If you only ever write GUI or Web applications in your career you probably won't have much use for cin and cout. If you ever write anything embedded you'll change your position. You just use a different set of libraries for console apps, text-based apps, GUI apps, and Web apps.

Bill the Lizard
+2  A: 

I'd rather use wcin and wcout, at least you get the Unicode stuff.

Dave Van den Eynde
AFAIK, that's a non-portable windows c++ only concept. other systems (and languages/libraries) simply define that stdin/stdout take bytes and should be adequately encoded (usually ASCII or UTF-8)
Javier
Not according to ISO/IEC 14882:2003 section 27.3.
Dave Van den Eynde
+1  A: 

I've worked on many professional applications and we've not used cin or cout on any of them, even for logging/debugging. Why? Because the stateless *printf methods work perfectly well. One thing I've learned over the years is that stateless trumps statefull for maintainability.

It's true that the state "facilities" provided by iostreams are horrible, especially the inability to save/restore all state in a RAII way. OTOH, they're great for when the types of values may change (e.g. in templates) and you won't ever get problems like: http://stackoverflow.com/questions/601180
j_random_hacker