- Online Community Forums » Programming Questions » Name input question

This thread is locked; no one can reply to it. rss feed Print
Name input question
Raf Vermeulen
Member #7,166
April 2006

So, my game runs smoothly, the highscore table works fine (except that for some reason, it shows 1 entry too much... last entry's always double, but that left aside), but now I want the player to enter his/her name, so it can store that into the highscore table instead of a standard string.

Anybody here know how I can get around to do this? preferably, it'd be an input-field that's drawn on the screen. I simply have no idea how to start on this:-X

Matthew Dalrymple
Member #7,922
October 2006

This thread is going on right now with someone creating a text input class.

Basic concept:
---Read a key,
------if its valid(a letter or whatever they can type in)
---------add it to a string/buffer
------if its not valid
---------discard it
------if key is backspace
---------remove the last character on the string/buffer
---Display it to the screen

Keyboard Routines

I like signatures that only the signer would understand. Inside jokes are always the best, because they exclude everyone else.

Member #8,283
January 2007

try looking at d_edit_proc, otherwise consider writing it yourself, is not that hard, also there is a small routine on the code gallery, look at it and go from there.


Member #8,201
January 2007

Yes hehe I was just going to post that. I have a couple of threads made dealing with text input.

Here's the other one:

Raf Vermeulen
Member #7,166
April 2006

Ack, compiling error I can't figure out.

        DIALOG the_dialog[] = 
        { d_agup_edit_proc,     15, 75, 125,  8,  0,  0,  0,  0,  100,   0,  name,     NULL,  name },
        { d_agup_push_proc,     90, 115, 100, 18,  0,  0,  0,  0,       0,   0,  (void*)"Done",     0, edit_high(pos, name, count) },        
                 /* the next two elements don't draw anything */
               { d_yield_proc,        0,   0,    0,    0,   0,  0,    0,      0,       0,   0,    NULL,                   NULL, NULL  },
               { NULL,                0,   0,    0,    0,   0,  0,    0,      0,       0,   0,    NULL,                   NULL, NULL  }

1void edit_high(long pos, char name[100], int count) {
2 int end_score;
3 int end_lvl;
6 FILE * file;
7 file = fopen("High.txt","r+");
11 fseek(file, pos, SEEK_SET);
12 fprintf(file, "NEW\n");
13 fprintf(file,"%d\n", score);
14 fprintf(file,"%d\n", lvl_nr+1);
15 if (count < 10) {
16 fprintf(file, "%s\n", name);
17 fprintf(file,"%d\n", end_score);
18 fprintf(file,"%d\n", end_lvl);
19 }
22 for (int i = (count+1); i <= 10; i++) {
23 cout << "I: " << i << "\n";
24 cout << "Count: " << count << "\n";
25 pos = ftell(file);
26 fseek(file, pos-1, SEEK_SET);
27 cout << "POS before: " << pos << "\n";
28 fscanf(file, "%s", name);
29 fscanf(file, "%d", &end_score);
30 fscanf(file, "%d", &end_lvl);
31 fseek(file, pos, SEEK_SET);
32 fprintf(file, "%s\n", name);
33 fprintf(file,"%d\n", end_score);
34 fprintf(file,"%d\n", end_lvl);
35 cout << "POS after: " << pos << "\n";
36 }
37 fclose(file);

That gives this error: void value not ignored as it ought to be

Tobias Dammers
Member #2,604
August 2002

That means that you assigned the result of a statement that evaluates to void (typically a function with return type void) to something, or passed it to a function as a parameter.
The compiler give you a source code line number. Check the line in question and see if you don't do anything like this:

void my_func() {
  // do some stuff here

int main() {
  int a = my_func(); // <- This line will trigger an error.
  return 0;

--- EDIT ---
Oh, I see it. You assign the return value of edit_high to a field in the DIALOG structure. That can't be what you want.

Me make music: Triofobie
"We need Tobias and his awesome trombone, too." - Johan Halmén

Raf Vermeulen
Member #7,166
April 2006

The line number it gives, is the last line of the upper code. If I comment out the d_agup_push_proc line, it works fine, so there must be something with the call to the function edit_high in there, but it all seems in order to me.

Tobias Dammers
Member #2,604
August 2002

It is not, I already explained.
The line in question calls edit_high(), then stores the result (the return value) in the dialog array. Since edit_high() returns nothing (void), there is nothing to store in the array, and the value put into the struct is undefined. You are not doing it right.
My guess is that you want the dialog item to trigger the edit_high function; in that case, you need to do one of the following things:
a) use a dialog item that can trigger a function, and pass edit_high itself to it, that is, just the function name, without the brackets or any arguments (which will be interpreted as a function pointer). This is only possible if the dialog item in fact supports this, and if the argument structure and return type are exactly what the dialog item expects.
b) make the dialog item exit the dialog, and if this item is the one triggering an end-of-dialog, execute edit_high() manually and re-run the dialog afterwards.
c) use a custom dialog procedure that checks if the button has been pressed; if so, it executes edit_high(), then resumes executing the dialog.

Me make music: Triofobie
"We need Tobias and his awesome trombone, too." - Johan Halmén

Kitty Cat
Member #2,815
October 2002

The dialog is initted at load-time, and as you have it, to fill in the dialog it has to run your function to get its return value. However, your function returns a void, and the place your're putting it wants a void*.

What you want to do is pass a function pointer, not call the function. Do that by omitting the parameters and (). The dialog code will run your function, not you. And as a result, it can only take the parameters the dialog code gives it, not the ones you want it to have. Same with its return value.

"Do not meddle in the affairs of cats, for they are subtle and will pee on your computer." -- Bruce Graham

Tobias Dammers
Member #2,604
August 2002

Good explanation.
One more thing: If you compile as C++ (as opposed to C), you need to manually cast your function pointer to void* by putting (void*) in front of it (including the brackets).

Me make music: Triofobie
"We need Tobias and his awesome trombone, too." - Johan Halmén

Raf Vermeulen
Member #7,166
April 2006

Sweet, it compiles now:D Can't actually click it or edit the text in the textfield, but it's progress:D

Go to: