Partage

# hanged first part

4 juillet 2016 à 1:58:41

evening everyone... please i would like you to help me with my code.

when i execute the code it doesn't work normally : there are repetition .

this the code:

int research (char let,char MystW[],char UseW[]);

int win(int userW);

int main()

{

char mysteriousWord[] = "BLACK",letter = 0;

long i,tryout = 7;

int userWord[5] = {0},condition = 0;

printf("welcome in the hanged ...\n");

do

{

printf("you have %ld tryout left :\n",tryout);

printf("what is the mysterious word ...?   ");

for (i = 0;i < 5;i++)

{

if (userWord[i])

printf("%c",mysteriousWord[i]);

else

printf("*");

}

printf("\n\nenter a letter : ");

condition = research(letter,mysteriousWord,userWord);

if (!condition)

{

tryout--;

}

}

while (tryout > 0 && !winn(userWord));

if (winn(userWord))

printf("\ncongratulation ! you win in % ld tryout\nThe secret word was %s\n\n",mysteriousWord);

else

printf("\nhanged ! you lose \nThe secret word was  %s  hihihihihihihiiiii...\n\n",mysteriousWord);

return 0;

}

{

char letter = 0;

letter = getchar();

letter = toupper(letter);

return letter;

}

int research (char let,char MystW[],char UseW[])

{

long i;

int value = 0;

for (i = 0;MystW[i] != '\0';i++)

{

if (let == MystW[i])

{

UseW[i] = 1;

value = 1;

}

}

return value;

}

int winn(int userW[])

{

long i;

int condition = 1;

for (i = 0;i < 5; i++)

{

if (userW[i] == 0)

condition = 0;

}

return condition;

}

4 juillet 2016 à 2:26:13

Hello there,

could you take some time to enapsulate your code with the appropriate code button right at the top of the input box ? It could spare us some time as some mistake can easily be seen right away that way.

@Line 15 : you declare an int* and try to use it in your research funciton (@Line 45), but the prototype of the said function mention a char* to be used.

@Line 57 : you mispelled the function name.

@Line 64 : the formated string suggest you want to display a long (%ld) and a char* (%s). But you only specified a char* variable. Missing one before.

Also don't know if you correctly used the following includes :

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

Should already solve some problems.

5 juillet 2016 à 1:13:24

thanks for your help Amin ..

i try to modify it but i still have some trouble : let me show you

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

char getWord();
int findOut(char SecretWord[],char playCarac,int Correct[]);
int win(int CorrectW[]);

int main()
{
char secretWord[] = "BLACK";
char playerCaracter = 0;
int correct[5] = {0},test = 0;
long i = 0,tryout = 10;

printf ("welcome in the hanged !\n\n");

do
{
printf("You have %ld tryout\nWhat is the secret word ? ",tryout);

for (i = 0; i < 5;i++)
{
if (correct[i])
printf("%c",secretWord[i]);
else
printf("*");
}
printf("\n\n");

printf("enter a letter : ");
playerCaracter = getWord();

test = findOut(secretWord,playerCaracter,correct);

if (!test)
tryout--;

else
continue;

}
while (tryout > 0 && correct != 1);

if (win(correct))
printf("congratulation ! \nyou win \nThe secret word is %s !",secretWord);
else
printf("Hanged ! you lose \nThe secret word was %s ! hihihi...",secretWord);

return 0;
}

char getWord()
{
char playLetter = 0;

playLetter = getchar();
playLetter = toupper(playLetter);

return playLetter;
}

int findOut(char SecretWord[],char playCarac,int Correct[])
{
long j = 0;
int answer = 0;

for (j = 0;SecretWord[j] != '\0';j++)
{
if (playCarac == SecretWord[j])
{
Correct[j] = 1;
}
else
continue;
}

}

int win(int CorrectW[])
{
long i = 0;
int ans = 1;

for (i = 0; i < 5; i++)
{
if (CorrectW[i] == 0)
ans = 0;

}

return ans;
}



5 juillet 2016 à 3:28:31

@Line 45 : you tried to compare an int[] with an int. I guess what you wanted was to compare if the player has won or still trying. I think you should use the return value of your function win. You'll see your program working way better.

Also, may I suggest you to use the debugger that is attached with your IDE ? It will be way more easier for you to search and fix for errors on your own than posting them here (not that it bothers me).

If you compile with a compiler like Clang or GCC (by "hand"), you can trigger warning to be treated as error with the option -Werror. Like that, you can easily see warnings a.k.a errors and fix them as the compiler is pretty talkative about them. Here is an example I always use on my Clang compiler (working with GCC as well) :

clang -std=c11 -O3 -Wall -Wextra -Werror -pedantic -o main *.c && ./main  // ./main to execute Linux executable files

If you're using an IDE instead, you can refer to the documentation of the said IDE to know how to enable warnings to be treated as errors.

-
Edité par Anonyme 5 juillet 2016 à 3:30:32

5 juillet 2016 à 12:05:35

thanks one small Amin... but it doesn't work..i have the same problem..

i try NANO on Ubuntu but it is not easy to find out error on it...

for the moment i download CodeBlock for ubuntu. but sometime it crash and stop functioning...

so i would like you please to show me another IDE that i can use on UBUNTU...

i also have SCITE but i don't really know how it works.

5 juillet 2016 à 13:01:02

Weird. I tried replacing the line 45 like that :

while (tryout > 0 && win(correct) != 1);

And haven't got any issue sor far with your code.

Also, If you're compilling "by hand" I would suggest you swiching to Sublime Text immediately. Because Nano is so much not convenient for wrting efficient code.

Sublime Text have way more advantages like his black theme allowing to code by night without getting onion eyes the following next . Also, it support code completion. Even if it's an extra feature on Nano, Sublime Text is a way more sexy for writing code.

Also, if you're using GCC, I suggest you turning on the warning like that when you'll compile again :

gcc -Werror -o myProgram

Replace your program name in myProgram in the code above. Then you can still run you code like usual with :

./myProgram

If you want to use an IDE, I would have suggest you to use Code::Blocks but it seems buggy at the moment. May be you'll get more luck with NetBeans or Eclipse. The first is relatively simple to use (though, it uses Makefile even for small project so it may not be that easily after all). The second is pretty much the same, except it use an interface very... special. With lots of windows. You can easily lost sight of your project under Eclipse in my opinion and I have got an awful experience with it uner Archlinux.

-
Edité par Anonyme 5 juillet 2016 à 13:02:44

5 juillet 2016 à 21:27:53

thanks for your help...

i work on my code today and i finally find out the error...

i forgot the following condition in my function getWord() :

while (getchar() != '\n');

now it work perfectly... it think that i will continue work with nano to become more efficient and with code block even if it seems having trouble and i will wait for the new version...

thanks one small...!

6 juillet 2016 à 1:12:43

Even better :

int c = 0;

while((c = getchar() != '\n') && c != EOF);

You one brave guy dear sir. Hope you'll keep up the good work. Unfortunately, i'm not that brave to work with either nano or vim.

6 juillet 2016 à 10:36:20

don't make me laugh...i'm sure you are more brave than a hundred persons in one

## hanged first part

× Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
× Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
• Editeur
• Markdown