Thread: Problems with strings and for loops

  1. #16
    Registered User
    Join Date
    Jan 2004
    Posts
    50
    is that a string.h function? It's really annoying because we aren't allowed to use anything that is in the string.h libary.

  2. #17
    Registered User
    Join Date
    Dec 2003
    Posts
    53
    ouch...
    Yep, strcmp is in the string.h...
    But it's quite easy to make a function to compare two strings, just an extention of what you'd have to do for the checkword function...

    speaking of which, I just realized my function prototype is wrong...
    use the following:
    void checkword(const char *source, char * target, char let2bc);

  3. #18
    Registered User
    Join Date
    Jan 2004
    Posts
    50
    Thanks, I'm almost there now, I've got it to print out what letters are where the only problem is that whenever you type disappears when you type the next letter, how can i save the state of it?

    Here's the code:

    Code:
    int checkchar (const char let2bg, char word2bguessed [], const char usercorrect [])
    	
    
    	{
    	int i;
    	char temp [7];
    
    		for (i=0; word2bguessed[i] !='\0'; i++)
    		    if (let2bg == word2bguessed [i])
    				{	
    				printf ("%c",let2bg);
    				temp [i] = let2bg;
    				}
    				
    				else
    				{
    				printf ("*");
    				temp [i] = usercorrect [i];
    				}
    			
    				return temp [i];
    	}

  4. #19
    Registered User
    Join Date
    Dec 2003
    Posts
    53
    okay, my idea is to use the usercorrect string to store the current state of the user's results, hence I wouldn't make it a const char array here.
    Code:
    int checkchar (const char let2bg, char word2bguessed [], char usercorrect []) // removed const
    	
    {
    
    int i; //char temp [7]; <--- not needed in first place for (i=0; word2bguessed[i] !='\0'; i++)
    if (let2bg == word2bguessed [i]) {
    printf ("%c",let2bg); // temp [i] = let2bg; temp is a local array, in other words, as soon //as you exit this function, temp disappears. Not good, especially //since you are not returning an array(or string) usercorrect[i] = let2bg; // inserts the correct char into the usercorrect array
    } else {
    printf ("*"); // temp [i] = usercorrect [i]; <--not needed anymore
    }
    //return temp [i]; Actually this line only returns the integer value of either '\0' or '*'... return 0;
    }
    With this code, you should be able to keep track of which characters the user has guessed so far by refering to the usercorrect string.
    HINT: print out the usercorrect string each time after call this function...
    Last edited by tzuchan; 03-31-2004 at 11:33 AM. Reason: Bah... indent tags

  5. #20
    Registered User
    Join Date
    Jan 2004
    Posts
    50
    I kinda get what you mean now:

    code:
    Code:
    int checkchar (const char let2bg, char word2bguessed [], char usercorrect [])
    	
    
    	{
    	int i;
    		for (i=0; word2bguessed[i] !='\0'; i++)
    		    if (let2bg == word2bguessed [i])
    				{	
    				printf ("%c",let2bg);
    				usercorrect [i] = let2bg;
    				}
    				
    			else
    				{
    			
    				printf ("*");
    				}
    				
    				return usercorrect [i];
    		
    	}
    It does the same thing only with an interesting character on the end that I'm not sure what it is.

  6. #21
    Registered User
    Join Date
    Dec 2003
    Posts
    53
    can you post the entire code somewhere were I can look at it?
    I'd like to complie and run it over here to be sure before I answer you...

  7. #22
    Registered User
    Join Date
    Jan 2004
    Posts
    50

  8. #23
    Registered User
    Join Date
    Dec 2003
    Posts
    53
    Okay, last reply before I go to bed (It's 2 am over here...)
    Code:
    if (let2bg != '*')
        {
            checkchar (let2bg, word2bguessed, usercorrect);	
            printf ("%c", usercorrect [i]);  // this is amusing
         }
    in truth, that line printf("%c", usercorrect[i]); should print nothing at all(at least it didn't on my comp) because it is only printing the '\0' character(which happens to be the string terminator character, BTW)
    replace it with this line:
    printf("\n%s\n", usercorrect); // <--- no subscripts for the usercorrect!!!

    when you run your program, you should get two lines of output per key pressed:

    here's what you should get if you enter h, then e, then s

    entering h:
    *h**** // <--- this line is from the checkchar function
    *h**** // <--- this line is from printf("\n%s\n", usercorrect);
    entering e:
    **ee*e
    *hee*e
    entering s:
    ****s*
    *heese
    Try it out.

  9. #24
    Registered User
    Join Date
    Jan 2004
    Posts
    50
    YAY! thanks for all your help, doubt i could do this without ya, cheers fella, all I gotta do now is remove the bugs and the obvious holes but the majority of it works yay!

  10. #25
    Registered User
    Join Date
    Jan 2004
    Posts
    50
    OK more bloomin bugs, this one is getting me. Here is the situation, the user enters all of their data and gets the word right? How can i tell them that they have won? I thought a simple if statement would do the job but oh no, here's the code:

    Code:
    while ((count < maxguess) && (let2bg != '*'))
     
    	{ 
    		count ++;
    		printf ("\nThis is guess no %d \n", count);
    		printf ("Please take a guess... \n");
    		scanf (" %c", &let2bg);
    		checkchar (let2bg, word2bguessed, usercorrect, correctguess);					
    		printf("\n%s\n", usercorrect);
    				
    		if (correctguess = wordlength)
    		{printf ("you win");}
    		
    		if (let2bg == '*')
    			{printf ("thanks for playing!\n");}
    	}
    
    		
    	
    		return 0;
    The exit statement works fine, its just the winning one. Full code again: http://rafb.net/paste/results/Q2114544.html

  11. #26
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    A few minor details, highlighted in red throughout..

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define BLANK '*'
    
    int lengthofstring(const char *eptr);
    int checkchar(const char let2bg, char word2bguessed[], char usercorrect[],
    	      int correctguess);
    
    int main(void)
    {
        char word2bguessed[] = { "cheese" };
        char usercorrect[] = { "******" };
        char let2bg;
        int wordlength = 0;
        int count = 0;
        int i = 0;
        int maxguess = 0;
        int correctguess = 0;
    
        wordlength = lengthofstring(word2bguessed);
    
        maxguess = 10;		/* Maximum number of guesses */
    
        printf("The word you are looking for is %d characters long \n",
    	   wordlength);
    
        for (i = -0; i < wordlength; i++) {
    
    	printf("*");		/* Printing blanks */
        }
    
        while ((count < maxguess) && (let2bg != '*')) {
    	count++;
    	printf("\nThis is guess no %d \n", count);
    	printf("Please take a guess... \n");
    	scanf(" %c", &let2bg);
    	checkchar(let2bg, word2bguessed, usercorrect, correctguess);
    	printf("\n%s\n", usercorrect);
    
    	if (correctguess == wordlength) { /* you had =  instead of == */
    	    printf("you win");
    	}
    
    	if (let2bg == '*') {
    	    printf("thanks for playing!\n");
    	}
        }
        return 0;
    }
    
    
    
    int lengthofstring(const char *eptr)
    {
    
        int length;
        for (length = 0; *eptr != '\0'; eptr++) {
    	length++;
        }
    
        return length;
    }
    
    int checkchar(const char let2bg, char word2bguessed[], char usercorrect[],
    	      int correctguess)
    {
        int i;
        for (i = 0; word2bguessed[i] != '\0'; i++) {
    	if (let2bg == word2bguessed[i]) {
    	    usercorrect[i] = let2bg;
    	    correctguess++;
    
    	}
        }
    return usercorrect[i], correctguess;
    
    /* you cannot return two values with the return statement.  You have 
    the pointer to usercorrect[], so use that to return the value for one, and 
    use the return statement to catch the other */
    
    }

  12. #27
    Registered User
    Join Date
    Jan 2004
    Posts
    50
    return usercorrect[i], correctguess;

    /* you cannot return two values with the return statement. You have
    the pointer to usercorrect[], so use that to return the value for one, and
    use the return statement to catch the other */

    So i need to set up a pointer to the correctguess value then? or would there be an easier way to do this outside the function?

  13. #28
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    I think your idea to compare the return value of correctguess to wordlength is not a bad idea - just one problem, wordlength is six, but the puzzle is solved with less than six guesses, as the letter e occurs 3 times. Might have to work something out to overcome that.

  14. #29
    Registered User
    Join Date
    Jan 2004
    Posts
    50
    Actually i think i know what you mean...

    Code:
    int checkchar(const char let2bg, char word2bguessed[], char usercorrect[], int correctguess)
    {
        int i;
        for (i = 0; word2bguessed[i] != '\0'; i++) 
    	
    	{
    	if (let2bg == word2bguessed[i])
    		
    		{
    	    usercorrect[i] = let2bg;
    	    correctguess++;
    		}
        }
    		return correctguess;
    	}
    Is this better? Nothing ever seems to be returned though? Is my syntax wrong or something?

    rest of the code: http://rafb.net/paste/results/PS402646.html

  15. #30
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    I had a closer look and realised I missed a couple of things here.

    Here are some hints:

    forget about returning a value from checkchar, and pass a pointer to correctguess to the function instead, in order to increment the counter. Doing this will also allow you to compare correctguess to wordlength without any adjustments - I was mistaken before when I said that the 'e' guess would make correctguess a couple less than wordlength.

    If the user enters a correct letter more than once, the correctguess count will still be incremented, and as such comparing correctguess to wordlength with == will not work, and the program would not end. I will leave that for you to play with and fix.

    ~/

    [edit]

    In response to this:
    Quote Originally Posted by petedee
    Is this better? Nothing ever seems to be returned though? Is my syntax wrong or something?
    A return value can be used in a couple of different ways - An example of one way would be if you wanted to print out a value, and you used a function to get that value - in your argument list for printf, you would call the function, and printf would print the value returned, just as if you had typed it in yourself. Here is an example:

    Code:
    #include <stdio.h>
       
    /* some_fruit.c - Rob Somers - November 8, 2003
     * demonstrate the use of multiple parameters
     * with a function
     */
    
    int Sum_Fruit( int, int, int );
    
    int main( void )
    {
         int apples = 4;
         int oranges = 7;
         int pears = 2;
    
         printf( "\nThe total pieces of fruit you have is: ")
         printf( " %d\n", Sum_Fruit( apples, oranges, pears ) );
         return 0;
    }
    
    /* sum_fruit() - Add up the total pieces of fruit and return value to caller */
    
    int Sum_Fruit( int apples, int oranges, int pears )
    {
         return( apples + oranges + pears );
    }
    Or, you could assign the return value to a variable like so:
    Code:
    correctguess = checkchar(let2bg, word2bguessed, usercorrect);
    This is an example only, to show how you would assign a return value to a variable.

    [/edit]
    Last edited by kermit; 03-31-2004 at 06:57 PM.

Popular pages Recent additions subscribe to a feed