Going nuts with challenge 2

+2 Haris Razis · January 24, 2015
This is my second challenge 



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

int main()
{
    srand((unsigned)time(NULL));
    int dice1, dice2, dice3, dice4, dice5, dice6 = 0;
    int sum1, sum2 = 0;
    char anwser;
    char startthegame[1];

    printf("Welcome to the dice roller 4000!\nIn this game a dice will roll 3 times and the result of each time will be added together,then you will have to make a prediction on what the next sum will be\npress any key to initiate dice roller protocol!");
    scanf("%c",startthegame);


        for(dice1=0; dice1<=1; dice1++){
            dice1 = ( rand()%6 ) + 1;
    printf("Dice roll 1: %d\n", dice1);
    }

    for(dice2=0; dice2<=1; dice2++){
            dice2 = ( rand()%6 ) + 1;
    printf("Dice roll 2: %d\n", dice2);
    }

    for(dice3=0; dice3<=1; dice3++){
            dice3 = ( rand()%6 ) + 1;
    printf("Dice roll 3: %d\n", dice3);
    }

    sum1=dice1+dice2+dice3 -3;



    printf("The sum of the first 3 dices is %d\n", sum1);
    printf("Time to decide!\n will the sum of the next 3 rolls be more than the 1st sum?(press H)\n will it be the same?(press S)\n or it will be less?(press L)\n");
    scanf(" %s", &anwser);


    for(dice4=0; dice4<=1; dice4++){
            dice4 = ( rand()%6 ) + 1;
    }

    for(dice5=0; dice5<=1; dice5++){
            dice5 = ( rand()%6 ) + 1;
   }

    for(dice6=0; dice6<=1; dice6++){
            dice6 = ( rand()%6 ) + 1;
   }

   sum2=dice4+dice5+dice6 -3;

   switch(anwser){
   case 'H':if(sum2 < sum1){printf("nope");
            }else if(sum2 = sum1){printf("nope");
            }else if(sum2 > sum1){printf("nice one");}
            break;

   case 'S':if(sum2 < sum1){printf("nope");
            }else if(sum2 = sum1){printf("nice one ");
            }else if(sum2 > sum1){printf("nope");}
            break;

   case 'L':if(sum2 < sum1){printf("Nice one ");
            }else if(sum2 = sum1){printf("nope");
            }else if(sum2 > sum1){printf("nope");}
            break;
   default:printf("You cant even follow one bitch ass order?");

}


   return 0;
}



i cant get whats wrong with is but every time i run it i insert my desicion and then nothing,it simple ends there with all the code sitting in the bottom...
help me im going crazy






Post a Reply

Replies

Oldest  Newest  Rating
+1 Jag Singh · January 25, 2015
I just added the header file #include <iostream> 
and it started working. PFB the output.


Welcome to the dice roller 4000!
In this game a dice will roll 3 times and the result of each time will be added together,then you will have to make a prediction on what the next sum will be
press any key to initiate dice roller protocol!Dice roll 1: 5
Dice roll 2: 6
Dice roll 3: 1
The sum of the first 3 dices is 12
Time to decide!
will the sum of the next 3 rolls be more than the 1st sum?(press H)
will it be the same?(press S)
or it will be less?(press L)
You cant even follow one bitch ass order?
+1 Jag Singh · January 25, 2015
Not sure if I am getting your question right. Are you saying that after putting your decision the program window(command prompt) shuts down on you ?
If that is the case try putting a System("Pause"); or a char.getch(); just before return 0;
that way you will be able to see your results.
+2 Joe Long · January 25, 2015
Great job on the code! I'm new to programming and I've watched up until the challenge 2 video so far. I spent the last hour looking at your code to try to figure out the problem and i think i finally found it. It was in this section:




switch(anwser){ //For some reason this math is wrong, 'L' should be 'H' and vice versa, can't figure out how to fix it...
case 'H':if(sum1 < sum2){printf("nope");
}else if(sum1 = sum2){printf("nope");
}else if(sum1 > sum2){printf("nice one");}
break;

case 'S':if(sum1 < sum2){printf("nope");
}else if(sum1 = sum2){printf("nice one ");
}else if(sum1 > sum2){printf("nope");}
break;

case 'L':if(sum1 < sum2){printf("Nice one ");
}else if(sum1 = sum2){printf("nope");
}else if(sum1 > sum2){printf("nope");}
break;
default:printf("You cant even follow one bitch ass order?");
}
printf("\n\nThe sum of the last 3 dices is %d\n", sum2); //put here to check if the math is correct
0 Haris Razis · January 25, 2015
no matter what i press the result is this

http://postimg.org/image/c0lf7gjdn/d266c805/

tried what you said joe and the result is still the same except i insert a wrong letter,which prompts me to default and then displays the sum

http://postimg.org/image/jf0e2gqsl/a525cd79/
0 Haris Razis · January 25, 2015
Joe sent me the some code and it seems to work, special thanks to him as well as to you Jag for trying to help my out :)



case 'H':if(sum1 < sum2){printf("nope");
}else if(sum1 = sum2){printf("nope");
}else if(sum1 > sum2){printf("nice one");}
break;

case 'S':if(sum1 < sum2){printf("nope");
}else if(sum1 = sum2){printf("nice one ");
}else if(sum1 > sum2){printf("nope");}
break;

case 'L':if(sum1 < sum2){printf("Nice one ");
}else if(sum1 = sum2){printf("nope");
}else if(sum1 > sum2){printf("nope");}
break;
default:printf("You cant even follow one bitch ass order?");
}
printf("\n\nThe sum of the last 3 dices is %d\n", sum2);


0 Lazar Bulic · January 25, 2015
Correct your #include tags:

You only need these:

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

and your code has some syntax error which pass the compiler but aren't good practice.
0 c student · January 26, 2015
stdlib is required for srand() and rand().

also you're scanning an expected type of string into a char type answer, meaning you may have an invalid write.
0 Haris Razis · January 26, 2015
"also you're scanning an expected type of string into a char type answer, meaning you may have an invalid write. "

i know had it as a %c before but tried everything because the program would not function
0 c student · January 26, 2015
find a solution for it.  don't leave your program with invalid access...
0 Haris Razis · January 27, 2015
http://www.tutorialspoint.com/cprogramming/c_operators.htm
  • 1

C

107,319 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator