Peculiar Problem with Printf

This is a discussion on Peculiar Problem with Printf within the C++ Programming forums, part of the General Programming Boards category; I am trying to find numbers between 1 and 100000000 which are both perfect squares and perfect cubes. I have ...

  1. #1
    Unregistered
    Guest

    Unhappy Peculiar Problem with Printf

    I am trying to find numbers between 1 and 100000000 which are both perfect squares and perfect cubes.

    I have written the code successfully and the results are good. The problem is coming with printf.
    When I print the results seperately, they're right.. when I print them in the same line, it is showing 0....

    please check the code below:

    #include <stdio.h>


    void sqroot(long long int number, long long int *prevsqroot, long long int *sqexists)
    {
    long long int i;
    *sqexists = 0;

    for(i= *prevsqroot; i<=number; i++)
    {
    long long int var = i*i;

    if (var == number)
    {
    *prevsqroot = i;
    *sqexists = 1;
    break;
    }

    if (var > number)
    {
    break;
    }

    }
    }

    void curoot(long long int number, long long int prevsqroot, long long int *prevcuroot, long long int *cuexists)
    {
    long long int i;
    *cuexists = 0;


    for(i=*prevcuroot; i<=prevsqroot; i++)
    {
    long long int var = i*i*i;

    if (var == number)
    {
    *prevcuroot = i;
    *cuexists = 1;
    break;
    }

    if (var > number)
    {
    break;
    }

    }
    }


    main()
    {
    long long int i;
    long long int prevsqroot = 1;
    long long int prevcuroot = 1;
    long long int sqexists=0, cuexists=0;

    for (i=1; i<=1000000000; i++)
    {
    sqroot(i, &prevsqroot, &sqexists);

    if (sqexists ==1)
    {
    curoot(i, prevsqroot, &prevcuroot, &cuexists);

    if (cuexists ==1)
    {
    printf("Squareroot is %6d\n", prevsqroot);
    printf("Cuberoot is %6d\n", prevcuroot);

    printf("Number is %9d Sqroot is %6d Curoot is %6d\n", i, prevsqroot, prevcuroot);
    }
    }

    }


    printf("Successful!\n");

    getchar();
    }



    notice that there are three printfs under the if above... the first
    two printfs are working fine... but when i do the same thing in the thrid printf, it is not working..



    another problem is that when i run this code in TC++, I get one extra problem, it is showing i as a negative number ... however, the squareroots and cuberoots calculated are right ...


    Can anyone tell me what's gone wrong with this ?

  2. #2
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802
    *throws up*
    Code tags please! Pretty please! Then I'll take a look.

  3. #3
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,267
    And DON'T cross post.
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  4. #4
    looking for the truth moemen ahmed's Avatar
    Join Date
    Feb 2002
    Location
    Egypt
    Posts
    161
    here we go , hoping it ll help u
    use this statement instead of yours


    Code:
    printf("Number is %9d  Sqroot is %6d  Cubroot is %6d \n " ,(int)i,(int)prevsqroot,(int)prevcuroot);
    Last edited by moemen ahmed; 07-02-2002 at 12:40 AM.
    Programming is a high logical enjoyable art for both programer and user !!

  5. #5
    Unregistered
    Guest

    Why did it work ?

    Just curious... why did it work ?

  6. #6
    looking for the truth moemen ahmed's Avatar
    Join Date
    Feb 2002
    Location
    Egypt
    Posts
    161
    as I figured , your variables(which were long long int) needed to be casted into (int) as u r trying to print them as integer(%d)

    and by the way let me tell u something, its much better to use the ( cout) function from the (iostream.h) its much better and it ll work without the need of casting , unless ur programming c not c++, as printf is c-style ......

    as ur programming c++ try the iostream its much cooler
    Programming is a high logical enjoyable art for both programer and user !!

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. Weird printf() problem
    By stanlvw in forum C++ Programming
    Replies: 2
    Last Post: 02-23-2008, 02:29 AM
  3. Newb Help: Full Arrays and Functions
    By LycanGalen in forum C Programming
    Replies: 5
    Last Post: 01-31-2008, 08:35 PM
  4. Drawing tables in C
    By stanoman in forum C Programming
    Replies: 5
    Last Post: 10-09-2003, 11:14 AM
  5. Whats Wrong Whith This!?
    By SmokingMonkey in forum C++ Programming
    Replies: 8
    Last Post: 06-01-2003, 10:42 PM

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