how to do printf( "%c", 12 ); via ostream cout ??

This is a discussion on how to do printf( "%c", 12 ); via ostream cout ?? within the C++ Programming forums, part of the General Programming Boards category; how to do printf( "%c", 50 ); and also printf( "%d", 'a' ); via ostream cout ?? thx...

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    88

    how to do printf( "%c", 12 ); via ostream cout ??

    how to do
    printf( "%c", 50 ); and also printf( "%d", 'a' );
    via
    ostream cout ??


    thx

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You should really consider using complete sentences. See how easy that was to read? See how easy it was to figure out what I was suggesting? Now let's see if we can get a question out of that mess you've posted above:

    "You can use a number, 50, for example, with the %c format specifier, or a letter, and both will print a character. Is there a way to do that with the ostream / cout?"

    My goodness that was so much better! I'm almost blown away by my own linguistic ability!
    Code:
    cout << static_cast<char>(50);
    Well, there's one way...

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    88
    Quote Originally Posted by quzah
    You should really consider using complete sentences.

    Quzah.
    I' m sorry. I am a Chinese.

    thanks for helping

  4. #4
    Registered User Kybo_Ren's Avatar
    Join Date
    Sep 2004
    Posts
    136
    Quzah, don't blast him about his language -- he obviously doesn't speak English natively. Anyway, you (the OP) want to learn about type casting. Quzah showed you the standard C++ way for regular type casts -- static_cast<>(). It accepts a data type as its template parameter and the variable you want to cast as a parameter. So, to convert "int a" into a float, you could say,
    Code:
    int a;
    //...
    float f=static_cast<float>(a);
    Also note that C++ accepts C-style casting, as well. Thus, the following will work (even if it is now outdated):
    Code:
    int a;
    //...
    float f = (float)a;
    Again, this is outdated and isn't as clear as using a C++-style cast.

    The other C++ casts are dynamic_cast<>(), const_cast<>(), and reinterpret_cast<>().


    Now, to get to your question:
    You need to type cast. For this cast, you would use static_cast (just as Quzah showed). You can do either
    Code:
    int a = 50;
    //...
    std::cout << static_cast<char>(a) << std::flush;
    //or
    std::cout << static_cast<char>(50) << std::flush;
    or
    Code:
    int a = 50;
    //...
    std::cout << (char)a << std::flush;
    //or
    std::cout << (char)50 << std::flush;

    I hope that answers your question in more depth.

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    And the other way round, the second example:
    std::cout << static_cast<int>('a');
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >printf( "%c", 50 );
    If you want to avoid casting (always a good idea if you can manage it):
    Code:
    cout.put(50);
    >printf( "%d", 'a' );
    Sadly, the other way around isn't nearly as clean:
    Code:
    cout<< static_cast<int>('a');
    My best code is written with the delete key.

  7. #7
    Registered User
    Join Date
    Sep 2002
    Posts
    88
    Thx all , I will study about these things more deeply,.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Kybo_Ren
    Quzah, don't blast him about his language -- he obviously doesn't speak English natively.
    Obvious huh? You must be new here. We* get* complete* idiots* here * all the time* who can't or simply won't "speak" coherently. So, how again was it obvious? Oh, that's right, you're one of those idiots who thinks it'll make you bigger in our eyes to blast me. Well go on, give it your best shot.



    Quzah.
    *Go on, it's a link. Knock yourself out.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User Kybo_Ren's Avatar
    Join Date
    Sep 2004
    Posts
    136
    No, I don't think it will make me "bigger in your eyes".

    I have not heard any native speaker say, "how to do.."
    when asking how to do anything.

    Also, the question was pretty short.

    For me, that's a big clue that either
    1.) The OP doesn't know English very well
    2.) The OP doesn't speak English natively.

    In either case, responding with a highly sarcastic, "You should really consider using complete sentences. See how easy that was to read? See how easy it was to figure out what I was suggesting? Now let's see if we can get a question out of that mess you've posted above:" isn't very nice.

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Who said anything about "nice"? Who are you, Ms. Manners?

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. making it portable.....?
    By ShadeS_07 in forum C Programming
    Replies: 11
    Last Post: 12-24-2008, 09:38 AM
  2. error in program????
    By SpEkTrE in forum C Programming
    Replies: 5
    Last Post: 11-24-2003, 06:16 PM
  3. Azbia - a simple RPG game code
    By Unregistered in forum Game Programming
    Replies: 11
    Last Post: 05-03-2002, 07:59 PM
  4. rand()
    By serious in forum C Programming
    Replies: 8
    Last Post: 02-15-2002, 02:07 AM
  5. Can't figure out why?
    By kwigibo in forum C Programming
    Replies: 10
    Last Post: 10-14-2001, 11:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21