Thread: REALLY need some tips on this simple letter guessing code

  1. #1
    Registered User
    Join Date
    Jun 2020
    Posts
    9

    REALLY need some tips on this simple letter guessing code

    Its meant to return a 1 if the inputted char is in the word "asenine". and return 0 if it isnt, am i going about this the right way?

    any help appreciated, thanks!

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    #define _CRT_SECURE_NO_WARNINGS
    int letterguess(char* text, int a);
    
    
    
    
    int main(void)
    {
      char word[] = "asenine";
      int x = strlen(word);
      char input;
    
    
      printf("%d", letterguess(word, x));
    
    
    
    
      return 0;
    }
    
    
    
    
    
    
    int letterguess(char* text, int a)
    {
    	char input;
      scanf("%c", input);
    
    
      for (int i = 0; i < a ; i++)
    	{
        if (text[i] == input)
    		{
    		return 1;
    		}
        else
        {
        return 0;
        }
    	}
    
    
    
    
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Trace through the loop with some example input, e.g. 'e'. How many iterations does the loop loop over?

    By the way, you need to indent your code properly. This is especially helpful when you're trying to trace through the logic of your code.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    1. Indentation.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define _CRT_SECURE_NO_WARNINGS
    int letterguess(char *text, int a);
    
    int main(void)
    {
      char word[] = "asenine";
      int x = strlen(word);
      char input;
      printf("%d", letterguess(word, x));
      return 0;
    }
    
    int letterguess(char *text, int a)
    {
      char input;
      scanf("%c", input);
      for (int i = 0; i < a; i++) {
        if (text[i] == input) {
          return 1;
        } else {
          return 0;
        }
      }
    }
    Now you can easily see that the for loop in letterguess only runs once. It doesn't matter whether there is a match or not, it's returning either way.


    Second, watch your scanf formats.
    Code:
    $ gcc -Wall foo.c
    foo.c: In function ‘main’:
    foo.c:12:8: warning: unused variable ‘input’ [-Wunused-variable]
       char input;
            ^
    foo.c: In function ‘letterguess’:
    foo.c:20:9: warning: format ‘%c’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat=]
       scanf("%c", input);
             ^
    foo.c:28:1: warning: control reaches end of non-void function [-Wreturn-type]
     }
     ^
    foo.c:20:3: warning: ‘input’ is used uninitialized in this function [-Wuninitialized]
       scanf("%c", input);
       ^
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need Help with Letter Guessing Game
    By topgun97 in forum C Programming
    Replies: 1
    Last Post: 02-18-2018, 09:44 PM
  2. Letter Guessing Game
    By Kiken_ in forum C Programming
    Replies: 3
    Last Post: 09-24-2015, 05:31 PM
  3. help with c letter guessing game
    By Daniel Aarons in forum C Programming
    Replies: 2
    Last Post: 02-10-2015, 04:33 PM
  4. Letter Guessing Game
    By jlowe2013 in forum C Programming
    Replies: 11
    Last Post: 09-24-2014, 06:32 PM
  5. Need help with letter guessing program
    By ltdec in forum C Programming
    Replies: 31
    Last Post: 10-05-2011, 12:29 AM

Tags for this Thread