GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 10-Jun-2004, 06:49
NigelZen NigelZen is offline
Invalid Email Address
 
Join Date: Jun 2004
Posts: 1
NigelZen is on a distinguished road

Small help with this program please


I've been developing a simple hangman program in C for a collage course i'm doing. It complies and runs (mostly) but it has an problem that i'm scratching my head about. In the Rungame function it doesn't seem to pick up the...

CPP / C++ / C Code:
if(Correct == 0)
         {
         	printf("Sorry, bad guess\n");
            state++;
			  }

...section of the code. Any pointers in the right direction would be very useful. The code is below.


#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>

#define MAX 20

void Rungame(char Words[][21]);

int main()

{
	char Continue = 'Y';
	char Words[20][21]={"computer","keyboard","mouse",
		"processor","memory","screen"};
	printf("*********** Hangman Game ************\n\n");
	printf("In this game of hangman you will have 8 lives\n\n");

	if(Continue == 'Y' || Continue == 'y')
	{
	 Rungame(Words);
	 printf("\n\nDo you wish to play again? (Y or N)\n");
	 scanf("%c",&Continue);
	}
	{
    //printf("Thanks for playing\n");
    //system ("PAUSE");
	//return ;
	}
}

void Rungame(char Words[][21])
{
   int Word;
   int Size;
   int state=1;
   int Count=0;
   char Guess[MAX];
   char Copy[MAX];
   char Letter;
   int Correct=0;

   srand((unsigned)time( NULL ));
   Word = rand() %3;
   strcpy(Copy,Words[Word]);

   Size = strlen(Words[Word]);

   for(; Count < Size; Count++)
   {
   	Guess[Count] = '-';
   }

   Guess[Count] = '\0';

   while(state<=8)
 {
      printf("Guess a letter :");
      scanf("%c",&Letter);

      for(Count = 0; Count < Size; Count++)
      {

			if(Copy[Count] == Letter)
			{
            	Guess[Count] = Letter;
           
               Correct = 1;
               printf("Good Guess!\n");
               printf("%s\n ",Guess);
			


			  if(Correct == 0)
         {
         	printf("Sorry, bad guess\n");
            state++;
			  }
			
					if(strcmp(Words[Word],Guess) == 0)
               {
               	printf("\nWell Done!! You have guessed the word!!\n");
                  return ;
			   }

            Correct = 0;
			
         }
            //printf("\nThe word was : ",Words[Word]);

	}
}
}
Last edited by JdS : 10-Jun-2004 at 08:12. Reason: Please enclose c code in [c] & [/c] for syntax highlighting
  #2  
Old 10-Jun-2004, 07:55
machinated machinated is offline
Regular Member
 
Join Date: Mar 2004
Location: victoria, canada
Posts: 324
machinated has a spectacular aura aboutmachinated has a spectacular aura about
for starters, you need a closing brace after the if (copy[count] == Letter ) Also change the if(continue ='y' ) clause in main to while if you want to repeat it. Also there is some problem with input stream. Can't be of much help there as i'm not familiar with c function scanf.
__________________
spasms!!!
  #3  
Old 10-Jun-2004, 13:37
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Quote:
Originally Posted by machinated
for starters, you need a closing brace after the if (copy[count] == Letter )

This is definitely the root of your problem, because the if(Correct==0) is never reached unless the letter is correct and if the letter is correct, then correct is set to 1....

Formatting of your code can really help you identify errors like this. It appears that you may have thrown an extra '}' at the end of the function to get it to compile. It really helps me out to be extra careful on the indentation to make sure that my opening and closing brackets line up. For instance:
CPP / C++ / C Code:
for(Count = 0; Count < Size; Count++)
{
    if(Copy[Count] == Letter)
    {
       ...
     }
}
  #4  
Old 29-Aug-2005, 00:17
Blue_knight Blue_knight is offline
New Member
 
Join Date: Aug 2005
Posts: 1
Blue_knight is on a distinguished road
Post

Solution.


Hi! I don't know if you still need it but i corrected the code so you can use it.

#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>

#define MAX 20

void Rungame(char Words[][21]);

int main()
{
char Continue = 'Y';
char Words[20][21] = {"computer", "keyboard",
"mouse", "processor", "memory", "screen"};

Rungame(Words);
}

void Rungame(char Words[][21])
{
int Word;
int Size;
int state;
int Count;
char Guess[MAX];
char Copy[MAX];
char Letter;

state = 8;
Count = 0;

Word = rand() %3;
strcpy(Copy,Words[Word]);

Size = strlen(Words[Word]);

for(; Count < Size; Count++)
{
Guess[Count] = '-';
}

Guess[Count] = '\0';

while(state >= 0)
{
printf("Guess a letter: ");
scanf(" %c", &Letter);


for(Count = 0; Count < Size; Count++)
{
if(Copy[Count] == Letter)
{
Guess[Count] = Letter;
printf("Good guess!\n");
pri ntf("%s\n ", Guess);
}
}
if(times == 0)
{
state = state - 1;
printf("Sorry, bad guess. You have %d lifes
left.\n", state);
}
if(strcmp(Words[Word], Guess) == 0)
{
printf("Well Done!");
return;
}
}
}
 
 

Recent GIDBlogProgramming ebook direct download available by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Need help with a C program (Long) McFury C Programming Language 3 29-Apr-2004 21:06
Modify a C program ayoub C Programming Language 3 15-Mar-2004 12:34
error during program rjd72285 C++ Forum 0 11-Nov-2003 19:49
one program access another? dgoulston C++ Forum 1 07-Oct-2003 12:26

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 15:45.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.