-2

Here is the code :

char specialKeys(char key, char *File)
{

    cout << _key << endl;

    FILE *OUTPUT_FILE;
    OUTPUT_FILE = fopen(file, "a+");




    if (key == VK_ABNT_C1)
        fprintf(OUTPUT_FILE, "%s", "Abnt C1")
        fclose(OUTPUT_FILE);
        return -2;

    else if (key == VK_ABNT_C2)
        fprintf(OUTPUT_FILE, "%s", "Abnt C2")
        fclose(OUTPUT_FILE);
        return -2;

    else if (key == VK_ADD)
        fprintf(OUTPUT_FILE, "%s", "Numpad +")
        fclose(OUTPUT_FILE);
        return -2;

    else if (key == VK_ATTN)
        fprintf(OUTPUT_FILE, "%s", "Attn")
        fclose(OUTPUT_FILE);
        return -2;

    else if (key == VK_BACK)
        fprintf(OUTPUT_FILE, "%s", "[Backspace]")
        fclose(OUTPUT_FILE);
        return -2;

    else if (key == VK_CANCEL)
        fprintf(OUTPUT_FILE, "%s", "[Break]")
        fclose(OUTPUT_FILE);
        return -2;

    else if (key == VK_CLEAR)
        fprintf(OUTPUT_FILE, "%s", "[Clear]")
        fclose(OUTPUT_FILE);
        return -2;

    else if (_key == VK_TAB)
        fprintf(OUTPUT_FILE, "%s", "[TAB]");
        fclose(OUTPUT_FILE);
        return -2;

    else if (_key == VK_SHIFT)
        fprintf(OUTPUT_FILE, "%s", "[SHIFT]");
        fclose(OUTPUT_FILE);
        return -2;

    else if (_key == VK_CONTROL)
        fprintf(OUTPUT_FILE, "%s", "[CONTROL]");
        fclose(OUTPUT_FILE);
        return -2;

    else if (_key == VK_ESCAPE)
        fprintf(OUTPUT_FILE, "%s", "[ESCAPE]");
        fclose(OUTPUT_FILE);
        return -2;

    else if (_key == VK_END)
        fprintf(OUTPUT_FILE, "%s", "[END]");
        fclose(OUTPUT_FILE);
        return -2;

    else if (_key == VK_HOME)
        fprintf(OUTPUT_FILE, "%s", "[HOME]");
        fclose(OUTPUT_FILE);
        return -2;

    else if (_key == VK_LEFT)
        fprintf(OUTPUT_FILE, "%s", "[LEFT]");
        fclose(OUTPUT_FILE);
        return -2;

    else if (_key == VK_UP)
        fprintf(OUTPUT_FILE, "%s", "[UP]");
        fclose(OUTPUT_FILE);
        return -2;

    else if (_key == VK_RIGHT)
        fprintf(OUTPUT_FILE, "%s", "[RIGHT]");
        fclose(OUTPUT_FILE);
        return -2;

    else if (_key == VK_DOWN)
        fprintf(OUTPUT_FILE, "%s", "[DOWN]");
        fclose(OUTPUT_FILE);
        return -2;

    else if (_key == 46)
        fprintf(OUTPUT_FILE, "%s", ".");
        fclose(OUTPUT_FILE);
        return -2;

    else if (_key == 1)
        fprintf(OUTPUT_FILE, "%s", "");
        fclose(OUTPUT_FILE);
        return -2;

    else
        fclose(OUTPUT_FILE);
        return -1;



    return 0;

}

I want that when I pass file pointer and char it check from the above case that it is true or not and give an indicator or if it's not then also give an indicator like if char not match any if else case then it returns -1 and if it is match in if else case then it will write the same in file close the file and return 1, thanks in Advance Guys.

John3136
  • 28,809
  • 4
  • 51
  • 69
  • C++ is not Python. Indenting code lines means nothing in C++. Block operations are controlled by `{` and `}` in C++. What's strange is that you did the correct thing here: `char specialKeys(char key, char *File) {`. Note the `{`. – PaulMcKenzie Jul 14 '21 at 03:26
  • Also off-topic, but you could have simply put the key value and text to write in a lookup table or map, thus removing those endless set of `if` statements. – PaulMcKenzie Jul 14 '21 at 03:32
  • [See this small example](http://coliru.stacked-crooked.com/a/0b9658c8e550f005). A good programmer would know that things could be done differently if they're writing the same thing over and over again, and thus would think of a more convenient and less-error prone way to write the code. – PaulMcKenzie Jul 14 '21 at 03:53

1 Answers1

2

You need brackets to properly scope your if/else statements

    if (key == VK_ABNT_C1) {
        fprintf(OUTPUT_FILE, "%s", "Abnt C1")
        fclose(OUTPUT_FILE);
        return -2;
    }
robthebloke
  • 9,331
  • 9
  • 12