Thread: Why is this code not working?

  1. #1
    Registered User
    Join Date
    Apr 2014
    Location
    Pakistan
    Posts
    7

    Question Why is this code not working?

    Hi there guys. Im new to the forum and wanted to ask a question.
    I wrote a piece of code which basically takes a number and prints out all of its factors also stating which ones are prime and which are not. When I run this code, Windows says that the program is not responding. Can anyone tell me why isn't it working?
    It compiles and runs but when I input the number, it crashes...
    I used Code blocks as a compiler.
    Attached Files Attached Files

  2. #2
    Registered User
    Join Date
    Mar 2012
    Location
    the c - side
    Posts
    373
    Dividing by zero is something to avoid at all costs. It makes computers a tad queasy.

    Code:
    answer = number / x;

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    1. You should post your code here, using code tags.

    2. You need to make sure your code is well formatted - there appear to be a few attempts, but overall, it is lacking. It should look more like this:

    Code:
    #include <stdio.h>
    
    int check(int n);
    
    int main()
    {
        int number;
        int x;
        int answer;
    
        scanf("%d", &number);
    
        for(x=0; x<number; x++)
        {
            answer = number / x;
    
            if(answer * x == number)
            {
                printf("\n %d", answer);
            }
    
            if(check(answer) == 1)
            {
                printf(" ---- PRIME");
            }
            else
            {
                printf(" ---- NOT PRIME");
            }
        }
    
        getch();
    }
    
    
    check(n)
    {
        int c = 2;
    
        for ( c = 2 ; c <= n - 1 ; c++ )
        {
            if ( n%c == 0 )
            {
                return 0;
                break;
            }
        }
       
        if ( c == n )
        {
            return 1;
        }
    }
    3. Heed all compiler warnings and errors:

    Code:
    ||=== scrap_c, Debug ===|
    main.c||In function 'main':|
    main.c|26|warning: implicit declaration of function 'getch'|
    main.c|30|warning: return type defaults to 'int'|
    main.c||In function 'check':|
    main.c|30|warning: type of 'n' defaults to 'int'|
    main.c|46|warning: control reaches end of non-void function|
    ||=== Build finished: 0 errors, 4 warnings ===|
    >> warning: implicit declaration of function 'getch'

    This is a non-standard function. Consider using "getchar()" instead.

    >> warning: return type defaults to 'int'
    >> warning: type of 'n' defaults to 'int'

    This is because your function definition is wrong - it should look just like the declaration (up top), only without the semi-colon.

    >> warning: control reaches end of non-void function

    This means there might be control paths in your function that do not lead to a "return", though that function claims to always be returning a value.

    --------

    gemera already gave you key information pertaining to your problem, so you now have quite a bit to go on with.

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Fixed you

    Quote Originally Posted by Owais View Post
    I used Code blocks as a
    your IDE.

    Please learn the name and version of your compiler.
    Learn to turn on warnings.
    Learn to read the warings.
    Learn to post the warnings in the build log. http://wiki.codeblocks.org/index.php...ler_problem.3F
    Learn to understand the warnings.

    If you do all of the above you will be better than 90% of the newbie programmers on this site.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Fixed your statement, I used Code blocks as a your IDE.

    Please learn the name and version of your compiler.
    Learn to turn on warnings.
    Learn to read the warnings.
    Learn to post the warnings in the build log. http://wiki.codeblocks.org/index.php...ler_problem.3F
    Learn to understand the warnings.

    If you do all of the above you will be better than 90% of the newbie programmers on this site.

    Edit: Learn to use code tags when posting code.

    Tim S.
    Last edited by stahta01; 04-02-2014 at 12:32 PM. Reason: grammar and formatting
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  6. #6
    Registered User
    Join Date
    Apr 2014
    Location
    Pakistan
    Posts
    7
    I guess that is it. A simple bug that made me rip my hair out... I actually never thought that could be a problem. Thank you guys.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can someone tell me why this code isn't working please?
    By psynt555 in forum C Programming
    Replies: 5
    Last Post: 04-24-2012, 05:14 PM
  2. code not working as it should..
    By transgalactic2 in forum C Programming
    Replies: 21
    Last Post: 12-05-2008, 02:10 PM
  3. working set code
    By chinu in forum C++ Programming
    Replies: 6
    Last Post: 04-13-2008, 08:44 PM
  4. Why is the above code working???
    By chottachatri in forum C++ Programming
    Replies: 2
    Last Post: 02-22-2008, 07:29 AM
  5. Non-Working Code
    By SheilahT in forum C++ Programming
    Replies: 9
    Last Post: 09-19-2006, 04:04 PM

Tags for this Thread