-2

i have been trying to input characters and the character should be put in the file unless users change the value of opt to 'n'.

#include<stdio.h>
void main()
{
    char ch,opt='y';
    FILE *fp;
    fp=fopen("myfile.txt","w");
    while(opt=='y' || opt =='Y')
    {   scanf("%c",&ch);
        fputc(ch,fp);
        printf("want to enter more characters(y or n):");
        scanf("%c",&opt);
    }
    fclose(fp);
}

So I want to give the inputs until opt value changes to 'n'.

max tech
  • 69
  • 1
  • 9

2 Answers2

-1

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);
}
R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • You assume that user will not enter more than 200 characters at a time(which is practically correct, off course). Also user will not get option to choose to `exit` after entering each character. – cse Feb 26 '18 at 06:17
  • @cse, with the second suggestion, the user has the option of entering one character at a time too. It offers the flexibility of entering more than one character at a time. – R Sahu Feb 26 '18 at 06:21
-1

If you are using Windows OS then you can use following code to read characters without pressing Enter. See here and here to know about reading characters without pressing Enter on Linux plateforms.

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

void main()
{
    char ch,opt='y';
    FILE *fp;
    fp=fopen("myfile.txt","w");
    while(opt=='y' || opt =='Y')
    {
        ch = getch();
        fputc(ch,fp);
        printf("%c\nWant to enter more characters(y or n):", ch);
        opt = getch();
        printf("%c\n", opt);
    }
    fclose(fp);
}

Note: You can modify the code if you don't need to print the last entered character. See here to know other variants of getch() function.

cse
  • 4,066
  • 2
  • 20
  • 37