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.
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.
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);
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]; }
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.
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.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]) {//return temp [i]; Actually this line only returns the integer value of either '\0' or '*'... return 0;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}
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
I kinda get what you mean now:
code:
It does the same thing only with an interesting character on the end that I'm not sure what it is.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]; }
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...
No problem, its all on http://rafb.net/paste/results/A1969227.html
Okay, last reply before I go to bed (It's 2 am over here...)
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)Code:if (let2bg != '*') { checkchar (let2bg, word2bguessed, usercorrect); printf ("%c", usercorrect [i]); // this is amusing }
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:
entering e:*h**** // <--- this line is from the checkchar function
*h**** // <--- this line is from printf("\n%s\n", usercorrect);
entering s:**ee*e
*hee*e
Try it out.****s*
*heese
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!
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:
The exit statement works fine, its just the winning one. Full code again: http://rafb.net/paste/results/Q2114544.htmlCode: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;
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 */ }
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?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 */
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.
Actually i think i know what you mean...
Is this better? Nothing ever seems to be returned though? Is my syntax wrong or something?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; }
rest of the code: http://rafb.net/paste/results/PS402646.html
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:
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:Originally Posted by petedee
Or, you could assign the return value to a variable like so: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 ); }
This is an example only, to show how you would assign a return value to a variable.Code:correctguess = checkchar(let2bg, word2bguessed, usercorrect);
[/edit]
Last edited by kermit; 03-31-2004 at 06:57 PM.