Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Look guys...i must create a programm that reads 2 numbers from the first line of text file named: function.in and then create a new file named: function.out and write inside this all the prime numbers inside the two that i been given...here is an example: if the programm takes 1 and 4 it will accually check 2 and 3.
Here is my code:
C++
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
int checkprime (int num)


{
 int counter = 2;
  while (1)
   {
    if (!(num %  counter)) return 0;
    if ((num/counter) < counter) return counter;
    counter ++;
   }
}

int main(){
    FILE *fp;
    int n, m, number, small, big;
    
    fp=fopen("function.in","r");
    fscanf(fp, "%d %d", &n, &m);
    fclose(fp);
    if(n>m)
    {
    small=m;
    big=n;
    }
    else
    {
    small=n;
    big=m;
    }
    
    fp=fopen("function.out","w+");
    for(number=small+1; number<big;>                           if (checkprime (number)){
                                          fprintf(fp,"%d ", number);
                                          }
    }
    fclose(fp);
}


It accually runs very well..but if the largest is number is 1500 or more the output is like glyphs and i dont know why..but if it is 1000 or less it is ok.
Please help me as soon as possible.
Thanks!!!!
Posted
Updated 10-Oct-11 3:28am
v2

Try replacing
C++
fprintf(fp,"%d ", number);
with
C++
fprintf(fp," %d", number);
and Notepad will display it correctly.

Notepad has a problem with the long length of the line and the final space at the end of it, don't ask me why.
If you open your original 'corrupted' file in Wordpad it is displayed correctly.
 
Share this answer
 
Use long instead of int as you are getting overflows.
 
Share this answer
 
You may solve this problem by using float or long in place of int.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900