i have made a program with this algorithm but its not working properly my code is given below can you help me to remove the errors

#include<stdio.h>
#include<conio.h>

void main()
{
	FILE *f;
	char ch;
	int nalpha=0,nword=0,nno=0,nsc=0;
	f=fopen("abc.txt","r");
	if(f!='NULL')
	{
		while(ch=getch(f)!=EOF)
		{
			printf("%c",ch);
			if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z'))
				nalpha++;
			else if((ch==' ')||(ch=='.'))
				nword++;
			else if(ch>='0'&&ch<='9')
				nno=nno+1;
			else
				nsc++;

		}
	}
	printf("no.of alphabets is %d",nalpha);
	printf("no.of words is %d",nword);
	printf("no.of numbers is %d",nno);
	printf("no.of other characters is %d",nsc);
	fclose(f);
	getch();
}

Recommended Answers

All 4 Replies

that all depends on what the errors are since you didnt bother to tell us what they are we dont know what to look for you cant just drop code on us an say fix it you have to xeplain what the problems are because we arent compilers were human

i have made a program with this algorithm but its not working properly my code is given below can you help me to remove the errors

There's only one compiler error: You can't call getch with any parameters, like you are when you say getch(f) . The only call you can make is getch() . But the real error is that you can't use getch to read from files. You probably want fgetc instead.

Other problems that you should correct: void main() isn't proper C; it should be int main() --with a matching return at the end, of course. if(f != 'NULL') is wrong. NULL isn't a character constant, it's a symbol defined in stddef.h . You meant if(f != NULL) . while(ch = fgetc(f) != EOF) is potentially confusing. You might want to write while((ch = fgetc(f)) != EOF) , which makes it clear that you're comparing the result of the assignment with EOF , instead of assigning a truth value.

that all depends on what the errors are since you didnt bother to tell us what they are we dont know what to look for you cant just drop code on us an say fix it you have to xeplain what the problems are because we arent compilers were human

sorry for some disturbance .the errors are:
1)character constant must be two or three chars long
2)cannot convert int to file*
3)extra parameter in call to getch
4)possibly incorrect assignment

There's only one compiler error: You can't call getch with any parameters, like you are when you say getch(f) . The only call you can make is getch() . But the real error is that you can't use getch to read from files. You probably want fgetc instead.

Other problems that you should correct: void main() isn't proper C; it should be int main() --with a matching return at the end, of course. if(f != 'NULL') is wrong. NULL isn't a character constant, it's a symbol defined in stddef.h . You meant if(f != NULL) . while(ch = fgetc(f) != EOF) is potentially confusing. You might want to write while((ch = fgetc(f)) != EOF) , which makes it clear that you're comparing the result of the assignment with EOF , instead of assigning a truth value.

thanks alot :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.