Good day everyone!

I would like to ask some problem about the program I am currently working (hangman to be specific). Since I am just a beginner, there are many things I would like to clarify. I would like to ask your help so that I can enable more to see what are my errors in my code. It is too many to ask. Please bear with me and sorry for the inconvenience.

1. I do have 4 categories (three rounds). The user will choose a category. For every round, a different category. You cannot choose the same category in different rounds. After choosing a category, the game will provide a random word. At this moment, i have done only the random part (and it might some errors too.)

2. I am also having difficulty in underscores and spaces. You see, I only guessed that part. I do not know if that will work (or maybe it will destroy my whole program.)

3. Will you give me some advices on how to check inputs and how to display the letters which are used already for a specific round? I am not merely asking for code (though it will be gladly accepted it) but some detailed explanations.

You see, my program is not yet finished. Sorry if you are kind of frustrated to see such kind of code. I have spent a week just for that long code and my head is spinning already. But then, I really want to understand this language. I just need some guidance from the experts and experienced people out there.

I have attached my code (for the hangman program). Please check it.

Thank you in advance. Hope that you can help me with this as soon as you can.

Recommended Answers

All 11 Replies

maybe this will be of some use. Its C++ version of hangman.
Its from C++ Primer Plus 5th edition. I copied and pasted for you.

// hangman.cpp -- some string methods
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <cctype>

using std::string;

const int NUM = 26;

const string wordlist[NUM] = {"apiary", "beetle", "cereal",
"danger", "ensign", "florid", "garage", "health", "insult",
"jackal", "keeper", "loaner", "manage", "nonce", "onset",
"plaid", "quilt", "remote", "stolid", "train", "useful",
"valid", "whence", "xenon", "yearn", "zippy"};
int main()
{
	using std::cout;
	using std::cin;
	using std::tolower;
	using std::endl;

	std::srand(unsigned(std::time(0)));

	char play;

	cout << "Will you play a word game? <y/n> ";
	cin >> play;
	
	play = tolower(play);

	while (play == 'y')
	{
		string target = wordlist[std::rand() % NUM];
		int length = target.length();
		string attempt(length, '-');
		string badchars;
		int guesses = 6;
	cout << "Guess my secret word. It has " << length
	<< " letters, and you guess\n"
	<< "one letter at a time. You get " << guesses
	<< " wrong guesses.\n";
	cout << "Your word: " << attempt << endl;
	
	while (guesses > 0 && attempt != target)
	{
			char letter;
			cout << "Guess a letter: ";
			cin >> letter;
		if (badchars.find(letter) != string::npos
			|| attempt.find(letter) != string::npos)
			{
				cout << "You already guessed that. Try again.\n";
				continue;
			}

		int loc = target.find(letter);
		if (loc == string::npos)
		{
			cout << "Oh, bad guess!\n";
			--guesses;
			badchars += letter; // add to string
		}
		else
		{
			cout << "Good guess!\n";
			attempt[loc]=letter;
			// check if letter appears again
			loc = target.find(letter, loc + 1);
			while (loc != string::npos)
			{
				attempt[loc]=letter;
				loc = target.find(letter, loc + 1);
			}
		}
		cout << "Your word: " << attempt << endl;
		if (attempt != target)
		{
			if (badchars.length() > 0)
			cout << "Bad choices: " << badchars << endl;
			cout << guesses << " bad guesses left\n";
		}
	}
	if (guesses > 0)
		cout << "That’s right!\n";
	else
		cout << "Sorry, the word is " << target << ".\n";
		cout << "Will you play another? <y/n> ";
		cin >> play;
		play = tolower(play);
	}
	cout << "Bye\n";

	return 0;

}

I have already finished my program. There are several errors (problems I am encountering) in it. Please help me with this.

1. If one category was chosen by the user, it must not been chosen again in the next round. How can I do such thing?

2.

int inputerror(char b, char* h)
{
    int i;
    char alpha[27];
    strcpy(alpha, "ABCDEFGHIJKLMNOPQRSTUVWXYZ");

    // change letters used from the alphabet to '_'
    for (i = 0; alpha[i] != '\0'; i++)
    {
        if ((alpha[i]) == h)
        {
            alpha[i] = '_';
        }
    }

    // pad with spaces so it appears in the middle of the screen
    printf("              ");

    for (i = 0; alpha[i] != '\0'; i++)
        printf("%c ", alpha[i]);
    printf("\n\n");

}

I am having difficulty with this code.
a. under what library(i.e stdio, stdlib) is strlwr, tolower and _tolower? do you think it requires in my code?
b. Is this code ok?

3. And how to do the part when the user inputs a letter, the underscores will be replaced by letter in a correct order?

Here are just some of my questions. Please see the attached file below for the whole code. If you see other errors, please tell me. I really need your advices regarding with this case.

Thanks in advance!

If you look at the code I posted, you would see that
there is a badchar string, which hold all the guesses.
when the user enters a letter, it checks if he already guessed that
by checking if its in badchar.

You can do similar in c.

char badchar[256] = "";
int badCharIndx = 0;
//get user input
//check if user's input exist in badchar
//if not then add it into badchar
badchar[badCharIndx++] = user's input

It is quite a help though what i need is that when a letter was input (by the user), like in the hangman, all the letters used in the puzzle will be marked by a line or an asterisk. it does not really need to have the badchar though.

in you while loop display all letters(maybe on the right side of the screen).
And turn by turn, remove the letters that has been picked. So the user
can see which letters he could get to choose from.

Answers
1) take array of int catg[4] = {0, 0, 0, 0}
now once a category is chosen set that array index.
suppose catg 1 is chosen then catg[0] = 1;
insert code to test if catg is set, yes break

 if (categoryno == 1)

    {

        if( catg[0])
        {
           printf("choose other catg")
           return;
       }
        /*your code*/

    }

2) ctype.h

3) reprint at the same place. use printf("b") to do this.

 printf("forum");
 printf("bbbbbb");
 printf("hello");

>>codeguru_2009
please see this code:

int catg[4]={0,0,0,0};

printf("Please choose a category\n");
printf("1\n");
printf("2\n");
printf("3\n");
scanf("%d", categoryno);

if(categoryno==1)
{
   catg[0]=1;
   if( catg[0])
   {
          printf("choose other catg");
   }
     return;
}

if(categoryno==2)
{
   catg[1]=1;
   if( catg[0])
   {
          printf("choose other catg");
   }
     return;
}

"insert code to test if catg is set, yes break "

--) i do not get it. how is that?? :|

My answers were not complete. all was hint only.
It works like this

void Category (int categoryno)
{

    int n, z=3;
    if (categoryno == 1)
    {
            if(catg[0]) 
           {
               printf("category already chosen");
               return;
           }
            catg[0] = 1;
             n=rand()%z;//randoms the Salita[n].Word
            strcpy(Salita[0].Category, "Academic Building\0");
            strcpy(Salita[0].Word, "College of Arts and Letters\0");
            strcpy(Salita[1].Word, "The UP Alumni Engineer's Centennial Hall  Department of Computer Science and Engineering Library II\0");
            strcpy(Salita[2].Word, "National Institute of Geological Science\0");

           Salita[n].Word;

        }
        else if (categoryno == 2)
        {
                if(catg[1]) 
               {
                    printf("category already chosen");
                    return;
               }
               catg[1] = 1;
               n=rand()%z;
               strcpy(Salita[1].Category, "Food Trip!\0");
              strcpy(Salita[0].Word, "Mcdonald's\0");
              strcpy(Salita[1].Word, "Lutong Bahay ( since 1977 )\0");
              strcpy(Salita[2].Word, "Food Stalls\0");
             Salita[n].Word;
        }
        else if (categoryno == 3)
        {
             if (catg[2]) 
            {
                 printf("category already chosen");
                return;
           }
           catg[2] = 1;
            n=rand()%z;
            strcpy(Salita[2].Category, "Phrases/Quotes anyone?\0");
            strcpy(Salita[0].Word, "You never know what you have till you lose it. You never get it back./n -snatcher/0");
            strcpy(Salita[1].Word, "Beware, so long as you live, of judging men by their outward appearance.\n -Jean de La Fontaine\0");
            strcpy(Salita[2].Word, "Curiosity. The reason why most of us haven't commited suiucide long ago.\n -Anonymous\0");
            Salita[n].Word;
        }
        else if (categoryno == 4)
        {
               if(catg[3]) 
              {
                   printf("category already chosen");
                  return;
              }
              catg[3] = 1;
              n=rand()%z;
             strcpy(Salita[3].Category, "Himitsu! Karu-chan's Category\0");
             strcpy(Salita[0].Word, "Only the ring finger knows - the ring will confess his love\0");
             strcpy(Salita[1].Category, "Lelouch Vi Britannia --) Lelouch Lamperouge\0");
            strcpy(Salita[2].Word, "Uragiri wa boku no namae wo shitteiru\0");
            Salita[n].Word;
        }

    return ;// since u r making no use uf return value from category function make it as void.
}

hope this will help you

I got one idea though coz hang man doesnt require junk genrator names unless you do a real name genrator my idea is have the program read from you 3 names for the movies or any other names and it will choose randomly between the 3 or more if you want to then after ur done with them u can rewrite them or having program calloc for more if you need more than 3 names

I am having an error as I am editing my code

"warning: passing arg 1 of 'Round1' from the incompatible pointer type"

what does that mean? And how to solve such warning?

#include<stdio.h>

void Round1(char *Salita);

char Salita[3][100];

int main (void)
{
   //the user will ask the user the categories to be used for nth round
  //from the if statements, the code will produce Salita[n]
   //function call
   Round1(&Salita[n]; //is this correct? 
   ...
}

void Round1(char *Salita)
{
   ....
}

Function Round1() prototypes(line 3) says Its arguments should be a char pointer. so you can pass single dimension array as an argument when calling Round1.

calling function
{
  char *ptr;   // or char ptr[]
 Round1(ptr);  
}
Round1(char* pointer)  // or Round1(char pointer[])
{
//body
}
 // for double dimension array
 
calling function
{
  char **ptr;   // or char ptr[][x]x=positive integer
 Round1(ptr);  
}
Round1(char** pointer)  // or Round1(char pointer[][x]) 
//in case of 2D array you should always mention no. of columns.
{
//body
//use pointer as 2D
}

Yours way of calling function Round1 is correct.
But Round1 definition and prototype for arguments should contain 2D array declaration.
Round1(char salita[][100])
When you call like Round1(&salita[1]), then in salita in Round1 will have first index element as zeroth index element.

You are immediately adviced to read C book by Denis Ritchie.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.