Second challenge

+4 Minh Quan Vu · November 30, 2014
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <math.h>

main ()
{

    int a, b;
    int diceRoll1, diceRoll2, diceRoll3;
    char c[20];

    srand(time(NULL));

    diceRoll1= (rand()%6) +1 ;
    diceRoll2= (rand()%6) +1 ;
    diceRoll3= (rand()%6) +1 ;
    a = diceRoll1 + diceRoll2 + diceRoll3;
    printf("The first value is %d\n", a);
    printf("You have to guess the next value will be higher, lower or same by inputing h/l/s\n");
    scanf("%s", &c);

    diceRoll1= (rand()%6) +1 ;
    diceRoll2= (rand()%6) +1 ;
    diceRoll3= (rand()%6) +1 ;
    b = diceRoll1 + diceRoll2 + diceRoll3;
    printf("The second value is %d\n", b);



    if((a = b && c=='s') || (a > b && c=='l') || (a < b && c=='h')) {
        printf("Good Job");
    }else{
        printf("Better next time");
    }

   return 0;
}


This is my code for challenge 2. However, I dont know why it keep saying "Better next time" even my guess is right. Can someone help me please?

Post a Reply

Replies

Oldest  Newest  Rating
0 Franz Schmidt · November 30, 2014
You made a little mistake in your if statement:
Thats what you wrote:
if((a = b && c=='s') || (a > b && c=='l') || (a < b && c=='h')) {

and this is the corrected version:
if ((a == b && c == 's') || (a > b && c == 'l') || (a < b && c == 'h')) {

=       assign a value to a variable

==     compares the variables
0 Minh Quan Vu · November 30, 2014
I edited like you said but I still say "Better next time" even my guess is right :(
0 Franz Schmidt · November 30, 2014
ok then try just to take a char as input not a string:
char c;
scanf("%c", &c);
0 Minh Quan Vu · December 1, 2014
Now, it works. Thank you very much :D
  • 1

C

106,983 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator