Jump to content

  • Log In with Google      Sign In   
  • Create Account

printf/cout, scanf/cin


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
20 replies to this topic

#1 Fractal   Members   -  Reputation: 122

Like
Likes
Like

Posted 23 October 2001 - 07:23 AM

What''s the difference between cout and printf, or cin and scanf? Could I be any more dumber? (What do you mean, "No"?)

Sponsor:

#2 Kickpuncher   Members   -  Reputation: 369

Like
Likes
Like

Posted 23 October 2001 - 08:33 AM

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."

#3 EvilJosh   Members   -  Reputation: 122

Like
Likes
Like

Posted 23 October 2001 - 08:36 AM

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.

#4 EvilJosh   Members   -  Reputation: 122

Like
Likes
Like

Posted 23 October 2001 - 08:42 AM

Did that help you answer your question at all I hope that it did cause thats what I think hit means


#5 igamemkr   Members   -  Reputation: 122

Like
Likes
Like

Posted 23 October 2001 - 08:46 AM

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?

#6 xstreme2000   Members   -  Reputation: 152

Like
Likes
Like

Posted 23 October 2001 - 08:52 AM

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.

#7 Anonymous Poster_Anonymous Poster_*   Guests   -  Reputation:

Likes

Posted 23 October 2001 - 09:45 AM

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.


#8 Null and Void   Moderators   -  Reputation: 1087

Like
Likes
Like

Posted 23 October 2001 - 10:00 AM

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!]

#9 Krunk   Members   -  Reputation: 122

Like
Likes
Like

Posted 23 October 2001 - 10:19 AM

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.

#10 Oluseyi   Staff Emeritus   -  Reputation: 1678

Like
Likes
Like

Posted 23 October 2001 - 11:40 AM

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;


#11 WarMage   Members   -  Reputation: 122

Like
Likes
Like

Posted 23 October 2001 - 11:54 AM

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...



#12 Krunk   Members   -  Reputation: 122

Like
Likes
Like

Posted 24 October 2001 - 10:37 AM

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

#13 Oluseyi   Staff Emeritus   -  Reputation: 1678

Like
Likes
Like

Posted 24 October 2001 - 11:01 AM

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.


#14 flip5577   Members   -  Reputation: 138

Like
Likes
Like

Posted 25 October 2001 - 05:10 AM

But what about text colors?

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

If so how

#15 Fractal   Members   -  Reputation: 122

Like
Likes
Like

Posted 25 October 2001 - 06:33 AM

I was wondering that myself

Could I be any dumber?
(What do you mean, "No"?)


#16 Cyberdrek   Members   -  Reputation: 100

Like
Likes
Like

Posted 25 October 2001 - 06:53 AM

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

#17 Oluseyi   Staff Emeritus   -  Reputation: 1678

Like
Likes
Like

Posted 25 October 2001 - 09:51 AM

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.

#18 Pactuul   Members   -  Reputation: 122

Like
Likes
Like

Posted 25 October 2001 - 10:09 AM

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


#19 Null and Void   Moderators   -  Reputation: 1087

Like
Likes
Like

Posted 25 October 2001 - 10:18 AM

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!]

#20 Arild Fines   Members   -  Reputation: 968

Like
Likes
Like

Posted 25 October 2001 - 11:53 AM

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"




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS