0

I wrote a Hangman game.

The problem is that when I go into main again using a menu system after the game ends to go to main again by calling the main function, the gets function just does not ask me for the string to be entered again and just assigns a blank to the string.

P.S.: I am using DevC++.

#include<stdio.h>
#include<strings.h>

main()
{
    int c,w=0,i,q,j,y,o,wrong,correct=0;
    c=0;
    wrong=0;
    char a,us='_',space=' ',x,ch;
    correct=0;
    printf("               %c",201);
    for(i=0;i<=8;i++)
        printf("%c",205);
    printf("%c",187);
    printf("\n");
    printf("               %c Hangman %c\n",186,186);
    printf("               %c",200);
    for(i=0;i<=8;i++)
        printf("%c",205);
    printf("%c\n",188);
    //  printf("  O  \n /|\\ \n/ | \\ \n / \\ \n/   \\\n");
    printf("\n |----|\n |    O  \n |   /|\\ \n |  / | \\ \n |   / \\ \n_|_ /   \\\n");
    char name[100];
    printf("Enter the name of the Movie:");
    gets(name);
    //  scanf("%s",&name);
    printf("The Movie you entered was: ");
    puts(name);
    int len=0;
    len=strlen(name);
    //  printf("\n%d",len);
    char guess[len];
    printf("\nPress any key and pass it to the person who has to guess the movie:");
    getch();
    printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
    printf("\nWelcome!\nGuess the name of the movie:\n");
    //  printf("  O  \n /|\\ \n/ | \\ \n / \\ \n/   \\\n");
    printf(" |----|\n |    O  \n |   /|\\ \n |  / | \\ \n |   / \\ \n_|_ /   \\\n");
    for(q=0;q<len;q++)
    {
        y=name[q];
        if ( y=='a' || y=='A' || y=='e' || y=='E' || y=='i' || y=='I' || y=='o' || y=='O' || y=='u' || y=='u' )
        {
            //  printf("%c",name[q]);
            guess[q]=name[q];
            c=c+1;
        }
        else if(y==' ')
        {
            //  printf("%c",space);
            guess[q]=space;
            c=c+1;
        }
        else
        {
            //  printf("%c",us);
            guess[q]=us;
            c=c+1;
        }
    }
    for(i=0;i<len;i++)
        printf("%c",guess[i]);
    while(1)
    {
        printf("\nGuess a letter: \n");
        scanf(" %c",&x);
        //  printf("%c\n",x);
        w=0;
        for(i=0;i<len;i++)
        {
            if(x==name[i]-32||x==name[i]+32||x==name[i])
                guess[i]=x;
            else
                w=w+1;
            if(w==len)
                wrong=wrong+1;
        }
        //  printf("\nw=%d\nlen=%d\n",w,len);

        for(i=0;i<len;i++)
            printf("%c",guess[i]);
        correct=0;
        //      printf("  0  \n /|\\ \n/ | \\ \n / \\ \n/   \\\n");
        for(i=0;i<len;i++)
        {
            if(guess[i]==name[i])
                correct=correct+1;
            //  printf("\n%d",correct);
        }   
        if(correct==len)
        {
            printf("\nCongratualtions!!!\nYou've Won!!!");
            printf("\nEnter 'R' to Restart Or 'Q' to Quit:\n");
            o=getch();
            if(o=='R'||o=='r')//Restart or quit menu
            {
                printf("\n");
                main();
            }
            else
                return 0;
        }
    //      printf("\n%d\n%d",correct,wrong);
        if(wrong==0)
            printf("\n  O  \n /|\\ \n/ | \\ \n / \\ \n/   \\\n");   
        if(wrong==1)
            printf("\n  O  \n /|\\ \n/ | \\ \n / \n/   \n");        
        if(wrong==2)
            printf("\n  O  \n /|\\ \n/ | \\ \n ");
        if(wrong==3)
            printf("\n  O  \n /| \n/ | \n");
        if(wrong==4)
            printf("\n O  \n | \n |" );
        if(wrong==5)
            printf("\n  O ");
        if(wrong==6)
        {
            printf("Oops You Ran Out Of Turns\n==========\nGAME OVER!!!\n==========\nThe Movie Was:\n");
            for(i=1;i<len;i++)
                printf("%c",name[i]);
            printf("\nEnter 'R' to Restart Or 'Q' to Quit:\n");
            o=getch();
            if(o=='R'||o=='r')//Restart or quit menu
            {
                printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
                main();
            }
            else
                return 0;
        }
        printf("------------------------------------------");
    }
}

enter image description here

DevSolar
  • 67,862
  • 21
  • 134
  • 209
PsNeo7
  • 1
  • 6
    There is no `gets()` in the language C. Not anymore. And good riddance. -- Please use correct indentation. Please **always** use `{}` to indicate where loops begin and end, even for one-line loop bodies. Please ask your question more clearly. Please [*identify* a *specific* programming problem](http://www.stackoverflow.com/help/mcve), don't dump full programms on us and ask us to fix them. – DevSolar May 02 '16 at 11:48
  • 1
    I am pretty sure you meant to write ``, not ``. Only declare one variable per line. Pad operators with spaces (i.e. `if ( x == y )` instead of `if(x==y)`), this helps readability immensely. [Never use `*scanf()` on human input.](http://stackoverflow.com/questions/35178520/how-to-read-parse-input-in-c-the-faq) Don't get into the habit of `getch()` and similar non-standard functions while still experimenting; build on the standard functions (like `fgets()`). – DevSolar May 02 '16 at 12:06
  • user3121023 is right BTW... check [this answer](http://stackoverflow.com/questions/35178520), the section "when *scanf() does not work as expected" for an explanation. – DevSolar May 02 '16 at 12:13
  • Overall, you need to consider why you are using over 25 years old programming methods. Your source of learning C is completely outdated and needs to be replaced. – Lundin May 02 '16 at 12:55
  • @DevSolar Sir , i guess i should've been more clear regarding my question . I do not expect to dump my code and ask people to solve it, maybe i just came out wrong . I just thought that if i show my code , it would be easier to spot the specific problem i have.Thank you for the inputs regarding the indentations will follow them for sure . I also later released that i had no comments added to help understand my problem better .Read the sources about gets() and will try to avoid it as much as possible . Thank you – PsNeo7 May 03 '16 at 17:51
  • @user3121023 Thaks ,,,, need to look out for the new lines , made the same mistake recently. – PsNeo7 May 03 '16 at 17:53
  • @Lundin Well I was just using some text books. What sources do u recommend? – PsNeo7 May 03 '16 at 17:54

1 Answers1

-2

Try putting this code before the call of the main()

fflush(stdin);
jnapor
  • 63
  • 1
  • 1
  • 9
  • 5
    `fflush`ing `stdin` is **undefined behavior**. See this: http://stackoverflow.com/questions/2979209/using-fflushstdin – HolyBlackCat May 02 '16 at 12:50