Thread: run-time error :(

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    29

    Question run-time error :(

    Why do this code get a run-time error ?

    Thanks alot!

    Code:
    #include <stdio.h>
    #include <math.h>
    int main(int argc, char *argv[])
    {
     
        int num,i,root;
        float j=1;
           printf("Enter a 3 digit number\n");
      scanf("%d",num);
      root=sqrt(num);
    
      for (i = 2; j!=2 && i<=root; i++)
    {
            if (num%i==0)
            {
            j++;
    }
    }
            
        if (j==2)
           printf("%d is a prime number",&num);
        else
            printf("%d is not a prime number",&num);
            
      system("PAUSE");	
      return 0;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    scanf requires a pointer for an argument, a la scanf("%d", &num).

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    29
    Thank you!!
    you are a genius!!!

  4. #4
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Quote Originally Posted by Livnat View Post
    Thank you!!
    you are a genius!!!
    OK, come on now. tabstop is certainly well versed in C and C++, but "genius"....? lol
    Mainframe assembler programmer by trade. C coder when I can.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Todd, you're a genius too.

  6. #6
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Quote Originally Posted by robwhit View Post
    Todd, you're a genius too.
    Well then... with that, I think I'll take the rest of the week off, starting at 5pm today. woo-hoo!

    (Thanks rob - you're easily impressed too it seems!)

    (edit - And, this was my 1,000th post too... This is a special day.)
    Mainframe assembler programmer by trade. C coder when I can.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Todd, you're a genius too.
    I need to find a way to abduct tabstop and Todd Burch and make them my slaves, muahhaha!

    By the way, Livnat, if you did not notice, although scanf() requires pointer arguments, printf() does not, especially when the format specifier is &#37;d instead of %p.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Just remember: it takes a super-devious plan to abduct geniuses, since we can see right through the merely devious plans!

    Or cookies. Either way.

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Note that using a float variable for a flag to indicate whether or not a divisor is found is not a very good idea. First of all, comparing a float with an integral like 2 might not work properly, because of the way floating point numbers are represented. And then, of course, you're using extra memory because a float takes up more space than, say, an int.

    In other words, I would probably use an int (or a bool, if you feel C99-ish), and set it to either 0 (false) or 1 (true). Something like this.
    Code:
    int found = 0;
    
    while(found == 0) {
        /* ... */
        found = 1;
        /* ... */
    }
    
    if(found == 1) puts("found");
    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.

  10. #10
    Registered User
    Join Date
    May 2008
    Posts
    29
    Wow... This forum is great!
    Thank you all!
    I think I'll be around during this semester...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 05:45 AM
  5. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 08:57 AM

Tags for this Thread