Function problem!!

This function, needs to do 2 extra things
Print a msg saying, u allready used this "letter" and loop back
The "letter" isnt in the word!!

Hope someone out there can help.
Its due 2day!!
thanks for looking

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
29
30
31
32
33
34
35
  void guess_letter (char word_player[], int word_size, char underscore[],char letter_used[], int guessed)
{    
	const int SIZE2 = 10;
    char guess[SIZE2]; 
    char used[SIZE2] = {'\0'};
    char LetterEntered,valid;
    int x,i=0,length;
       
    do{
        printf("Enter a guess letter: ");

        fgets(guess, length, stdin);
        LetterEntered = guess[0];
        printf("got letter %c\n",LetterEntered);
              
        if(LetterEntered >= 'a' && LetterEntered <= 'z')
        {   
            valid = 'y';
            for (x=0; x < word_size; x++) {
                if(word_player[x] == LetterEntered) 
                    underscore[x] = LetterEntered;
            } 

            letter_used[guessed] = LetterEntered;
                          
            printf("Now the word is %s\n",underscore);
            printf("letters used %s\n",letter_used);
        }
        else
            printf("Enter a char.\n");
            
    }while(valid != 'y');
    
	return;
}
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include<stdio.h>
#include<ctype.h>

int find_letter( const char str[], int sz, char letter )
{
    for( int i = 0 ; i < sz ; ++i ) if( str[i] == letter ) return 1 ;
    return 0 ;
}

void guess_letter( /* .... */ const char letter_used[], int sz_letter_used )
{  
    int guess ;
    char valid = 'n' ;
    
    do{
        printf("Enter a guess letter: ");
        int temp = tolower( getchar() ) ;
        
        if( temp == EOF ) 
        {
            puts( "input failed" ) ;
            return ;
        }
        
        if( !isalpha(temp) )
        {
            puts( "please enter a letter (a-z)" ) ;
            continue ;
        }
        
        guess = temp ;
        
        if( find_letter( letter_used, sz_letter_used, guess ) )
        {
            puts( "you have already used this letter" ) ;
            continue ;
        }
        
        valid = 'y' ;
        
    }while( valid != 'y' );
    
    /* .... rest of the code .... */
}
Last edited on
For what I heard in my class, return should be only at the end of the function!
and we should avoid use continue; and break;, to affect a loop.

thanks for trying JLBorges
The "letter" isnt in the word!! is done!!
last problem!!!
Print a msg saying, u allready used this "letter" and loop back

PLS SOMEONE HELP!!!
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
void guess_letter (char word_player[], int word_size, char underscore[],char letter_used[], int guessed)
{    
	const int SIZE2 = 10;
    char guess[SIZE2]; 
    char wrong_letter[SIZE2] = {'\0'};
    char LetterEntered,valid;
    int x;
	

   
    do{
        printf("Enter a guess letter: ");

        fgets(guess, word_size, stdin);
        LetterEntered = guess[0];
        //printf("got letter %c\n",LetterEntered);
        //printf("got word_size %d\n",word_size);    
        if(LetterEntered >= 'a' && LetterEntered <= 'z')
        {   
            valid = 'y';
            for (x=0; x < word_size; x++) {
                if(word_player[x] != LetterEntered) 
                    wrong_letter[guessed] = LetterEntered;
            } 
            printf("Letter \"%s\" isnt in the word \n",wrong_letter); //t//n
					
			for (x=0; x < word_size; x++) 
			{
                if(word_player[x] == LetterEntered) 
                    underscore[x] = LetterEntered;
            } 
            letter_used[guessed] = LetterEntered;
			printf("Now the word is %s\n",underscore);
            printf("letters used %s\n",letter_used); //tn
					
        }
        else
        {
			printf("Invalid Input.\n");
			valid = 'n';
        }
		
    }while(valid != 'y');
    
	return;
}
What is wrong??
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
void guess_letter (char word_player[], int word_size, char underscore[],char letter_used[], int guessed)
{    
	const int SIZE2 = 10;
    char guess[SIZE2]; 
    char wrong_letter[SIZE2] = {'\0'};
    char LetterEntered,valid;
    int x;
	

   
    do{
        printf("Enter a guess letter: ");

        fgets(guess, word_size, stdin);
        LetterEntered = guess[0];
        //printf("got letter %c\n",LetterEntered);
        //printf("got word_size %d\n",word_size);    
        if(LetterEntered >= 'a' && LetterEntered <= 'z')
        {   
            valid = 'y';
            
			for (x=0; x < SIZE2; x++) {
                if(letter_used[x] == LetterEntered) 
                    wrong_letter[guessed] = LetterEntered;
            } 
            printf("You already used %c\n",LetterEntered); //t//n
			valid = 'n';
			
			for (x=0; x < word_size; x++) {
                if(word_player[x] != LetterEntered) 
                    wrong_letter[guessed] = LetterEntered;
            } 
            printf("Letter \"%s\" isnt in the word \n",wrong_letter); //t//n
					
			for (x=0; x < word_size; x++) 
			{
                if(word_player[x] == LetterEntered) 
                    underscore[x] = LetterEntered;
            } 
            letter_used[guessed] = LetterEntered;
			printf("Now the word is %s\n",underscore);
            printf("letters used %s\n",letter_used); //tn
					
        }
        else
        {
			printf("Invalid Input.\n");
			valid = 'n';
        }
		
    }while(valid != 'y');
    
	return;
}

Topic archived. No new replies allowed.