cout to printf problems

This is a discussion on cout to printf problems within the C++ Programming forums, part of the General Programming Boards category; i m trying to port something of cout to printf this is the cout: int y = 1; int x ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    70

    cout to printf problems

    i m trying to port something of cout to printf this is the cout:
    int y = 1;
    int x =1;
    cout<<"schrijf 2 getallen voor te vermenigvuldigen: \n";
    cout<<"het produkt van de 2 getallen is :";
    cout<<x*y;






    and this is the not working printf version :
    int y = 1;
    int x =1;
    double a = (x*y);


    printf("schrijf 2 getallen voor te vermenigvuldigen: \n");

    printf("het produkt van de 2 getallen is : %a ");
    i have got some reasons to make it printf so how do i have to do that?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,408
    Code:
    int y = 1;
    int x =1;
    printf("schrijf 2 getallen voor te vermenigvuldigen: \n");
    printf("het produkt van de 2 getallen is :%d", x*y);
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    Code:
    printf("The product of the two integers is: %f ", x*y);
    But the product doesn't have to be a double, int*int=int.

    (thanks RichieT)

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    70
    the compille had a warning that's says that it better will be a double so.....

  5. #5
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    I've seen this problem many times.
    Somehow, people think that printf acts as it does in PHP.

    I made many such mistakes when I was learning C++...
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,408
    the compille had a warning that's says that it better will be a double so.....
    You compiled my version, or twomers' version? Incidentally, you might want to read about printf.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Apr 2006
    Posts
    2,021
    Quote Originally Posted by twomers
    Code:
    printf("The product of the two integers is: %f ", x*y);
    But the product doesn't have to be a double, int*int=int.

    (thanks RichieT)
    That won't work right. Printf will expect the second peramitter to be a double, but you're sending it an int. The compiler won't complain because it's a veradic function. Yet when you try to print it out it will try to read the int as if it were a double. It might crash, look beyond the input value in memmory, because ints are generally smaller than doubles (not sure about this). If it doesn't, it will print out the bitwize value as if it was a double. And since 1 is not encoded in an int the same whay as it is in a double, the value will be different.

    You would need to do this:
    Code:
    printf("The product of the two integers is: %f ", (double)(x*y));
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  8. #8
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    If x or y are large enough, then the result would overflow an integer. Maybe that is why the double is being used?

    Try setting x and y to INT_MAX and you'll see what I mean. (double)(x * y) won't work in that scenario.

    Code:
    printf("het produkt van de 2 getallen is : %.f\n", (double) x * (double) y);
    Note the use of "%.f" so the output "appears" to be an integer.

    Een enkel gedachte!

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    70
    i used the version of laserlight and it works

    thnx to all

  10. #10
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    You're right. I got kinda confused with the whole -

    double a = (x*y);
    thing, and the printf, so instead of printf'in "%f", a, I did "%f", x*y, if ya get me. I should have had a there instead of x*y. My bad.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems reading entered race times C
    By loopymoo26 in forum C Programming
    Replies: 12
    Last Post: 05-23-2009, 07:38 AM
  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. Homework due tonight! help please!!
    By Andy717 in forum C++ Programming
    Replies: 41
    Last Post: 04-07-2005, 01:18 PM
  4. how to do printf( "%c", 12 ); via ostream cout ??
    By L.O.K. in forum C++ Programming
    Replies: 9
    Last Post: 01-08-2005, 05:37 PM
  5. Azbia - a simple RPG game code
    By Unregistered in forum Game Programming
    Replies: 11
    Last Post: 05-03-2002, 06:59 PM

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