devxlogo

Program fails because do … while loop needs braces

Program fails because do … while loop needs braces

Question:
I’m a beginner in C programming. I have Microsoft Visual C++ 4.0.

I’ve read the book Programming with C by Byron Gottfried.

Unfortunately, no matter which simple program I try to write, I always run into a mistake. The error I make doesn’t seem wrong, so I don’t know what to change …

I’d appreciate it if you could help me with this simple program and tell me what to change. I want to make it so that it inputs characters and stores them as other character (‘a’ as ‘b’, ‘b’ as ‘c’ …. etc.)The error is: (23) : error C2061: syntax error : identifier ‘i’

This is the program:

#include #include main(){	FILE *fpt;	int i;	char c;		/* open file enc.dat as write-only to store coded text */	fpt=fopen(“enc.dat”, “w”);	/* send to ASCII and code */		do		c=getchar();		i=c;		++i;		c=i;		putc(c, fpt);	while (c != ‘%’);	/* close the enc.dat */	fclose(fpt);}

Answer:
The problem is that you need braces around the body of your do … while loopwrite the do … while block as follows.

do	{		c=getchar();		i=c;		++i;		c=i;		putc(c, fpt);	}	while (c != ‘%’);
and you will be OK.

devx-admin

Share the Post: