code is not compiling!! urgent

Mar 7, 2014 at 10:50pm
Write your question here.
I'm using visual studio 2012, and the code seemed to be right but it's not compiling. anyone find the problem with this code??
[code]
Put the code you need help with here.
[#include<stdio.h>
#include<string.h>
#include<math.h>
bool isprime(int);
int countprime(int);
bool isprime(int n)
{
int i,j,k,f=0;
int d=sqrt(n);
for(i=2;i<=d;i++)
{
if(n%i==0)
{
f=1;
i=i+d;
}
}
if(f==1)
return false;
else
return true;

}
int countprime(int n)
{
int i,j,k,count=0;
for(i=2;i<=n;i++)
{
int f=0;
int d=sqrt(i);
for(j=2;j<=d;j++)
{
if(i%j==0)
{
f=1;
j=j+n;
}
}
if(f==0)
count++;

}
return count;
}
int main()
{

int i,j,n;
bool k;
printf("Enter an integer :\n");
scanf("%d",&n);
while(n>=0)
{
if(n>1)
{
k=isprime(n);
if(k)
printf("%d is prime ? True\n",n);
else
printf("%d is prime ? False\n",n);
j=countprime(n);
printf("No of primes <= %d : %d\n",n,j);
}
else if(n==0)
{
printf("%d is prime ? False\n",n);
printf("No of primes <= %d : %d\n",n,0);
}
else if(n==1)
{
printf("%d is prime ? False\n",n);
printf("No of primes <= %d : %d\n",n,0);
}
printf("Enter another integer :\n");
scanf("%d",&n);
}
return 0;
}
/code]
Mar 7, 2014 at 11:05pm
Any error messages?

Aceix.
Mar 7, 2014 at 11:10pm
I just reformatted the code to make it easier to read.

If the compiler is going through the code top to bottom, I don't think it will know what n is in the functions (or it will have a garbage uninitialized value). I'd order the code like this:

- function prototypes
- main
- functions


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include<stdio.h>
#include<string.h>
#include<math.h>

bool isprime(int);
int countprime(int);


bool isprime(int n)
{
	int i,j,k,f=0;
	int d=sqrt(n);
	
	for(i=2;i<=d;i++)
	{
		if(n%i==0)
		{
			f=1;
			i=i+d;
		}
	}

	if(f==1)
		return false;
	else 
		return true;
}


int countprime(int n)
{
	int i,j,k,count=0;
	for(i=2;i<=n;i++)
	{
		int f=0;
		int d=sqrt(i);
		
		for(j=2;j<=d;j++)
		{
			if(i%j==0)
			{
				f=1;
				j=j+n;
			}
		}

		if(f==0)
			count++;

	}

	return count;
}


int main()
{

	int i,j,n;
	bool k;

	printf("Enter an integer :\n");
	scanf("%d",&n);

	while(n>=0)
	{
		if(n>1)
		{
			k=isprime(n);

			if(k)
				printf("%d is prime ? True\n",n);
			else
				printf("%d is prime ? False\n",n);

			j=countprime(n);

			printf("No of primes <= %d : %d\n",n,j);
		}

		else if(n==0)
		{
			printf("%d is prime ? False\n",n);
			printf("No of primes <= %d : %d\n",n,0);
		}

		else if(n==1)
		{
			printf("%d is prime ? False\n",n);
			printf("No of primes <= %d : %d\n",n,0);
		}

		printf("Enter another integer :\n");
		scanf("%d",&n);
	}

	return 0;
}
Last edited on Mar 7, 2014 at 11:14pm
Mar 7, 2014 at 11:18pm
@Aceix
my engineer major friend fixed it, he said it was overloaded but thank you everythings over

@wildblue
how do I write the code like you?? I don't know how to do it on this site QQ
Mar 7, 2014 at 11:23pm
how do I write the code like you?? I don't know how to do it on this site QQ


I just copied your code into a text editor and put in tabs to indent the code. I'd do the same thing in Visual Studio - tab to indent code. Then when it's copied to the forum and put it between the code tags, it keeps the formatting.
Mar 7, 2014 at 11:24pm
@wildblue
how do I write the code like you?? I don't know how to do it on this site QQ

Code tags: http://www.cplusplus.com/articles/jEywvCM9/

Aceix.
Mar 7, 2014 at 11:25pm
qwerty you are missing the '[' in your code tags that is why yours is plain old text.
Topic archived. No new replies allowed.