Hi all,
I'm making a game and I have a problem inside a function. I've included the typedef of the characters struct and the function that makes use of this struct. I've marked where the problem occurs with a comment.
What happens is, when the function reaches the point where i've marked the bug, it will execute the first of a series of 9 reads, but after the first read it will execute another read that appears out of nowhere, and this value will be stored in the next read variable. (Eg, after the HP read, the ghost read will store that variable into offense, and offense will actually be read into defense etc).
I'm running cygwin on windows. Any kind of help is appreciated. Thanks for your time^^
P.S I've only included 2 of the 9 reads of data because they are all of the same format, just different variables.
I'm making a game and I have a problem inside a function. I've included the typedef of the characters struct and the function that makes use of this struct. I've marked where the problem occurs with a comment.
What happens is, when the function reaches the point where i've marked the bug, it will execute the first of a series of 9 reads, but after the first read it will execute another read that appears out of nowhere, and this value will be stored in the next read variable. (Eg, after the HP read, the ghost read will store that variable into offense, and offense will actually be read into defense etc).
I'm running cygwin on windows. Any kind of help is appreciated. Thanks for your time^^
Code:
typedef struct {
int side, class;
char name[NAME_LIMIT];
int hp, offense, defense, agility, force_cap;
int force_1, force_2, force_3, ultimate;
} character;
/*Make a new character and store character data into structure*/
void
make_char(character *char_s){
int success = 0;
int hp, offense, agility, force_cap;
int force_1, force_2, force_3, ultimate;
/*Read in the name, side and class of character*/
printf("Enter name of character (limit 20 characters): ");
scanf("%s", char_s->name);
while(success == 0){
printf("Choose side (1 for Jedi, 2 for Sith): ");
scanf("%d", &char_s->side);
if(char_s->side > 0 && char_s->side < 3){
success = 1;
}
else {
printf("You have made an invalid selection!!\n");
}
}
success = 0;
while(success == 0){
printf("Choose class (1 for battle, 2 for force, 3 for intelligence): ");
scanf("%d", &char_s->class);
if(char_s->class > 0 && char_s->class < 4){
success = 1;
}
else {
printf("You have made an invalid selection!!\n");
}
}
/*Read in the stats of the character*/
success = 0;
[b]/*Problem begins here*/[/b]
while(success == 0){
printf("Enter points allocated to HP: ");
scanf("%d\n", &hp);
if(hp >= 0 && hp < 41 ){
success = 1;
}
else {
printf("You have made an invalid selection!!\n");
}
}
success = 0;
while(success == 0){
printf("Enter points allocated to Offense: ");
scanf("%d\n", &offense);
if(offense >= 0 && offense < 41){
success = 1;
}
else {
printf("You have made an invalid selection!!\n");
}
}
}
P.S I've only included 2 of the 9 reads of data because they are all of the same format, just different variables.