• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

Problem with my Code, crashing everytime I input my choice[C programming]

Status
Not open for further replies.
Level 6
Joined
Oct 10, 2013
Messages
173
So I was planning to make match stick game for my friends, and then I came up with 116 line code hand written in 2 hours. Now I am facing this irritating problem Imgur: The most awesome images on the Internet

Here is the code
Code:
#include<stdio.h>
#include<conio.h>
void main()
{
    char first;
    int pick,second,third,fourth;
    printf("Hello Human,\n you wanna play a game with me?\nReply with Y to play, or else press any other key to exit\n");
    scanf("%c",first);
    if(first=='y'||first=='Y')
    {
        printf("Ok I say see you want to play with me\n The rules are simple.\nWe have total of 21 matchsticks, and we will pick matchsticks amount from 0 to 5 at every pick\n The player who gets the last 21th matchstick will loose\n");
        printf("Since you are puny, I will grant you first pick for fair play\n");
        printf("Select an amount of matchsticks, from 0 to 5. Note that you cannot pick 0 or 5 match stick\n");
        scanf("%d",&pick);
        if(pick==1)
        {
         printf("Hmm, In this case I shall pick ||||(4) matchstick\n");
         printf("Now, there are ||||||||||||||||(16) matchsticks are left.Lets continue the game\n");


        }
        if(pick==2)
        {
            printf("Hmm, In this case I shall pick |||(3) Matchstick\n");
            printf("Now, there are ||||||||||||||||(16) matchsticks are left.Lets continue the game\n");

        }
        if(pick==3)
        {
            printf("Hmm, In this case I shall pick ||(2) Matchstick\n");
            printf("Now, there are ||||||||||||||||(16) matchsticks are left.Lets continue the game\n");

        }
        if(pick==4)
        {
            printf("Hmm, In this case I shall pick |(1) Matchstick\n");
            printf("Now, there are ||||||||||||||||(16) matchsticks are left.Lets continue the game\n");
        }

        printf("Time for round 2, Human Lets go\nType in your amount of matchstick\n");
        scanf("%d",&second);
        if(second==1)
        {
        printf("Oh, Now I will take ||||(4) match stick\n");
        printf("Amazing Human,You have played well now |||||||||||(11) matchsticks are left.Lets continue this game");
        }
        if(second==2)
        {
        printf("Oh, Now I will take |||(3) match stick\n");
        printf("Amazing Human,You have played well now |||||||||||(11) matchsticks are left.Lets continue this game");
        }
        if(second==3)
        {
        printf("Oh, Now I will take ||(2) match stick\n");
        printf("Amazing Human,You have played well now |||||||||||(11) matchsticks are left.Lets continue this game");
        }
        if(second=4)
        {
            printf("Oh, Now I will take |(1) match stick\n");
            printf("Amazing Human,You have played well now |||||||||||(11) matchsticks are left.Lets continue this game\n");
        }
        printf("Well played human, two more rounds are left to see the winner\n");
        printf("Take your sticks");
        scanf("%d",&third);
        if(third==1)
        {
            printf("Woah, I will take ||||(4) sticks now\n");
            printf("Now humans ||||||(6) matchsticks are left\n");
        }
        if(third==2)
        {

            printf("Woah, I will take |||(3) stick now\n");
            printf("Now humans ||||||(6) matchsticks are left\n");
        }
        if(third==3)
        {
            printf("Woah, I will take ||(3) stick now\n");
             printf("Now humans ||||||(6) matchsticks are left\n");
        }
        if(third==4)
        {
             printf("Woah, I will take ||(3) stick now\n");
             printf("Now humans ||||||(6) matchsticks are left\n");
        }
        printf("And so ends the third round, lets move to last round human\n");
        printf("You know the drill\n");
        scanf("%d",fourth);
        if(fourth==1)
        {
            printf("Gotcha,I take ||||(4) match. Since you got one match, you are LOOSER");

        }
        if(fourth==2)
        {
             printf("Gotcha,I take |||(3) match. Since you got one match, you are LOOSER");
        }
        if(fourth==3)
        {
             printf("Gotcha,I take ||(2) match. Since you got one match, you are LOOSER");
        }
        if(fourth==4)
        {

        printf("Gotcha,I take ||||(4) match. Since you got one match, you are LOOSER");
        }

    }
    else
    {
        printf("OK BYE, THE CREATOR CREATED ME WITH LOVE FOR YOU");
    }

}
Please find out whats wrong, since I have been scratching my head from yesterday.
 
Level 6
Joined
Oct 10, 2013
Messages
173
Change: scanf("%c",&first);
I recommend using switch statement. switch statement in C

And you can also do this

Code:
if ( expresion ) printf( . . . );

There is no need for curly braces if you have one-liner so to say inside if statement.
Oh so he was the culprit, thanks for saying. My game worked like charm.
Also I was practising if, and else statements. So yea I could had used switch for good codes.
+REP
Thanks again for being a wind, beneath my wing.
 
Level 29
Joined
Jul 29, 2007
Messages
5,174
Always prefer to have curly braces, also where you are not forced to use them.
This is probably the most obvious error newcomers to C/C++ do.
Then they change their code and add a line to the if, forget it isn't attached to it, and waste a lot of time figuring why their code suddenly fails to work.

Also in the future, please use the [code=c] tag.

Now that you have this code that works, maybe you can figure how to use a function, to make it a lot shorter and generic (and even random, if you so desire) :)
 
Status
Not open for further replies.
Top