char* error

https://www.youtube.com/watch?v=Px5kXp_t2-U

1
2
3
4
5
int save(int x, char* y){
   return 0;
}

save(6, "hello");


why is this not working? why is the code in the video works fine?
Where is your main function?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int main(){
    char i;
    
    while(1){
        for(i = 8; i <= 190; i++){
            if(GetAsyncKeyState(i) == -32767){
                save(i, "LOG.TXT");
            }
        }
    }
    
    system("pause");
    return 0;
}

int save(int key_stroke, char *file){
    std::cout << key_stroke << std::endl;

    return 0;
}
First you have to prototype your function, put this above main

int save(int key_stroke, char *file);

And I get an error here - GetAsyncKeyState(i) Becuase I dont have that function.
Last edited on
well my mistake. I usually don't post includes files and prototypes because I think that's just a waste of space but in this case, I guess I need to post it :)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <windows.h>
#include <Winuser.h>


int save(int key_stroke, char *file);


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

int save(int key_stroke, char *file){
    std::cout << key_stroke << std::endl;

    return 0;
}
Works perfectly Fine for me.

Input: 1,2,3,4,5.
Output - http://gyazo.com/f40582717db74dd3f13feb9bfcdfa50c
You might want to replace char *file with char file[], as well as replace the system("pause") with the _getch() included in the conio file.
I'm using MinGW. Do you guys think this is related to compiler? also, file[] won't work...
Im using mingw, and doing

file[]

worked perfectly for me.
Ok, it compiles fine if I use online compiler but whenever compiling with my compiler, it gives me this error.

 
warning: deprecated conversion from string constant to 'char *'


It is just a warning, and a common one at that, not an error. It did compile it, I got the same worning. Warnings just mean that, it might not work for verrry certain cases, and it did still compile, so run.it.
Last edited on
Just a little question. If I use string instead of char, it won't give me an error. Is there's any reason to use char instead of string? thanks
Well, no, its just that when you want to open the file later, you would need
File.open(stringname.c_str());

Use a string :)
Topic archived. No new replies allowed.