Let's say you type a and press Enter.
When you do that, there are two characters in the input stream: 'a' and '\n'. The first scanf reads the 'a' into ch and the second scanf reads the '\n' into opt. That is the source of your problem.
You'll have to write code to read and discard the newline character. Here's one way to do it.
while(opt=='y' || opt =='Y')
{
scanf("%c",&ch);
fputc(ch,fp);
// Assuming that no more than one characte is entered per line,
// read the discard the newline.
fgetc(stdin);
printf("want to enter more characters(y or n):");
scanf("%c", &opt);
// Read and discard the newline again.
fgetc(stdin);
}
If you want to be a bit more flexible about your input, you can use:
// Make it as large as you need it to be.
#define LINE_LENGTH 200
int main()
{
char line[LINE_LENGTH];
FILE *fp;
fp=fopen("myfile.txt","w");
// Read a line of text.
while( fgets(line, LINE_LENGTH, stdin) != NULL )
{
// Print the line to the output file
fputs(line, fp);
printf("want to enter more characters(y or n):");
// Read the entire line again.
if( fgets(line, LINE_LENGTH, stdin) != NULL )
{
// If the entire line is just "n" or "N", break from the loop.
if ( strcmp(line, "n\n") == 0 || strcmp(line, "N\n") == 0 )
{
break;
}
}
}
fclose(fp);
}