Thread: Long Integers

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    26

    Long Integers

    Are these just regular numbers, such as 1, 2 ,3, etc.? Or are they something different? I need to make a code that accepts two long integers, then raises one to the power of the other.

    Edit:

    I figured out something like this

    Code:
    #include <stdio.h>
    #include <math.h>
    
    
    void main(void)
    
    {
         int a,b;
         
         printf("Please Enter a Number.\t");
         scanf("%d", &a);
        
               
         printf("\nPlease Enter a Second Number.\t");
         scanf("%d", &b);
         
         pow(a, b);
        
               
    system("Pause");                      
    
    
    
    
    }

    but I need it to display the number. How would I do that? Also, I need to ask for someone's name, and then display it. I know it has something to do with strings, but I am not sure. Any help?
    Last edited by kabuatama; 01-27-2006 at 04:25 PM.

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    It's ashame you aren't using long integers.

    This looks like an assignment... if it is, I'd have to say... open your book and read it. Maybe don't sleep in class tomorrow, either.
    Sent from my iPadŽ

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    26
    Unfortunately, I do not go to classes for my class. Basically I have an assignment due each week, and I am on my own to learn it. I have looked in the book, and I know that long integers are whole numbers, but have a greater range than regular integers? I am not asking anyone to do the assignment for me, but help would be appreciated.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Quote Originally Posted by kabuatama
    I know that long integers are whole numbers, but have a greater range than regular integers?
    correct, they are also defined like so
    Code:
    long a, b;

    read up on the printf statement particularly use of the %d escape sequence.
    Last edited by Quantum1024; 01-27-2006 at 05:09 PM.

  5. #5
    Registered User
    Join Date
    Jan 2006
    Posts
    26
    Here is my code so far. I got the string part to work, but am still having trouble with the exponents. What am I doing wrong?
    Code:
    #include <stdio.h>
    #include <math.h>
    
    
    void main(void)
    
    {
             char string[256];                               
    
        printf( "Please enter your name:\t " );
    
        fgets ( string, 256, stdin );
        printf("Hello, %s", string);
           
    long a,b;
    
    printf("Enter first number\t");
    scanf("%d", &a);
    
    printf("Enter second number\t");
    scanf("%d", &b);
    
    printf("Your total is %d\n", pow(a,b));
        
               
    system("Pause");
    Do I use something other than %d for the printf section? Or does long use something entirely different? Sorry for all the questions.

  6. #6
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    The reason I'm ragging on you is because I feel there is no possible way your book doesn't explain this. I'm not even sure how you made it to this forum without at least at some point have come in contact with a long datatype. You're either not paying attention or you have really bad memory.

    As for an answer, Quantum, pretty much gave you all you need to know. So there.
    Sent from my iPadŽ

  7. #7
    Registered User
    Join Date
    Jan 2006
    Posts
    26
    I am truly sorry, but I have never come across a long datatype before. My book does not explain this, so I have been checking numerous websites and the FAQs on this site to try to better understand this. Although I understand that you may be annoyed by my newbish questions, but if you could point me in the right direction, it would help me a lot.

  8. #8
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    As I said, Quantum told you everything you need to write the program.
    Sent from my iPadŽ

  9. #9
    Registered User
    Join Date
    Jan 2006
    Posts
    26
    Yes, and I wrote the program. I do not understand why I keep getting a value of "0;" it should work.

  10. #10
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    oh for gods sake throw him/her a bone!!! we were all n00bs once, some with better teachers than others, and some with better investigation skills than others too.

    as stated, look up format specifiers. as a general rule of thumb, most functions in math.h take arguments which are doubles, and also return doubles. pow is one such function, so unless there is a requirement to use long, i would use doubles when passing arguments to pow. either is fine, you will just get a compiler warning relating to truncation. look up printf escape sequence for double...
    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

  11. #11
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    couple of other things: as regurgitated ad nauseum in this forum, the return type of main is int - use it. also, that code won't compile because there is a missing brace at the end, you may not have selected all of your code when pasting it in, but in case you didnt...

    also, declare all your variables at the start of the program, up where you declare your array
    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

  12. #12
    Registered User
    Join Date
    Jan 2006
    Posts
    26
    Well, my teacher wants us to use the Void Main(void), and also I did forget the last brace when I posted my code. It compiled, but I kept getting an answer of "0." Now, I figured out the long, but can only make it work while using a long double escape sequence, "%lf." Is there a way to make it work using a long integer? Finally, I have the code posted as
    Code:
    long int a,b;
    ,

    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.

  13. #13
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    put the int in for safety - standards and all that.

    %lf is for doubles - it stands for long float (double).

    to make if work with %d, when you call pow, make a cast:

    (long int)pow(a,b);
    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

  14. #14
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    ps. your teacher is an idiot for telling you to use void main - its non standard and although it probably works on your type of compiler, your teacher (if he/she has any professional competence) should at least explain to you that if you ever find yourselves coding under a different compiler void may/may not work...
    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

  15. #15
    Registered User
    Join Date
    Jan 2006
    Posts
    26
    It works! Thank you so much! 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. Here I am posting my code; can you just make sure everything looks correct?

    Code:
    #include <stdio.h>
    #include <math.h>
    
    
    void 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("%d", &a);
    
    printf("Enter the second number, which will be the exponent.\t");
    scanf("%d", &b);
    
    printf("Your total is %ld\n", (long int)pow(a,b));
        
               
    system("Pause");                      
    
    
    
    
    }
    Thanks again for everyone's help!

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