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; Hey guys, I've being given a task of making a simple game of hangman for a project, you would have ...

  1. #1
    Registered User
    Join Date
    Jan 2004
    Posts
    50

    Problems with strings and for loops

    Hey guys, I've being given a task of making a simple game of hangman for a project, you would have thought it wouldn't be too hard would you, still, I'm having a couple of problems and i was wondering if you could help me.

    What i am trying to do is use the script below to read through an array of characters and stop when it finds a specific one:

    Code:
    	for (j=0; let2bg[j] != '\0'; j++);
    so whilst the code is running and is not at the terminating character it will keep checking for specific characters.

    I also have the rest of the code at http://rafb.net/paste/results/m1638719.html

    Thanks for your help!

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,344
    Code:
    for (j=0; let2bg[j] != '\0'; j++);
    See that semi-colon at the end?

    Remove it!
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jan 2004
    Posts
    50
    thanks but when i remove it i still get the error

    "error C2109: subscript requires array or pointer type
    Error executing cl.exe"


    It says it requires and array or a pointer, isn't the let2bg already an array?

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,344
    > isn't the let2bg already an array?
    ...
    > char let2bg, numbercorrect;
    No, apparently it isn't

    Which kinda makes several other things in the code broken as well.

    I suggest you visit your compiler options page and set the warning level to /W4
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Jan 2004
    Posts
    50
    Ok so well it is only going to contain the one character and the null value so does it matter if it isn't an array? The program will just check to see if it part of hiddden word string (word2bguessed)

  6. #6
    Registered User
    Join Date
    Dec 2003
    Posts
    53
    Quote Originally Posted by petedee
    thanks but when i remove it i still get the error

    "error C2109: subscript requires array or pointer type
    Error executing cl.exe"


    It says it requires and array or a pointer, isn't the let2bg already an array?
    Went and had a look at your code:

    Code:
    char let2bg, numbercorrect;
    you declared let2bg as a char variable, not a char array(AKA string).
    BTW, a brief look at your code seems to indicated that there are a couple of logic errors...

  7. #7
    Registered User
    Join Date
    Dec 2003
    Posts
    53
    Quote Originally Posted by petedee
    Ok so well it is only going to contain the one character and the null value so does it matter if it isn't an array? The program will just check to see if it part of hiddden word string (word2bguessed)
    yes, null character '\0' is still a character and will take up one character space. However, if your intention was to only get one character and compare it with those in the string, use getchar.

  8. #8
    Registered User
    Join Date
    Jan 2004
    Posts
    50
    I'm not too used to getchar, how can i print the letter selected using printf?

    I've also heard of the fgets function? Is this easy to use or am i going to need another function to make the character usable?

  9. #9
    Registered User
    Join Date
    Dec 2003
    Posts
    53
    getchar can only return one character. So just use it to see if the user entered a character that exists in the string.

    try this out and see what happens:
    Code:
    #define STRLEN 10
    int main(void)
    {
    
    char temp, array[STRLEN]; int i; for(i = 0; (temp = getchar()) != EOF && i < STRLEN - 1; i++) {
    array[i] = temp; printf("%c\n", temp);
    } if(i < 9) i++; // should be required in order to place the null character properly. array[i] = '\0'; //inserts string terminator character printf("%s", array); system("pause"); return 0;
    }
    I won't promise that the code is bug free, but it should do enough for you to see how to use getchar.

  10. #10
    Registered User
    Join Date
    Jan 2004
    Posts
    50
    Thanks for that, i have some idea's reading back through my notes I've being suggested to try using the code:

    Code:
    	while( (let2bg = getchar()) !='\n');
    this will remove the enter which is put in when you accept the value won't it?

  11. #11
    Registered User
    Join Date
    Dec 2003
    Posts
    53
    Quote Originally Posted by petedee
    Thanks for that, i have some idea's reading back through my notes I've being suggested to try using the code:

    Code:
    	while( (let2bg = getchar()) !='\n');
    this will remove the enter which is put in when you accept the value won't it?
    Not exactly...
    What happens is that your program will loop until the enter key is press, which will cause getchar to return the character '\n'.
    The thing is, when using scanf to read a character, it does precisely that, reads one character, and forgets about anything else in the input stream(which just so happens to include one '\n' placed in the input stream by the enter key)

  12. #12
    Registered User
    Join Date
    Jan 2004
    Posts
    50
    So that will do the opisite of what i want and only return the enter key?

    If i crate an extra character which job would be just to store the enter key can i remove it from the string?

  13. #13
    Registered User
    Join Date
    Dec 2003
    Posts
    53
    Quote Originally Posted by petedee
    So that will do the opisite of what i want and only return the enter key?

    If i crate an extra character which job would be just to store the enter key can i remove it from the string?
    No, it means that the while loop will process ANY key that you press up until you press the enter key, which will then exit the loop. The '\n' character will not enter the loop, so you don't need to worry about it at all.

    /*** EDIT ***/

    *Grumbles* I should be more careful when I'm reading codes...

    the precise line that you gave me,
    Code:
     while ((let2bg = getchar()) != '\n'); <-- didn't see this semicolon just now...
    has the effect of clearing out the input stream.

    that is to say in this example

    Code:
    int main (void)
    {
    
    char temp; int data = 0; while(data != 13) {
    printf("Please enter 12abc here: "); //gets user to type 12abc and press enter scanf("%d", &data); printf("enter 13 instead of 12abc to exit program.\n"); while((temp= getchar()) != '\n'); // comment and uncomment this line to see what happens
    } printf("bye bye\n"); system("PAUSE"); return 0;
    }
    if you comment out the line "while((temp= getchar()) != '\n');", the program will enter an infinite loop after you enter "12abc" because of the stuff that scanf is leaving in the input stream.
    Last edited by tzuchan; 03-31-2004 at 07:44 AM.

  14. #14
    Registered User
    Join Date
    Jan 2004
    Posts
    50
    Its ok, i seem to have fixed it, i still used scanf but i changed "%s" to " %c" it seems to have done the trick (thanks for your help though). I've now opened up a whole new world of pain though

    I've now moved on to actually replaceing the letters that are found in the string. What i have so far is below:

    Code:
    int checkchar (const char let2bg, const char word2bguessed [])
    	
    
    	{
    	char answer;
    	int i;
    	
    		for (i=0; word2bguessed[i] !='\0'; i++)
    		  if (let2bg == word2bguessed [i])
    			
    
    		return answer;
    	}
    this basically checks the string to see if the letter that the user has searched for is in the string, and replaces the letters found with real letters instead of having *'s which blank out the mystery word, any ideas?

    Oh yea: full code is avalible if you need it http://rafb.net/paste/results/Cvt88411.html
    Last edited by petedee; 03-31-2004 at 08:47 AM.

  15. #15
    Registered User
    Join Date
    Dec 2003
    Posts
    53
    looking at your program, I'm guessing that you want to first printout a number of '*' indicating the length of the word, and then whenever the user enters a correct letter, replace the '*' where apropriate with the character?
    eg on the screen
    ******
    becomes
    **ee*e
    when the user enters 'e'?
    If so, I would suggest that you use a string of the same size as your word2bguessed and fill it with '*'s first.

    eg
    char word2bguessed[7] = "cheese", usercorrect[7] = "******";
    and then use a function to check and replace the corresponding '*' with the correct letter so that
    eg user enters 'e'
    Code:
    //before passing into function word2bguessed = "cheese", usercorrect = "******";
    void checkword(const char* word2bguessed, char* usercorrect); //I'm giving you the function prototype as a hint
    //after passing into function word2bguessed = "cheese", usercorrect = "**ee*e"
    The upside to this is that you can now use the strcmp function to compare word2bguessed and usercorrect to see if the user has guessed all the letters in the word.

    Hope that helps.

Page 1 of 4 1234 LastLast
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