Problems with strings and for loops

This is a discussion on Problems with strings and for loops within the C Programming forums, part of the General Programming Boards category; since you say that it returns a char value, I'm assuming that i need to run those if statements outside ...

  1. #46
    Registered User
    Join Date
    Jan 2004
    Posts
    50
    since you say that it returns a char value, I'm assuming that i need to run those if statements outside the function?

    I've tried it but i guess this isn't in the right order?

    Code:
    			scanf(" %c", &let2bg);
     
    				usedcharacters (usedchars, let2bg);
    				
    				if (usedchars != '\0')
    				{
    				
    				checkchar(let2bg, word2bguessed, usercorrect);
    				
    				printf("\n%s\n", usercorrect);
    					correctguess = numberright (currentright, usercorrect);
     
    								if (correctguess == 0)
    										{
    										printf ("You have no correct letters");
    										}
     
    
    								if ((correctguess < wordlength) && (correctguess > 0))
    										{
    										printf ("You have %d correct letters", correctguess);
    										}
     
    
    								if (correctguess == wordlength)
    								{
    								printf ("You win! The correct word was %s!\n", word2bguessed);
    								return 0;
    								} /*ends the program*/
     
    					}
    				if (usedchars == '\0')
    					{printf ("Oops! you tried this letter before. Please enter another letter.");}
    						
    			}
     
    		if (let2bg == '*') 
    			
    			{
    			printf("thanks for playing!\n");
    			}
    	
    		
    		printf ("\nSorry but you've had too many gueses, better luck next time!\n");
    		return 0;
    		}

  2. #47
    Registered User
    Join Date
    Dec 2003
    Posts
    53
    The good news is, you almost got it working.
    The bad news?
    You didn't store the char value that the function return.
    In order to store a value that a function returns using the return, you have to do this:
    char temp;
    temp = usedcharacters(...);
    then use temp to do the comparisons.
    That should do the trick.

  3. #48
    Registered User
    Join Date
    Jan 2004
    Posts
    50
    Its ok, I've had it running for about the last hour or so, I've just being trying to add all the comments and do the documentation before 9am tomorrow morning (its 2am now!) It should be all done and dusted within the next hour or so, I'll show you the cleaned up code before i go to sleep

  4. #49
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You're over complicating this drasticly. I've only read the last code posting here, but it seems much simpler to do this:
    Code:
    unsigned char letterlist[256] = {0};
    
    scanf("%c", &letter );
    if( letterlist[letter] )
        printf("You've already used %c.\n", letter );
    else
    {
        letterlist[letter]=1;
        ...letter is valid, do whatever with it...
    }
    No need for a seperate function, though you could move it to a function using the same idea.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #50
    Registered User
    Join Date
    Jan 2004
    Posts
    50
    Final code: http://rafb.net/paste/results/p2052581.html
    Took less time than i though, added loads of comments and removed some stuff i didn't need.

    Thanks again for all your help, even added a little bit of credit to you (see if you can spot it )

  6. #51
    Registered User
    Join Date
    Dec 2003
    Posts
    53
    I spotted some thing that doesn't seem right...

    Code:
    int usedcharacters (char usedchars [], char let2bg)			
    {
         int k;
         for(k = 0; usedchars[k] != '\0' && usedchars[k] != let2bg; k++); 
         {
                if(usedchars[k] == '\0')
                {
    	   usedchars[k] = let2bg;
                       usedchars[k+1] = '\0';
                       printf ("%c", usedchars [k]);
                       return usedchars [k];					
                 }
           }
     
           return usedchars [k] = '\0';  /*In this line, you replace the kth character with a null character, essentially removing the record of a previously entered letter.*/
    }
    you should replace that line with
    Code:
    return '\0';

  7. #52
    Registered User
    Join Date
    Jan 2004
    Posts
    50
    if its wrong how come it compiles without any errors and works fine? Still I've handed it all in so there is very little i can do now

  8. #53
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Here's something you'll want to take notice of:
    Code:
    int usedcharacters (char usedchars [], char let2bg)			
    {
         int k;
         for(k = 0; usedchars[k] != '\0' && usedchars[k] != let2bg; k++); 
         {
                if(usedchars[k] == '\0')
                {
            	   usedchars[k] = let2bg;
                       usedchars[k+1] = '\0';
                       printf ("%c", usedchars [k]);
                       return usedchars [k];					
                 }
           }
     
           return usedchars [k] = '\0';  /*In this line, you replace the kth character with a null character, essentially removing the record of a previously entered letter.*/
    }
    There is no need for anything inside your loop. Your loop doesn't do anything. It simply loops through until it finds your character you're looking for or a null, at which point it quits. Nothing inside the loop ever happens. It can't. If usedchars[k] is '\0', then the loop exits. Thus, the middle of the loop where it actually checks for '\0', is pointless, because it's impossible for usedchars[k] to ever be '\0' inside the loop.

    So in short, all this function does is walk through the array, and when it finds the first occurance of the character you want, or it finds a null, it replaces that character or that null with a null. Then it returns null.

    [edit]
    Quote Originally Posted by petedee
    if its wrong how come it compiles without any errors and works fine? Still I've handed it all in so there is very little i can do now
    Just because it compiles doesn't mean it's correct and functioning as you'd like.
    Code:
    int main( void )
    {
        int *p;
    
        scanf("%d", p ); /* see? */
        return 0;
    }
    [/edit]

    Quzah.
    Last edited by quzah; 04-02-2004 at 02:55 AM.
    Hope is the first step on the road to disappointment.

Page 4 of 4 FirstFirst 1234
Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21