Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Home » Tip Bank » C++
Language: C++
Expertise: Beginner
May 6, 1997

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 loop write 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 Pro
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date