PDA

View Full Version : cin or scanf???


benzun_1999
Jul20-06, 09:46 AM
i use cin from iostream.h in c++ i also knowabout scanf in stdio.h but which is better??? What is the difference betweeen the both???

-benny

neurocomp2003
Jul20-06, 11:35 AM
its a matter of which one your more comfortable with.
scanf requires you to know the format parameters(no spaces % & etc) whereas cin the format parameters are hidden and all you gotta do is cin>>var. thus cin is nicer to look at. Personally though if you want to change parameters like precision i think it makes the code ugly.
ALso you can create functions based on scanf for easier display. UNfortunately I can't remember if C compilers allow for default values so i use C++ compiler and scanf.

gnomedt
Jul21-06, 04:39 PM
(Almost) always cin.

Always prefer the updated versions of functions over the old C functions which are provided for compatibility. In the case of scanf, the danger occurs when passing "%s", where you risk buffer overflow. cin provides getline, which avoids the problem by requiring that you pass in the length of the buffer.

Changing precision using cin is more verbose, but in general using cin is preferable.

nmtim
Jul22-06, 06:19 PM
(Almost) always cin.

Always prefer the updated versions of functions over the old C functions which are provided for compatibility.

I agree: they didn't go to a new system just for kicks. Also, scanf requires passing pointers: classic bug injection site.

Cheers,
Tim

chmate
Jul28-06, 01:05 PM
use fgets(); :)

jtbell
Jul28-06, 03:56 PM
(cin provides getline, which avoids the problem by requiring that you pass in the length of the buffer.

Even better, use the standalone getline() which reads into a C++ string variable, Then you don't have to worry about the length of the buffer, because the string automatically expands as necessary!


#include <iostream>
#include <string>

using namespace std;

int main ()
{
string whatever;
cout << "Give me a string, terminated with a newline: ";
getline (cin, whatever);
cout << "You entered \"" << whatever << "\"" << endl;
return 0;
}


You can bang away on the keyboard almost forever before hitting the "enter" key, if you have enough memory. :cool:

mariam.kupa
Jan27-09, 08:02 AM
i use cin from iostream.h in c++ i also knowabout scanf in stdio.h but which is better??? What is the difference betweeen the both???

-benny

My friend,if you believe me i should say that "scanf()" and "printf()" are faster than "cin>> "and "cout<<" Once I had a time limit when my code was submited ,after a long thought i changed "cin>> "and "cout<< " by "scanf()" and "printf()" and my problem was solved, and if i remember well "scanf()" and "printf()" was 1sc faster then "cin" and "cout".But to tell the truth scanf() has problems with strings.

shoehorn
Jan27-09, 02:39 PM
My friend,if you believe me i should say that "scanf()" and "printf()" are faster than "cin>> "and "cout<<" Once I had a time limit when my code was submited ,after a long thought i changed "cin>> "and "cout<< " by "scanf()" and "printf()" and my problem was solved, and if i remember well "scanf()" and "printf()" was 1sc faster then "cin" and "cout".But to tell the truth scanf() has problems with strings.

*shakes head in utter disbelief*

mariam.kupa
Jan29-09, 10:35 AM
*shakes head in utter disbelief*

Sorry,my friend,but i didn't understand what do you mean? Am I wrong or not?

signerror
Jan29-09, 09:43 PM
operator>> is overloadable to user-defined types, if you're into that. Otherwise, it's mostly a matter of aesthetic preference.

In the case of scanf, the danger occurs when passing "%s", where you risk buffer overflow.

Then don't take %s arguments with scanfs! Use fgets for those.

I agree: they didn't go to a new system just for kicks.

Welcome to software.