Thread: Long Integers

  1. #16
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    looks fine, and if it runs and works then you're sorted. just as a matter of interest, what compiler are you using? system isn't normally in stdio.h. its not a big deal, just curiosity
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  2. #17
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Some fixes.
    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main(void)
    {
       char string[256];  
       long int a,b;                             
    
       printf( "Please enter your name:\t " );
    
       fgets ( string, 256, stdin );
       printf("Hello, %s\n", string);
       printf("You will enter two numbers, and I will raise the first number\n"); 
       printf("to the power of the second number!\n\n");
    
       printf("Enter the first number.\t");
       scanf("%ld", &a);
    
       printf("Enter the second number, which will be the exponent.\t");
       scanf("%ld", &b);
    
       printf("Your total is %ld\n", (long int)pow(a,b));
    
       return 0;
    }
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #18
    Registered User
    Join Date
    Jan 2006
    Posts
    26
    I am using the bloodshed dev whatever compiler. And the reason I have the beginning wrong is because once again, my teacher wants us to use that format. And for the return 0, my compiler will not pause the program unless I use that code.

    Edit:

    I'm sorry, it will pause the program, but my teacher wants us to "Press any key to end the program." The way I did it before (using getchar( )) would only end the program if I pressed Enter. The code I use now seems to be the easiest way to do that.

  4. #19
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by kabuatama
    And the reason I have the beginning wrong is because once again, my teacher wants us to use that format.
    Fine. I'm in the habit of not perpetuating bad habits. It's habit forming and bad.

    Quote Originally Posted by kabuatama
    And for the return 0, my compiler will not pause the program unless I use that code.
    Pause before the return?

    Quote Originally Posted by kabuatama
    The code I use now seems to be the easiest way to do that.
    Fine. When you get out of the class, remember to ditch this bad habit.

    -----

    Learn wrong. [school and bad books]
    Unlearn. [real life]
    Relearn correctly. [real life again]

    I wish they'd skip the first one or two steps and just teach correctly.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #20
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > my teacher wants us to use that format
    Who's paying who?
    As Dave has pointed out, if you think learning is hard, wait until you have to unlearn it.

    Your compiler is based on gcc, so you should be able to add these compiler options to get a lot more information about your code.
    Code:
    $ gcc -W -Wall -ansi -pedantic -O2 foo.c -lm
    foo.c:7: warning: return type of ‘main’ is not ‘int’
    foo.c: In function ‘main’:
    foo.c:21: warning: format ‘%d’ expects type ‘int *’, but argument 2 has type ‘long int *’
    foo.c:24: warning: format ‘%d’ expects type ‘int *’, but argument 2 has type ‘long int *’
    foo.c:29: warning: implicit declaration of function ‘system’

  6. #21
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by kabuatama
    And for the idiot teacher remark, yeah, I guess his teaching methods are a little weird. He did give us notes for this week, but the notes were on loops and if statements and conditionals.
    If so I would think that your teacher wants you to compute a to the power of b by yourself using long ints. like this
    Code:
        res = a;
        for ( ; b > 1; --b )
            res *= a;
        printf("Your total is %ld\n", res );
    checking for b < 1 needs to be added.
    Kurt

  7. #22
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    is the int necessary? I am really sorry for asking all these questions, but it is difficult just going by the book, and I thank everyone for taking the time to help me.
    put the int in for safety - standards and all that.
    The following are all exactly the same:
    Code:
    long int x;
    int long x;
    long x;
    signed long x;
    signed int long x;
    signed long int x;
    long signed int x;
    long int signed x;
    int long signed x;
    int signed long x;
    Take your pick. I use just plain long.

    It's the same with shorts. You can have the int keyword before the short, after, or absent.

    And the order of keywords doesn't matter. So you can do things like this:
    Code:
    extern const long long int ll;
    ->
    Code:
    long const int extern long ll;
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #23
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    foo.c:29: warning: implicit declaration of function ‘system’
    This is indicating that you need to include <stdlib.h> for system()'s prototype.

    In Dev-C++: Options->Compiler options (or something similar), and add
    Code:
    -W -Wall -ansi -pedantic -O2
    into the "Extra compiler options" textbox.

    Note that older versions of Dev-C++ find several thousand warnings in <stdio.h> and other standard header files. If that happens, use just this:
    Code:
    -W -Wall -O2
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #24
    Registered User
    Join Date
    Jul 2005
    Posts
    69
    I guess it's worth noting that when you use type 'long' with pow you can get inaccurate results. For example pow(5, 3) is likely to be displayed as 124. But hey, at least you know the general reason of why that could happen.

    p.s. actually when casting the result to long.

    Regards,
    Brian
    Last edited by Br5an; 01-28-2006 at 01:22 PM.

  10. #25
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    For example pow(5, 3) is likely to be displayed as 124.
    Only if you don't include <math.h>.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. long int type
    By sarahr202 in forum C++ Programming
    Replies: 4
    Last Post: 05-20-2009, 12:55 PM
  2. Dev-cpp - compiler options
    By tretton in forum C Programming
    Replies: 7
    Last Post: 01-06-2006, 06:20 PM
  3. Sorting Algorithms with Time
    By silicon in forum C++ Programming
    Replies: 3
    Last Post: 05-03-2005, 11:27 AM
  4. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 10:36 AM
  5. Replies: 6
    Last Post: 08-04-2003, 10:57 AM