Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Extra reads coming out of nowhere 1

Status
Not open for further replies.

groovysl

Programmer
Feb 13, 2005
3
AU
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^^

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.
 
Code:
    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");
        }
    }
This is just begging for some modularisation!

Code:
int ReadAnInt( char *prompt, int lower, int upper, int *answer ) {
    char buff[BUFSIZ];
    int success = 0;
    while(success == 0){
        printf("%s",prompt);
        fflush( stdout );
        if ( fgets( buff, BUFSIZ, stdin ) == NULL ) {
            break;
        } else 
        if ( sscanf( buff, "%d", answer ) == 1 &&
             *answer >= lower && *answer <= upper ) {
            success = 1;
        } else {
            printf("You have made an invalid selection!!\n");
        }
    }
    return success;
}

Then you can do
Code:
    ReadAnInt( "Choose side (1 for Jedi, 2 for Sith): ", 1, 2, &char_s->side);
    ReadAnInt( "Choose class (1 for battle, 2 for force, 3 for intelligence): ", 1, 3, &char_s->class);

> scanf("%d\n", &hp);
A trailing newline on scanf doesn't mean what you think it means.

--
 
Ah.. thank you.. i totally did not see that \n

LOL stupid me!!!
 
I don't know why HP stored in offensive, but it's a very suspicious moment: your problem begins in place where char_s->var indirect references avoided. Why you read into local variables with the same names as structure members names?..
 
In the latter part of this function (I left out the last part) there's a lot of arithmetic, and it's easier for me if I don't do something like:

char->hp = BASE + MULTIPLIER * char->hp

It's just I have a problem with keeping track of things, really confuses me >_<

But problem has been fixed, thanks for all your help ^^
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top