Thread: Cin & Cout VS. Scanf & Printf

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    6

    Cin & Cout VS. Scanf & Printf

    The title says it all, which should i use, which i shouldn't, or are there special circumstances. They use different libraries, does using one have benefits over the other? THe book I'm going through uses Cin & Cout, but loads of people online use the <stdio.h> library approach.

    So which way is better? THank you for your replies.

  2. #2
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    If you're already using C++, then you'll probably find that cin and cout are a lot easier to use.
    Away.

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    6
    So then why doesn't everybody just use cin and cout if they're easier to use? Are they inefficient or something like that?

    -------MaTrIxXx-------

  4. #4
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    For my own gratification, could someone convert the following printf into a cout? I know a little about cout but what I know looks extremely cumbersome and formatting is difficult But others seem to claim it's easy.

    Code:
    printf("%02X  %10d  val=%7.3f \n", hval, dval, fval);
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  5. #5
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Code:
    sprintf( buffer, "%02X  %10d  val=%7.3f \n", hval, dval, fval);
    cout<<buffer;
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  6. #6
    Registered User
    Join Date
    Jul 2003
    Posts
    102

    Suggestions

    Hi,
    Using cout and cin will make life easier. If you use scanf or printf you should be aware to put & in scanf.......... all those sort of things. But if you love C more then please use scanf and printf as i am using.
    Saravanan.T.S.
    Beginner.

  7. #7
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,212
    scanf causes many a buffer overflow.

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Originally posted by WaltP
    For my own gratification, could someone convert the following printf into a cout? I know a little about cout but what I know looks extremely cumbersome and formatting is difficult But others seem to claim it's easy.

    Code:
    printf("%02X  %10d  val=%7.3f \n", hval, dval, fval);
    Code:
       cout << setfill('0') << setw(2) << hex << hval << setfill(' ') << dec << setw(12) << dval
          << "  val=" << setw(7) << setprecision(3) << fval << endl;
    However the hex is in lowercase vs uppercase for the printf(). I also vote for printf().
    Last edited by swoopy; 08-07-2003 at 06:01 PM.

  9. #9
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Code:
    
    cout << setfill('0') << setw(2) << hex << hval << setfill(' ') << dec << setw(12) << dval
          << "  val=" << setw(7) << setprecision(3) << fval << endl;
    vs.
    printf("%02X  %10d  val=%7.3f \n", hval, dval, fval);
    
    Thanks swoopy, that's what I thought. And I've written much more intricate printf's. So what's so special aboiut cout? Looks like most enhancements these days, both in and out of computers. "Let's remove the really useful stuff and get it to the bare bones. Too bad if you need anything specia 'cuz only 20% of the users actually use them."

    Like tapes in answering machines.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  10. #10
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    printf may be a bit easier for the incredibly complex things like that, but for simply displaying a string or an int, or getting either from the user, cin and cout are easier.
    Away.

  11. #11
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    It's also more type-safe. In printf, you have to memorize and double-check that the tokens '%?' are correct. If they aren't, your compiler has no way of knowing.

  12. #12
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    This won't work.
    Code:
    cout << setfill('0') << setw(2) << hex << hval << setfill(' ') << dec << setw(12) << dval
          << "  val=" << setw(7) << setprecision(3) << fval << endl;
    http://cplusplus.com/ref/iostream/io...precision.html
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  13. #13
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    I personally linke printf() and the like because C++ code looks too sloopy to me. I like things that look organized. << and >> and the like don't look organized so I'm reluctent to learn it.

  14. #14
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    Originally posted by Thantos
    I personally linke printf() and the like because C++ code looks too sloopy to me. I like things that look organized. << and >> and the like don't look organized so I'm reluctent to learn it.
    I would agree, it doesn't look like code, looks like a bash script or something. Not saying bash scripts aren't code, just that it is not what I expect c & c similar codes to look like.

  15. #15
    Registered User
    Join Date
    Jul 2002
    Posts
    913
    If C++ is a superset of C why did they even bother with cin/cout?


    C++ at times seems more like a different language/knock off.

    The whole object oriented stuff sounds great, I think ill try to avoid c++ as long as possible and give Objective C a shot. I havnt heard much, but i havent heard that they cheated either.

    But then again there are alot of C++ers out there, there has to be something good about it...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. IF CONDITION plese help
    By birumut in forum C Programming
    Replies: 12
    Last Post: 03-06-2009, 09:48 PM
  2. I need help on this particular Linked List problem
    By sangken in forum C Programming
    Replies: 11
    Last Post: 08-06-2006, 12:26 AM
  3. help with basic program
    By JOlszewski in forum C Programming
    Replies: 3
    Last Post: 02-01-2006, 04:19 PM
  4. error in program????
    By SpEkTrE in forum C Programming
    Replies: 5
    Last Post: 11-24-2003, 06:16 PM
  5. Drawing tables in C
    By stanoman in forum C Programming
    Replies: 5
    Last Post: 10-09-2003, 10:14 AM