Archived

This topic is now archived and is closed to further replies.

Fractal

printf/cout, scanf/cin

Recommended Posts

I _think_ that printf, and scanf are from the days of C, and cout, and cin came along with C++... the differences, though i''m not too sure.

check the msdn, might help.
Scott

Email
Website

"If you try and don''t succeed, destroy all evidence that you tried."

Share this post


Link to post
Share on other sites
I can help a little CIN is a Console Input like sa you wanted a use to enter his/her age and COUT our prolly get Console Out puts a string out. and I think... Printf is a same thing as COUT.. not to sure about that one.

Share this post


Link to post
Share on other sites
cout and cin are stream objects, while printf and scanf are functions.

Jesus is knocking at the door of your heart, won''''t you let him in?

Share this post


Link to post
Share on other sites
One bit of info (don''t quote me on this though):

the cout/cin/cerr are more reliable

but printf/scanf functions are very slightly quicker

Mind you if your using these function I can''t believe that there will be any relevent difference.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by xstreme2000
One bit of info (don''t quote me on this though):

the cout/cin/cerr are more reliable

but printf/scanf functions are very slightly quicker

Mind you if your using these function I can''t believe that there will be any relevent difference.


Neither is more reliable than the other, when used correctly. I don''t think there''s much speed difference either.

They are just two different interfaces to the standard input/output streams. There''s also write() and read(), if you like lower level printing.

It doesn''t matter which one you use in the slightest. Just don''t mix them in one program.

Share this post


Link to post
Share on other sites
In most implementations the C i/o library is faster with large amounts of data. That doesn''t mean your compiler or all compilers are the same way, they each have to implement the C and C++ standard libraries on their own. Neither is more reliable than the other, but the C++ version is more type safe (sometimes to your detriment, but C++ has its ways of getting around those problems).

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

Share this post


Link to post
Share on other sites
cin/cout are type safe. The type of the operand determines how IO is performed. With printf/scanf, you have to tell the function the type of its arguments seperately, so theres scope for errors like:

printf("%s", `a`);

Here, a single char is printed as a string, which is a bad thing because printf is expecting the address of a null terminated array of characters. Instead it gets a single char, which is probably a value below 127. The equivalent with cout can`t go wrong:

cout << `a`;

Thats the main advantage of cin/cout - they protect you from your own errors. The disadvantage is that the syntax is horrible if you want columns of data printed.

Share this post


Link to post
Share on other sites
quote:
Original post by Krunk
The disadvantage is that the syntax is horrible if you want columns of data printed.


#include <iomanip>
...
// print age in an 8-cell column
// use blanks for padding
cout << setw(8) << setfill('' '') << age << endl;

Share this post


Link to post
Share on other sites
quote:
Original post by Fractal
Could I be any more dumber?


Yes, if you can come up with a use of English that is more incorrect than "more dumber"

---------
-WarMage
...instant smartass, just add smart and ass...

Share this post


Link to post
Share on other sites
quote:
Original post by Oluseyi
// print age in an 8-cell column
// use blanks for padding
cout << setw(8) << setfill(' ') << age << endl;


That`s not bad, but how about:
printf("%-20s%3d%4d%4d%3d", name, age, height, weight, shoesize);
vs
cout << setw(20) << setiosflags(ios::left) << name
<< setiosflags(ios::right) << setw(3) << age
<< setw(4) << height << setw(4) << weight
<< setw(3) << shoesize << endl;

Its a contrived example but it shows that with printf, layout is seperated from the values being output. This lets you see the column layout easily. With the cout version the layout commands are scattered through the statement. Cout is safer and more powerful, but too verbose for my liking when outputting tables.

Edited by - Krunk on October 24, 2001 5:42:16 PM

Share this post


Link to post
Share on other sites
quote:
Original post by Krunk
Its a contrived example but it shows that with printf, layout is seperated from the values being output. This lets you see the column layout easily. With the cout version the layout commands are scattered through the statement. Cout is safer and more powerful, but too verbose for my liking when outputting tables.


Point taken. I guess I almost never output tables.

Share this post


Link to post
Share on other sites
I removed my post after seing that most of the people here had already answered...

Sorry..


Edited by - cyberdrek on October 25, 2001 1:59:37 PM

Share this post


Link to post
Share on other sites
quote:
Original post by flip5577
But what about text colors?

is there a way to modify the color of the text outputed when you cout?


No. There isn''t when you use printf() either. Text attributes comes under console properties, and that''s very platform-specific.

Share this post


Link to post
Share on other sites
I think a big misunderstanding here is equating cout and cin (c++ classes) to printf and scanf (c functions)

cout and cin are just instances of a class. you rename these to what you want. what''s more powerful about it is they come from the fstream and iostream classes. YOu can even extended this to arrays of chars with strstream.

For example you can read in a file like so:

char buffer[1000];
//our array is now just like cout and cin!!!
strsteam my_buffer(buffer,sizeof(buffer);

fstream my_file("some.fil", ios::in);

char hold[100];
while (!my_file.eof())
{
my_file.getline(hold, sizeof(hold), "\n") // "\n" is a delimeter that can be any character

//so now we have a line from our file...stick in the buffer
my_buffer << hold;
}

//now we just stored the whole file into the my_buffer stream
//which points to char buffer[]...
//and now we can edit and jump aroun this buffer like a file
//example usage:

my_buffer.seekg(5); //skip five spaces in
my_buffer >> hold;
cout << hold; // should print out the first line (until it hits the "\n" in the array

anyways that''s just some sample stuff to use. the fstream and iostream and strstream are very powerful and can be used together for a lot of stuff. Just food for that.

Pactuul

Share this post


Link to post
Share on other sites
Pactuul, same thing can be done with the C i/o functions. The printf and scanf functions are just macros for fprintf and fscanf that happen to use stdin and stdout as their files. The C version is modular, the C++ version is object oriented, not a whole lot of difference.

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

Share this post


Link to post
Share on other sites
Uhmm...what makes printf and scanf modular? Structured I can see, but modular?

"A society without religion is like a crazed psychopath without a loaded .45"

Share this post


Link to post
Share on other sites
quote:
Original post by flip5577
is there a way to modify the color of the text outputed when you cout?


As Oluseyi said, coloured text is platform specific. If you`re lucky, you might find your console accepts ANSI Escape sequences. The consoles I`ve used in linux do, among others. They`re alright if you can be certain of the particular console the program will run in, but I wouldn`t use them in a shipping product

Share this post


Link to post
Share on other sites