Click here to Skip to main content
15,885,904 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I tried to code hangman game in C for my schoolwork, but it doesn't work..
And my coding ability is total disaster...OMG and due date is this sunday...
when I play the code, it goes like this;

dog
a
*
NONONOr
NONONONONONONOd
NONONONONONONONONONONOg
NOYES*NONONONONONONONONONONONONOa
NONONONONONONONONONONONONONONONONONONO


I'll show the code below..

What I have tried:

I tried to show the word as '*****' at the start, and you should type the word first that you will use at the game.
And there's only 10 chances for guessing. if you fail to guess the answer in 10 times, the computer needs to show 'Fail' at the screen. Or if you success, computer will show 'Success'. But it doesn't work at all, and I don't know what's the problem. There is the code below




<pre>nclude <stdio.h>
#include <string.h>

int main(void)
{
	char arr[100];	
	int l, i, j, u, p;
	
	scanf("%s\n",arr);	
	l=strlen(arr);		 
	char blank[l]={"*"};	
	printf("%s\n" ,blank);
	
	for(i=1; i<=10; i++)	
	{
		scanf("%c", &u);	 
		
		for(j=1; j<=i; j++)
		{
			if(u==arr[j])	
			{
				blank[j]=u;
				printf("YES");	
				printf("%s", blank);
			}
			
			else
			{
			printf("NO"); 	 
			}
	
		}
	}
    		if (strcmp("arr", "blank")==0)	
			{ 

     				 printf("Success");		
					printf("%s", blank);	 					
           	 				            
    
			if(i==10)    		  
           	{
				 printf("Fail");	
			}
	return 0;
}
Posted
Updated 22-Jun-17 19:43pm

Start by stopping panicking - it never helps. Calm down, relax, and engage your brain! :laugh:

The first thing to do is learn to use the debugger: I can't give you explicit instructions because I don't know what system you are using and they all work differently, but they all have the same basic features. Google for the name of your development system (Visual Studio, Turbo C, whatever) and "debugger" and you should find instructions pretty easily.
Start with three things the debugger can do for you:
1) Breakpoint: when the application is running in the debugger and it reaches a line with a breakpoint, it stops, and passes control to you. Put a breakpoint in your code on this line:
C++
l=strlen(arr);
and you'll be fine.
2) Variables: once the debugger has passed control to you, you can use it to view the variables and their content - even while you run the code!
3) Stepping: you can tell the debugger "execute the next one line of code" and it will.

Think of the development process as writing an email: compiling successfully means that you wrote the email in the right language - English, rather than German for example - not that the email contained the message you wanted to send.

So now you enter the second stage of development (in reality it's the fourth or fifth, but you'll come to the earlier stages later): Testing and Debugging.

Start by looking at what it does do, and how that differs from what you wanted. This is important, because it give you information as to why it's doing it. For example, if a program is intended to let the user enter a number and it doubles it and prints the answer, then if the input / output was like this:
Input   Expected output    Actual output
  1            2                 1
  2            4                 4
  3            6                 9
  4            8                16
Then it's fairly obvious that the problem is with the bit which doubles it - it's not adding itself to itself, or multiplying it by 2, it's multiplying it by itself and returning the square of the input.
So with that, you can look at the code and it's obvious that it's somewhere here:
C#
private int Double(int value)
   {
   return value * value;
   }

Once you have an idea what might be going wrong, start using the debugger to find out why.

Think about what each line in the code should do before you execute it, and compare that to what it actually did when you use the "Step over" button to execute each line in turn. Did it do what you expect? If so, move on to the next line.
If not, why not? How does it differ?

This is a skill, and it's one which is well worth developing as it helps you in the real world as well as in development. And like all skills, it only improves by use!

Yes, I could probably tell you what "the problem" is - but it's not difficult to do this yourself, and you will learn something really worthwhile at the same time!
 
Share this answer
 
Comments
Member 13274680 23-Jun-17 1:55am    
I'm using Dev C++. Will debugger work the same in this programm too?
By the way, thank you for the kind comment:) I'm really beginner in coding, and your comment gave me a courage :D Thank you!!!
OriginalGriff 23-Jun-17 2:04am    
Yes - google for "Dev C++ debugger" and you will get loads of useful info.

You're welcome!
Member 13274680 23-Jun-17 2:54am    
I've used this video's steps to debug my code: https://www.youtube.com/watch?v=kHFpzxMFB3E

but 'system' code doesn't work in my programm. Can you tell me what the problem is?
OriginalGriff 23-Jun-17 3:09am    
No - I don't have Dev++.
Ignore YouTube videos - they are mostly made by idiots who have no idea what they are talking about.
The basics are covered in the first Google text hit:

http://eilat.sci.brooklyn.cuny.edu/cis1_5/HowToDebug.htm

Which should tell you want to do.
If that doesn't help, you need to tell us what "system code" is, and how it "doesn't work" - we can't see your screen!
use gotoxy() to position the cursor where you want - printf() prints starting at that position.


If your compiler on windows does not support gotoxy() use:

C++
#include <windows.h>
COORD coord;
  coord.X = <column>;
  coord.Y = <row>;
  SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);

You can print the "******" after using gotoxy() (or the above function)

Use getch() which does not echo the character and if it's valid (part of the word) replace the '*' in the appropriate position by this keyed in character again using gotoxy() before using printf("%c", <keyed_in_char>).

Use gotoxy to get user input only at a fixed row/column (use getch() #include <conio.h> for the purpose)

Likewise use gotoxy() to print your "YES" , "NO", "Success" at a fixed postion on the screen. Your screen can be something like this (assuming user guesses T, G, R correctly in the word TIGER):

Word: "T*G*R"

Feedback: YES/NO/Success

Enter your char:

Thus, you will be using only 3 rows on the screen.
BTW how can you have strcmp("arr", "blank") - you intended strcmp(arr, blank).

Just 1 tip:
you code:
C++
gotoxy(5, 10);  // column 5, row 10 col/ row values are 1 (and not 0) based
printf("Feedback: %-7s", "NO"); //  (or "YES" or "Success")
/* printing left justified in field width of 7 (length of "Success" the longest word of the 3) (%-7s) will pad with spaces and erase all previous output
*/

Later when you improve your skills you can enhance your program and even symbolically hang the user by drawing a stick figure (again you'll need gotoxy() or the workaround Windows function - bcc32 for Windows still supports gotoxy() but GCC doesn't)
 
Share this answer
 
v3
Comments
Member 13274680 23-Jun-17 2:04am    
Thank you:D I'll try that code and leave comment later!! By the way, is 'gotoxy' is kind of function, too?
RAMASWAMY EKAMBARAM 24-Jun-17 3:36am    
gotoxy() is a function - it was implemented in all C compilers for MS-DOS. Possibly, it is still there in compilers on Linux. Borland C++ freeware compiler v5.5 (bcc32) which runs in Windows Console still supports gotoxy().
There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 
Comments
Member 13274680 23-Jun-17 2:05am    
Thank you:D I'll try using debugger!!!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900