Thread: cout vs. printf

  1. #1
    Registered User Invincible's Avatar
    Join Date
    Feb 2002
    Posts
    210

    Question cout vs. printf

    What are the differences between cout << and printf() ?

    Is there any advantage to using one over the other?

    Also, cin >> has a method called .getline that allows you to convert white spaces without terminating by declaring a string array.


    Example:

    #include <iostream.h>
    #include <string.h>

    void main()

    {

    string szName[50];

    cin.getline( szName, 50, '\n' )

    cout << szName << endl;

    }

    Returns up to 50 characters including white spaces


    Is there any way to keep the scanf() function from terminating on a white space? or must you assign each string of user input to a seperate variable?

    I know there are some formatting options you can set following the % sign (type being madantory) including width and some others, but I can't seem to find the equivelent of cin.getline.

    Sorry this post is turning out longer than I expected, but if anyone cares to discuss exception handling for either of these functions I'm also interested in learning some methods to keep users from entering incorrect values ... i.e. letters where an integer is expected or exceeding the max string array.

    Thanks in advance.
    Last edited by Invincible; 02-08-2002 at 06:23 PM.

  2. #2
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    printf is easier to use to change the output format. cout is extensible (can cout << classInstance) and typesafe (cannot screw up by outputting an int with the double symbol).
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    cout is a class, printf is a function. cout tends to be easier to use than printf because you don't have to use format identifiers and printf is actually faster, but the speed difference is not noticeable enough to determine which one to use.

    Personally, I'll use cout when I'm coding in C++ and printf when I'm coding in C if only to remain constistent. There are exceptions when I'm using C++ and printf is easier or more effective.

    -Prelude
    My best code is written with the delete key.

  4. #4
    Registered User dirkduck's Avatar
    Join Date
    Aug 2001
    Posts
    428
    i havnt used printf much, so I always use cout, so much nicer

  5. #5
    Registered User Invincible's Avatar
    Join Date
    Feb 2002
    Posts
    210

    Lightbulb

    Thank you all for your responses.

    I see there's a general consensus here.

    After delving into some of the features of cout and printf, I would have to agree that cout has many more formatting options and is much more versatile. I'm assuming that's because it's a class, and printf is merely a function.

    I still can't figure out a better way to make it read white spaces in strings though.

    -invincible

  6. #6
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    Well.. I'm still learning C++, but for what it's worth, I still use printf() and scanf(). Of course, I really just don't know how to do formatting with cout. For example...
    Code:
    printf ("%20s%20s\n, Name, Value);
    printf ("%20s%20s\n", "-----", "-----");
    printf ("%20s%20d\n", "A", a);
    printf ("%20s%20d\n", "B", b);
    printf ("%20s%20d\n", "Sum", a + b);
    printf ("%20s%20d\n", "Product", a * b);
    It's a relatively simple (for me) way to print a nicely formatted table. Field specifiers seem to be the only nice feature of printf that I really use.

    scanf is quite nice on merit of it's [] conversion specifier, practically eliminates to handle input a character at a time. Just have to make sure the user doesn't overflow your string buffer. Even though one can stop scanf from terminating it's string input on a whitespace, the function you describe sounds like fgets().
    Callou collei we'll code the way
    Of prime numbers and pings!

  7. #7
    Registered User Invincible's Avatar
    Join Date
    Feb 2002
    Posts
    210
    I still don't see a way to make scanf read whitespaces.

    For instance, I want the user to enter his or her first and last name. Then I want scanf to read both names --space and all-- and store it in one string, instead it wants to terminate on the white space and store the second name in the next available input string effectively throwing off the entire program.

    Kinda discouraging since my next line of input happens to be age, and the users last name ends up being translated to an integer and output as -858993460

    <<What is your name?

    >>Skyler Lopez

    <<Hello Skyler.

    <<How old are you?

    >>user input bypassed>>

    <<Oh wow, you're -858993460!


    Pretty funny...

    If you know an stdio function that will handle this, please share.

    fgets() won't work because it reads from a file rather than input.
    "The mind, like a parachute, only functions when open."

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    20
    have you tried
    #include <string>
    string stringname;

    getline ( cin, stringname, termination character optional );


    or

    getline ( cin, stringname );
    cin.ignore();

    these get everything until the return.


    susan

  9. #9
    Fingerstyle Guitarist taylorguitarman's Avatar
    Join Date
    Aug 2001
    Posts
    564
    You could use fgets() with the stdin as the file handle to pull from console input.

    For speed I use cout & cin, but for most things I like to use the c-style I/O routines.

  10. #10
    Registered User Invincible's Avatar
    Join Date
    Feb 2002
    Posts
    210

    Thumbs up

    Thanks Taylor

    fgets( string, [] , stdin )

    it works
    "The mind, like a parachute, only functions when open."

  11. #11
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    char array [256]={0};
    scanf("%[^\n]",array);

    I think will read a string in a similar fashion to gets()
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  12. #12
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I think will read a string in a similar fashion to gets()
    Yep, it's a fancy way of saying gets() with no added benefit except to confuse new people. On the whole I don't recommend using scanf() except for very simple input, anything else is easier and safer using fgets.

    On a side note, cout is very refreshing for simple input, but you still have to use cin.getline() to read anything with spaces and you still need to clear the input stream with cout just as with scanf. So they're pretty much the same in usage and problems from what I've seen, fgets and cin.getline are what I tend to use the most

    -Prelude
    My best code is written with the delete key.

  13. #13
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    The best is Console.Writeline(..); because it combines the abilities of cout and printf and adds additional formatting features not build into C and C++ such as outputting formatted numbers with commas or currencies plus more options available in C#.

  14. #14
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    Not the same as gets because you can control the lenght of the read:

    scanf("%20[^\n]",array);

    Stops when it reaches newline or else 20 chars. Definately not the same as gets.

  15. #15
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    In addition formatters can skip characters in the datafile

    fscanf(fptr, "%10[^\n]%*8c%2d",array,&num);

    If you know what you are doing than you can be extremely effective in C but for this kind of stuff you need the standard specification.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. making it portable.....?
    By ShadeS_07 in forum C Programming
    Replies: 11
    Last Post: 12-24-2008, 09:38 AM
  3. Need Help with a Bowling Score Program
    By oobootsy1 in forum C++ Programming
    Replies: 6
    Last Post: 11-01-2005, 10:04 AM
  4. Whats Wrong Whith This!?
    By SmokingMonkey in forum C++ Programming
    Replies: 8
    Last Post: 06-01-2003, 09:42 PM
  5. (printf VS cout) and (including libraries)
    By \cFallen in forum C++ Programming
    Replies: 3
    Last Post: 01-20-2003, 09:47 PM