0
#include <stdio.h>

int main()
{

    FILE *f1;
    int ch, i, n = 0;
    char q[500], opt[4][100];
    int corAns;

    f1 = fopen("C://Users//Lenovo//Desktop//fileInC1.txt", "a+");
    if (f1 == NULL)
    {
        printf("Error Opening File.");
        return 0;
    }
    else
    {
        while (n != 2)
        {
            n++;
            printf("\nQuestion: ");
            fgets(q, 500, stdin);

            for (i = 0; i < 4; i++)
            {
                printf("\nOption %d: ", i + 1);
                fgets(opt[i], 100, stdin);
            }

            printf("\nCorrect answer: ");
            scanf("%d", corAns);
            //program terminating here after only one iteration
            fprintf(f1, "{\nQ: \"%s\", \n\topt: [\"%s\", \"%s\", \"%s\", \"%s\"], \n\tCA: %d }", q, opt[0], opt[1], opt[2], opt[3], corAns);
            printf("\nData Written Successfully.");
        }
    }

    fclose(f1);

    return 0;
}

I have been trying to create a Javascript generator as you can see in the code.

The main problem i am getting is inside the while loop.

The while loop is terminating after only one iteration and the program not writting the data in the created file. The file already exists.

I am not getting where is the problem occuring.

  • 3
    One error: `scanf("%d", corAns);` -> `scanf("%d", &corAns);` – kaylum Apr 06 '21 at 06:12
  • 3
    Watch out for the compiler warnings and fix them. – prehistoricpenguin Apr 06 '21 at 06:13
  • 2
    [Treat your warnings as errors, and fix them properly](https://godbolt.org/z/59fzx63PE). – WhozCraig Apr 06 '21 at 06:14
  • thank you... But now the data is written but format is not acurate. The output is breaking just before closing "(quote). But I did not give any \n there. –  Apr 06 '21 at 06:18
  • I was expecting the format like a JS object. Can you solve that. –  Apr 06 '21 at 06:19
  • Hi, you don't seem to be initializing `n` before checking `n !=2` in while loop. What's your terminating condition for the while loop? – Rohan Kumar Apr 06 '21 at 06:23
  • Please give the **exact** input, expected result and actual result. Not just a summary description but exact data. – kaylum Apr 06 '21 at 06:23
  • @RohanKumar `n = 0` is at the top. – kaylum Apr 06 '21 at 06:24
  • @user14070533 Please read the [`fgets` manual](https://linux.die.net/man/3/fgets). Specifically: *If a newline is read, it is stored into the buffer*. – kaylum Apr 06 '21 at 06:26
  • @kaylum input is just easy, Entering data as the screen is asking. Pleaserun the code in your machine. you'll get it. Output should look like a javascript object. Every property should display the value in one line. Inside the object there should be only 3 lines. –  Apr 06 '21 at 06:27
  • @user14070533 I know input is easy. But we need to know what your test input is. We don't want to guess what you enter. You need to tell us that info. Please provide the requested info if you want further help. – kaylum Apr 06 '21 at 06:30
  • what is your name? micheal john sam rose 2 –  Apr 06 '21 at 06:40
  • Seems you need a string template engine. – prehistoricpenguin Apr 06 '21 at 06:53
  • `C://Users//Leno...` with duplicated slashes is incorrect. You would need to 'escape' back-slashes as in `C:\\Users\\Leno...` – Weather Vane Apr 06 '21 at 06:56
  • Please add the exact output and expected output for the input you've added. – Gerhardh Apr 06 '21 at 08:28
  • Maybe you should show expected output for at least 2 objects. – Gerhardh Apr 06 '21 at 08:30

1 Answers1

0

You need to cleanse your input of new-lines. You also had a redundant Else statement, and Scanf requires the address of a variable, not it's value.

This should work for you. You can check out this question here: Fgets skipping inputs, which I shamelessly copied.

#include <stdio.h>
#include <string.h>

int main()
{

    FILE *f1;
    int ch, i, n = 0;
    char q[500], opt[4][100];
    int corAns;

    int c;
    char *p;

    f1 = fopen("fileInC1.txt", "a+");
    if (f1 == NULL)
    {
        printf("Error Opening File.");
        return 0;
    }

    while (n != 2)
    {
      n++;
      printf("\nQuestion: ");
      fgets(q, 500, stdin);

      if ((p=strchr(q, '\n')) != NULL) *p = '\0';

      for (i = 0; i < 4; i++)
      {
        printf("\nOption %d: ", i + 1);
        fgets(opt[i], 100, stdin);
        if ((p=strchr(opt[i], '\n')) != NULL) *p = '\0';
      }

      printf("\nCorrect answer: ");
      scanf("%d", &corAns);
      //program terminating here after only one iteration
      fprintf(f1, "{\nQ: \"%s\", \n\topt: [\"%s\", \"%s\", \"%s\", \"%s\"], \n\tCA: %d }", q, opt[0], opt[1], opt[2], opt[3], corAns);
      printf("\nData Written Successfully.");

      while ( (c = getchar()) != '\n' && c != EOF );
    }

    fclose(f1);

    return 0;
}
Any Day
  • 418
  • 2
  • 9
  • I tried to add a object limit instead of const number(2), But when I change 2 with limit variable by taking user input, the input for question didn't work...Can you do that too? Thanks in advance –  Apr 06 '21 at 13:08
  • You should check out the link in my answer. I think the answer there does exactly what you want. You should be aware, my answer *as-is* is **not** JSON compliant. Specifically, you're not setting commas between your objects. I don't want to add to much to the answer because I didn't want to make any assumptions for you. – Any Day Apr 06 '21 at 18:45
  • A quick hack would be to set the limit before you ask for the questions and add a `if(n>1 && n < limit) fprintf(f1, ",");` before the end of the While-Loop. – Any Day Apr 06 '21 at 18:53