HighScore Player Name Entering Quesion
I am making some real good progress on my HS system (thanks to everyone here )
I''m on to the final point where I have the player enter their name to be saved in the highscore file.
Now, I am having problems with is the string manipulation(as always)
Ok...so the player has gotten a new highscore and goes to a screen where he inputs his name where it is saved to char player_score[16];
My problem is with the realtime manipulation of the player_score string.
//The string is empty...The player hits the P key
How do I add the ''P'' onto the string while keeping all the other letters and also not going over the allotted size of the array??
//The string is full and the player wants to delete some characters so hits the backspace key
How do I delete one character from the string without deleting an invalid character?
**I think my problem is I just can''t seem to see Char arrays as the same thing as any other array...anyway,
if anyone has a spare minute and could help me that''d be great.
Thanks(again),
-Tim
Just keep an index to the current letter.
char player[PLAYER_NAME_SIZE+1];
int name_index = 0;
Store the key pressed (if valid) at player[name_index], inc name_index (if not at PLAYER_NAME_SIZE-1), put a ''\0'' at player[name_index]. If the key was a delete, dec name_index (only if > 0) and put a nul at name_index. If he hits Enter, you''re done.
aig
char player[PLAYER_NAME_SIZE+1];
int name_index = 0;
Store the key pressed (if valid) at player[name_index], inc name_index (if not at PLAYER_NAME_SIZE-1), put a ''\0'' at player[name_index]. If the key was a delete, dec name_index (only if > 0) and put a nul at name_index. If he hits Enter, you''re done.
aig
I think something like this would work:
char done = 0
int pos = 0;
char string[16];
while (!done) {
char ch = getKey();
switch (ch) {
case 9: // backspace
if (pos == 0) break;
string[--pos] = 0;
break;
case 13: // enter
done = 1;
break;
case default: // everything else
if (pos == 16) break;
string[pos++] = ch;
break;
}
}
If it doesn't...well... fix it
Wow, that's ugly. If there a way to make the board preserve leading spaces?
Edited by - TheHoff on 4/20/00 8:13:55 PM
char done = 0
int pos = 0;
char string[16];
while (!done) {
char ch = getKey();
switch (ch) {
case 9: // backspace
if (pos == 0) break;
string[--pos] = 0;
break;
case 13: // enter
done = 1;
break;
case default: // everything else
if (pos == 16) break;
string[pos++] = ch;
break;
}
}
If it doesn't...well... fix it
Wow, that's ugly. If there a way to make the board preserve leading spaces?
Edited by - TheHoff on 4/20/00 8:13:55 PM
quote: Original post by TheHoff
Wow, that's ugly. If there a way to make the board preserve leading spaces?
Yes, putting the HTML codes <font size=+1><pre> before your code, and </pre></font> after it should work.
test:
no spaces one space two spaces three spaces
aig
Edited by - An Irritable Gent on 4/20/00 8:26:06 PM
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement