Link to home
Start Free TrialLog in
Avatar of jenmaz
jenmaz

asked on

Having Trouble with my C Script that determines Prime and Perfect numbers HELP!:)

I am having problems making this script below work.  I want this script to check the numbers input from file imput.txt and tell me if they are Prime or Perfect.  (example numbers would be 110, 7, 10, 6 etc.  New to C and trying to learn it by doing these types of excersizes.  Can someone tell me what I am doing wrong?

Thanks in advance,

Jennifer  SEE SCRIPT BELOW:)


# include <stdio.h>
# include <math.h>

int main ()
{
       FILE *inp;
       FILE *outp;
     int num, total, i;
     float ans;
       int IsPrim;


//Initialize
     num = 0;

     do
          {
              inp=fopen("C:input.txt", "r");
              outp=fopen("C:output.txt", "w");

              fscanf(inp, "%d", num);
              printf(outp, "%d", num);

          /* printf("\nEnter a number from 1 to 1000>");
          scanf("%d", num); */
          }
     while (num<=0 || num>=1001);

//Initialize
     total = 0;
     ans = 0;

// For prime
    ans = 1;
    IsPrim = 1;

    for(i = 2; i < num; i++);
       if ( num % i == 0)
       {
          IsPrim=0;
          printf("Divisor of prime");
       }
    if(IsPrim)
       printf("It is Prim");

// For perfect

         total = 0;
         for ( i = 1; i < num; i++)
             if(num%i==0)
                total+=i;
         if(total==num)
         {
             printf("Perfect");  
             for ( i = 1; i < num; i++)
                if(num%i==0)
                   printf("Perfect divisor");
         }

             
fclosef (inp);
fclosef (outp);
return (0);

}
Avatar of avizit
avizit

>>
 do
          {
            inp=fopen("C:input.txt", "r");
            outp=fopen("C:output.txt", "w");

            fscanf(inp, "%d", num);
            printf(outp, "%d", num);

          /* printf("\nEnter a number from 1 to 1000>");
          scanf("%d", num); */
          }
     while (num<=0 || num>=1001);
<<

this will terminate the loop for numbers like 1 , 2, 3 ...etc

as the number is not <=0 and >= 1001

so your program will exit the loop after the first number ...
Avatar of jenmaz

ASKER

Thanks for the comments.  I think I understand.  So I would need to take the while (num<=0 || >=1001) at the end of the entire program and make the Perfect LOOP as well as the PRIME check loop as functions and do a function call?

Thanks in advance,
Jennifer
ASKER CERTIFIED SOLUTION
Avatar of avizit
avizit

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of sunnycoder
sunnycoder
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jenmaz

ASKER

Thanks Sunnycoder..  I am almost getting it but I am having problems with the fclosef statement.  

Basically I would like to read numbers from an imput.txt files.  Any numbers and then tell the user that the number read was prime or a perfect number and the program would run until the there are no more numbers or if the number read is -999.  After I get this running then I want to figure out how to make the Prime and Perfect a function and just place a function call in the loop.  Hope this makes sense.  I am trying to understand it.  Think I am getting it.



My code is below.... The error I am getting when compiling is this.

--------------------Configuration: perfectnumber - Win32 Debug--------------------
Compiling...
perfectnumber.c
C:\Program Files\Microsoft Visual Studio\MyProjects\perfectnumber\perfectnumber.c(60) : warning C4013: 'fclosef' undefined; assuming extern returning int
C:\Program Files\Microsoft Visual Studio\MyProjects\perfectnumber\perfectnumber.c(64) : fatal error C1004: unexpected end of file found
Error executing cl.exe.

perfectnumber.exe - 1 error(s), 1 warning(s)

--------------------
--------------------



# include <stdio.h>
# include <math.h>

#define SENTINEL -999

int main ()
{
       FILE *inp;
       FILE *outp;
     int num, total, i, number, prime;
     float ans;
       int IsPrim;


//Initialize
     number = 0;
       prime = 0;

       inp=fopen("C:input.txt", "r");
       outp=fopen("C:output.txt", "w");

     do
          {

              fscanf(inp, "%d", &num);
              fprintf(outp, "%d", &num);

          //Initialize
     total = 0;
     ans = 0;

// For prime
    ans = 1;
    IsPrim = 1;

    for(i = 2; i < num; i++);
       if ( num % i == 0)
       {
          IsPrim=0;
          printf("Divisor of prime");
       }
    if(IsPrim)
       printf("It is Prim");

// For perfect

         total = 0;
         for ( i = 1; i < num; i++)
             if(num%i==0)
                total+=i;
         if(total==num)
         {
             printf("Perfect");  
             for ( i = 1; i < num; i++)
                if(num%i==0)
                   printf("Perfect divisor");

          }while(number != SENTINEL);

fclosef (inp);
fclosef (outp);
return (0);
              }


fclose()  and not fclosef()
function is fclose and not fclosef :-)
that was faaaast avizit
>fprintf(outp, "%d", &num);

I told you

"notice the & (address of operator) infront of num ... note that it is required only for scanf family of functions and not printfs ..."

pls read the previous post carefully http:#12441280
Avatar of jenmaz

ASKER

Thanks
hehe sunny :)

welcome back from hibernation :)
thnx :-)
Avatar of jenmaz

ASKER

One more little silly question... When compiling I am getting an error

C:\Program Files\Microsoft Visual Studio\MyProjects\perfectnumber\perfectnumber.c(64) : fatal error C1004: unexpected end of file found

Which it points to this code below.  I  know this is a silly mistake that I am missing

return (0);
            }
check if your opening and closing braces and paranthesis in the program are balanced
       if(total==num)
         {
             printf("Perfect");  
             for ( i = 1; i < num; i++)
                if(num%i==0)
                   printf("Perfect divisor");

          }while(number != SENTINEL);

here is the imbalance ... if closes with do while :-)
one more thing i just notice

>>> for(i = 2; i < num; i++);

the above is a empty for loop which doesnt really accomplish anything
normally you will have

for(i=XX; i< YY; i++ ){

/*** some statements here ***/



....
....

}

Avatar of jenmaz

ASKER

Thanks Guys,

I am not focused tonight and am giving up for the night.  I got all the issues and resolutions that you all gave me accept the last comment from avisit.  I actually was trying to create a loop to check the input data to see if it is prime.  After all the cut and pasting I think I lost part of that code for that loop.  It did work before I added everything else.  At this time the script below executes but then closes as soon as the black box opens.  The error I get is from the fscanf or something.  So the black window does not really execute even though there is 0 errors when compiling.  I really wanted to get the prime and perfect part working so that I can go ahead and try to make functions out of them.  ANY HELP IN REVIEWING THE CODE would be apreciated.

I am a beginner.  I will give all the points I can to whomever can resolve this for me.:)

# include <stdio.h>
# include <math.h>

#define SENTINEL -999

int main ()
{
       FILE *inp;
       FILE *outp;
     int num, total, i, number, prime;
     float ans;
       int IsPrim;


//Initialize
     number = 0;
       prime = 0;

       inp=fopen("C:input.txt", "r");
       outp=fopen("C:output.txt", "w");

     do
          {

              fscanf(inp, "%d", &num);
              fprintf(outp, "%d", num);

          //Initialize
     total = 0;
     ans = 0;

// For prime
    ans = 1;
    IsPrim = 1;

    for(i = 2; i < num; i++)
       if ( num % i == 0)
       {
          IsPrim=0;
          printf("Divisor of prime");
       }
    if(IsPrim)
       printf("It is Prim");

// For perfect

         total = 0;
         for ( i = 1; i < num; i++)
             if(num%i==0)
                total+=i;
         if(total==num)
             printf("Perfect");  
             for ( i = 1; i < num; i++)
                if(num%i==0)
                   printf("Perfect divisor");

          }
             while(number != SENTINEL);
             
fclose(inp);
fclose(outp);
return(0);
              }
Are you sure it is crashing .. I do not see any scanf or getch to wait for pressing a char to exit !!! ... If you are using something line VC++, then it should give you the error message and wait for a key press on its own

>I am a beginner.  I will give all the points I can to whomever can resolve this for me.:)
be careful on those lines ... I would not like to answer further questions from an asker who chose to give all points to someone who posted only one last correction like say an extra ; while I was out for lunch ...

Points mean recognition of efforts, be careful about how you distribute them.
btw what avizit's last comment meant was

> for(i = 2; i < num; i++);
                                     ^
this ; terminates the loop and following statements are executed anyway ... remove that ;
>while(number != SENTINEL);
infinite loop ... number was initialized to 0 and never changed !!!! ... perhaps you wanted to use num here

while ( num != SENTINEL);
Avatar of jenmaz

ASKER

Sorry Sunnycoder for making that comment about the points.  I appreciate the correction.  I will make sure I remember that.  I am new and really did not know what big value the points had.
Avatar of jenmaz

ASKER

Found the comment.  When I execute the script it gives me no errors until it brings up the black box and then another window pops up and says Windows debug failed.  Line 54 fscanf.c
Then says that Expression: Stream != NULL
Avatar of jenmaz

ASKER

Still not working but thanks for all your help.
i think you have to change

 inp=fopen("C:input.txt", "r");
      outp=fopen("C:output.txt", "w");


to

 inp=fopen("C:\\input.txt", "r");
      outp=fopen("C:\\output.txt", "w");

Avatar of jenmaz

ASKER

Thanks.. I tried that and it still did not work.  I am sure it is something very silly that I am over looking.  It is driving me crazy!
Do you want to repost what you've got now with a brief description of the problem.

Paul
check if you are actually able to open the file "input.txt"
by doing the following ..



int main ()
{
      FILE *inp;
      FILE *outp;
     int num, total, i, number, prime;
     float ans;
      int IsPrim;


//Initialize
     number = 0;
      prime = 0;

      inp=fopen("C:input.txt", "r");     //or may be inp=fopen("C:\\input.txt", "r");
      outp=fopen("C:output.txt", "w");
      if(!inp){
                 printf("Unable to open file input.txt");
                  exit(1);
      }

......... //rest of you code
you are probably looping too many times. (as sunnycoder has already mentioned)

make sure your `fscanf' s do read some value.

if(fscanf(inp, "%d", &num) != 1)
    break;

-- Adil
Avatar of jenmaz

ASKER

Sure this what I have so far.  Like I stated above there were no errors when I compiled but an error when the black window came up stating something about a fscanf.c file and an Expression: != NULL.

I just wanted to have this script test numbers that were coming from an imput.txt file (ex. 3, 6, 91, 19, 11 etc.) and have the script tell  me whether or not the number is prime or not prime and/or a perfect number or not a perfect number.  Then after I got it working I would go back and clean it up by creating functions for Prime and Perfect etc.  I am still learning functions so I would save that for last.  I am just stuck right now.  Maybe cause it is late.

# include <stdio.h>
# include <math.h>

#define SENTINEL -999

int main ()
{
       FILE *inp;
       FILE *outp;
     int num, total, i, number, prime;
     float ans;
       int IsPrim;


//Initialize
     number = 0;
       prime = 0;
       num = 0;

        inp=fopen("c:\\input.txt", "r");
      outp=fopen("c:\\output.txt", "w");

       fscanf(inp, "%d", &num);
       fprintf(outp, "%d", num);

     do
          {

          //Initialize
     total = 0;
     ans = 0;

// For prime
    ans = 1;
    IsPrim = 1;

    for(i = 2; i < num; i++)
       if ( num % i == 0)
       {
          IsPrim=0;
          printf("Divisor of prime");
       }
    if(IsPrim)
       printf("It is Prim");

// For perfect

         total = 0;
         for ( i = 1; i < num; i++)
             if(num%i==0)
                total+=i;
         if(total==num)
             printf("Perfect");  
             for ( i = 1; i < num; i++)
                if(num%i==0)
                   printf("Perfect divisor");

          }
             while(num != SENTINEL);
             
fclose(inp);
fclose(outp);

return(0);
              }
call fscanf inside your do{}while loop
Avatar of jenmaz

ASKER

I tried the script to check to see if I can open my input.txt file and the black screen prints "Unable to open file input.txt"
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jenmaz

ASKER

--------------------Configuration: perfectnumber - Win32 Debug--------------------
Compiling...
perfectnumber.c
C:\Program Files\Microsoft Visual Studio\MyProjects\perfectnumber\perfectnumber.c(23) : warning C4013: 'strerror' undefined; assuming extern returning int
C:\Program Files\Microsoft Visual Studio\MyProjects\perfectnumber\perfectnumber.c(23) : error C2065: 'errno' : undeclared identifier
C:\Program Files\Microsoft Visual Studio\MyProjects\perfectnumber\perfectnumber.c(24) : warning C4013: 'exit' undefined; assuming extern returning int
Error executing cl.exe.

perfectnumber.exe - 1 error(s), 2 warning(s)
have you include <string.h> , <stdlib.h> and <errno.h> in the beginning of your source file?

please do that and see if it works
Avatar of jenmaz

ASKER

OK did that and the black screen says no such file or directory.  If that is the case then it is not reading that file from my c drive.  It is in my C drive.  I do not hava an a drive.  Should I try to put the file somewhere else and call it from there?
>> I tried the script to check to see if I can open my input.txt file and the black screen prints "Unable to open file input.txt"

Well in any case, the above simply shows that you are not able to open the file input.txt.
there can be various reasons for this, most common being:
1) you arent specifying the pathname to your file correctly.
2) you dont have permissions to read the file, or search the directories in the path.

try running the program in the same directory as in whch you have the file "input.txt",
and specify the fopen() call in your source file as follows.

       inp=fopen("input.txt", "r");
      outp=fopen("output.txt", "w");


i think it should work then,
van_dy
Avatar of jenmaz

ASKER

That did not work.  
Avatar of jenmaz

ASKER

This is what is in the imput.txt file ---

6 7 28 18 496 31 24 17 36 3 19 -999

Just numbers that I want the script to read to determine if they are prime or perfect and -999 stops and exits the program.
find the correct pathname to your file.  like supposse input.txt is in some folder name PROJECT in you C drive, then
the correct call to fopen will be

inp = fopen("c:\\PROJECt\\input.txt", "r");

do that and it will work for sure

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Actually, that's not a perfect solution but it will get you on your way once you solve the 'fopen' problem. It may be better to change your loop to a for loop but lets get it doing something first.

Paul
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
hello jen,

here is another reccommendation, after u can successfully open your
input file i would reccommend you to loop as follows

    ans = 1;
    IsPrim = 1;

    for(i = 2; i < num/2; i++);
       if ( num % i == 0)
       {
          IsPrim=0;
          printf("Divisor of prime");
       }
    if(IsPrim)
       printf("It is Prim");


well you  loop in the similar fashion for perfect numbers.
consider, if you can't device  37 by a number till 18, wats the point
testing its divisibility with numbers from 19 to 36 ?? so  test the divisibility
as prescribed above. there are ways to make the algorithm better, but i hope
you get it working at least as we reccommend
i <= num/2

for detecting primes, it suffices to test only upto sqrt(num)

-- Adil
Were you able to solve the problem ?
Are you still getting compilation errors?
May be the "include path" is not set properly in the environment.
Also, the correct dlls are not getting compiled!

-ssnkumar