• C

# 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?

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);

}
###### Who is Participating?

Commented:
here is a rough skeleton

main(){

1. open files
2. Do{
if number >=1 and <=1000
check_if_prime(number);
if (prime)
print "prime"
else
print "not prime"
}while(!eof)

explanation :

1. first of all your conditon was wrong
>>>>> while (num<=0 || >=1001)
so only numbers <= 0 or numbers >= 1001 will satisfy the above condition . i.e numbers like -1 ,-2 or 10987867867868  :)
also you dont need the condition in the while loop as in that case , you wilexit the program when you encounter the first number which doesnt satisfy ( maybe that's what you want .. but anyway you need to be clear on this ..

so you need a condition like

while ((num >=1) && (num <=1000) )

but i think you dont need the condition in the "while" anyway cos in that case if your file has

10
12
-1
3
4
5
the loop will exit when -1 is encountered

hence i think you should use the "rough steps" given at top and check for the numbers inside the loop

Hope I make myself clear .and dint confuse you more
0

Commented:
>>
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 ...
0

Author Commented:
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?

Jennifer
0

Commented:
There are a number of issues with your program

# 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");        << These file operations have been included within the loop ... this means that in each iteration, file will be opened again and again and previous file handle/descriptor, overwritten .... this is not a very good idea and will lead to leaking file descriptors (yes they are a precious progamming resource) ... Move the file openeing to the beginning of the program, outside the loop .... Once you do this, you will be opening the files only once, at the beginning of the program and closing them before you exit ...

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

fscanf(inp, "%d", num);   << arguments to any scanf need not the variable name but variable address ... this statement should have been fscanf (inp, "%d", &num); ... notice the & (address of operator) infront of num ... note that it is required only for scanf family of functions and not printfs ...

printf(outp, "%d", num);  <<perhaps you wanted fprintf here

/* printf("\nEnter a number from 1 to 1000>");

scanf("%d", num); */   << again missing & ... also this will be read from keyboard and not file ... are you sure you want scanf and not fscanf here ?

}
while (num<=0 || num>=1001);  << note that this check is carried out at the end of the loop ... within the loop you are reading two values into variable num ... thus the first value is getting overwritten and this test will appply only to the second value read in the loop ... either you should be reading one value in the loop or you should be storeing them separately and checking them both ... Former is preferable

<<by the time you get out of the loop, you would have read several values into the same variable num, each read overwriting the previous value and processed none of them !!! So there was no point in reading those values ... you need to move these checks to inside loop .... If you have been reading a bit about functions, this might be the place to use them
//Initialize
total = 0;
ans = 0;

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

for(i = 2; i < num; i++);  << this loop can be terminated at a value atleast 1/2 num ....
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);

}

There might be more issues but this should be enough to get you going ... Obviously there are some compilation errors that you must have encountered while compiling this program ... it helps us a lot if you post all those EXACT error messages ...

cheers
sunnycoder
0

Author Commented:
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);
}
0

Commented:

fclose()  and not fclosef()
0

Commented:
function is fclose and not fclosef :-)
0

Commented:
that was faaaast avizit
0

Commented:
>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
0

Author Commented:
Thanks
0

Commented:
hehe sunny :)

welcome back from hibernation :)
0

Commented:
thnx :-)
0

Author Commented:
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);
}
0

Commented:
check if your opening and closing braces and paranthesis in the program are balanced
0

Commented:
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 :-)
0

Commented:
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 ***/

....
....

}

0

Author Commented:
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);
}
0

Commented:
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.
0

Commented:
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 ;
0

Commented:
>while(number != SENTINEL);
infinite loop ... number was initialized to 0 and never changed !!!! ... perhaps you wanted to use num here

while ( num != SENTINEL);
0

Author Commented:
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.
0

Author Commented:
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
0

Author Commented:
Still not working but thanks for all your help.
0

Commented:
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");

0

Author Commented:
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!
0

Commented:
Do you want to repost what you've got now with a brief description of the problem.

Paul
0

Commented:
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
0

Commented:
you are probably looping too many times. (as sunnycoder has already mentioned)

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

0

Author Commented:
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);
}
0

Commented:
call fscanf inside your do{}while loop
0

Author Commented:
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"
0

Commented:
ok that means you aRE not able to open the file.
the meaning of this is that either the path you specify
to open your file is not correct.. or you dont have the permission to open the file.
add the strerror(errno) part to what i told u and tell wats the error printed.

if(!inp){
printf("Unable to open file input.txt, %s\n", strerror(errno));
exit(1);
}

0

Author Commented:
--------------------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)
0

Commented:
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
0

Author Commented:
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?
0

Commented:
>> 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
0

Author Commented:
That did not work.
0

Author Commented:
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.
0

Commented:
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

0

Commented:
You are reading from the file ouitside the loop.

Change:

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

do
{

to:

do
{

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

Paul
0

Commented:
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
0

Commented:
Hi Jennifer,

It looks like you're checking only once, not for every number in your file.
You'd probably want to put the checks of each number into an extra function.

fscanf needs the address of a variable, not the variable itself, so instead of
fscanf(inp, "%d", num);
write
fscanf(inp, "%d", &num);

Put your number analysis in an extra function:

void check_num(FILE* out, int num){
int num, total, i;
//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");
}
}
(it's incomplete, but I'm sure you can complete it by yourself.)

You'd also want to use fprintf(out,...) instead of printf().

Cheers!

Stefan
0

Commented:
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
0

Commented:
i <= num/2

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

0

Commented:
Were you able to solve the problem ?
0

Commented:
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
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.