Error in my program, help!

This is a discussion on Error in my program, help! within the C Programming forums, part of the General Programming Boards category; My program is supposed to take in an interger, and tell me if it's a prime number or not. If ...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    9

    Error in my program, help!

    My program is supposed to take in an interger, and tell me if it's a prime number or not. If I enter something like "woof", it'll tell me that it is invalid and does not work.

    My program is as follows:


    Code:
    #include<stdio.h>
    #include<math.h>
    #include<stdlib.h>
    
    int is_prime(int n)
    {
        int b;
        
        if(n<2)
            return 0;
        for(b = 2; b<=sqrt(n); b++)
        {
            if (n%b==0)
            {    
                return 0;
            }
        }
        return 1;    
    }
    
    int read_an_int(void)
    {
        int a, ret, c;
        
        while(1)
        {    
            ret = scanf("%d", &a);
            if(ret == EOF)
                return -999;
            if(ret == 1)
                return a;
            printf("Invalid input, please try again: \n");
            while((c = getchar()) != '\n' && c != EOF)
                ;
                continue;
        }
    }    
    
    int main(void)
    {
    
        int a, ret;
    
        while (1)
        {
            
            printf("Enter a number to test (negative number to stop): \n");
            ret = read_an_int();
            if(ret==-999)
                printf("End of file.");
                
            if(ret==a)
            {
                if (a<0)
                {
                    printf("Bye!\n");
                    break;
                }
    
                if(is_prime(a) == 1)
                    printf("%d is prime\n", a);
                else
                    printf("%d is not prime\n", a);
    
            }
        }
        return 0;
    }
    When I try to compile it, the printf statement works, but if I try a number, it won't tell me if it's prime or not. I tried to put in woof, and that does indeed tell me that what I entered is invalid and that I need to enter another number.
    In other words, it scans in my function called read_an_int, but for some reason it doesn't read in the part where I have to say if it's a prime number or not.
    like if(ret==a) and so on. Anyone know how I can fix it?

  2. #2
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,649
    Code:
    int a, ret;
    .
    .
    .
    if(ret==a)
    What do you think the value of a is here?

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    "a" in main() and "a" in read_an_int() are two completely seperate and unrelated local variables.

    Code:
    int main(void)
    {
     
        int a, ret; // uninitialized
     
        while (1)
        {
             
            printf("Enter a number to test (negative number to stop): \n");
            ret = read_an_int();
            if(ret==-999)
                printf("End of file.");
                 
            if(ret==a)
            {
    You are comparing ret to a, but what is a? It could be anything. Try this:

    Code:
    printf("main() a=%d\n", a);
    Before and after you call read_an_int(). Notice it is a random number, and unchanged after the call.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    Oct 2011
    Posts
    9
    I'm confused as to what I should declare "a" to be, because when I said in the first part of my function:

    Code:
    if(ret == EOF)            
    return -999;
    then at the main part of my function I used:

    Code:
    if(ret==-999)
    Why doesn't it work for a, when I put at the top (in my read_an_int function):
    Code:
    if(ret == 1)
                return a;

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    If you are expecting to be able to enter "woof" and have it not wreck your program, then you need to change how you read input. Consider reading the entire line with fgets and then checking it with sscanf to see if it is valid or not. Cprogramming.com FAQ > How do I get a number from the user (C)


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 13
    Last Post: 11-03-2010, 12:45 PM
  2. C Program... Program or Compiler error?
    By Zemira in forum C Programming
    Replies: 13
    Last Post: 12-02-2009, 07:59 PM
  3. Help with error in c program.
    By psapd in forum C Programming
    Replies: 2
    Last Post: 03-10-2009, 10:31 PM
  4. Replies: 18
    Last Post: 11-13-2006, 12:11 PM
  5. i'm trying to fix an error with my error messaging on my program
    By RancidWannaRiot in forum C++ Programming
    Replies: 10
    Last Post: 09-30-2003, 01:02 PM

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