run-time error :(

This is a discussion on run-time error :( within the C Programming forums, part of the General Programming Boards category; Why do this code get a run-time error ? Thanks alot! Code: #include <stdio.h> #include <math.h> int main(int argc, char ...

  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,185
    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
    Katy, Texas
    Posts
    2,309
    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
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  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
    Katy, Texas
    Posts
    2,309
    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.)
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,310
    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.
    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

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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,046
    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, 02: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


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