challenge # 2 by xain

0 zain ahmed siddiqui · August 25, 2014
hey  plz run this code do me a favor when i input any thing alse instead of h, l, s why this program give me same two line of while loop i am new in programing plz help me out 

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

int main()
{
    int i;
    int dice1;
    int dice2;
    int dice3;
    int sum1;
    int sum2;
    char input;
    char input1;

    srand(time(NULL));
    printf("****Dice roll game with 3 dices****\n\n");

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

    dice2 = (rand()%6) +1;
    printf("dice2 = %d\n", dice2);

    dice3 = (rand()%6) +1;
    printf("dice3 = %d\n", dice3);

    sum1 = dice1+dice2+dice3;
}
printf("The total from 3 dices is %d \n\n",sum1);
printf("What do you think your next roll will be higher(h),lower(l) or same(s) as the current total? \n\n");

do{
printf("Enter (h),(l) or (s) to continue \n\n");
scanf("%c", &input);

if((input == 'l') || (input == 'h')|| (input == 's') ){
input1 = 1;
}else{
input1 = 0;
printf("please read the text and try again\n\n");

}
}while(input1!=1);

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

    dice2 = (rand()%6) +1;
    printf("dice2 = %d\n", dice2);

    dice3 = (rand()%6) +1;
    printf("dice3 = %d\n", dice3);
}

    sum2 = dice1+dice2+dice3;
    if((sum1<sum2) && (input == 'h')){
        printf("Congratz you guessed right total %d\n", sum2);
    }else if ((sum1>sum2) && (input == 'l')){
        printf("Congratz you guessed right total %d\n", sum2);
    }else if ((sum1==sum2) && (input == 's')){
        printf("Congratz you guessed right total %d\n", sum2);
    }
    else{
        printf("you loose your total is %d\n", sum2);
    }

return 0;
}

Post a Reply

Replies

Oldest  Newest  Rating
+2 Bucky Roberts · August 25, 2014
I fixed it for you, here is the new code:

http://pastebin.com/bgFbNZme

There were two issues. First, on line 16 input1 should be an int not a char.

char input1;
int input1;


Second, on line 39. Whenever you use scanf() you should always add a space after the first double quote like " %c" and not "%c"

scanf("%c",&input);
scanf(" %c",&input);

C compilers are weird and sometimes they get confused when you don't have that extra space
+1 Bucky Roberts · August 25, 2014
Just put all of your code inside one big loop.
+1 Bucky Roberts · August 25, 2014
If you want to loop it a certain number of times (such as 10 times) I would use a for loop. 

If you want to loop it until the user presses a certain character (such as 'q' for quit) I would use a do while loop.
0 zain ahmed siddiqui · August 31, 2014
hi bucky, 
i want to ask some thing about upper case character 
if i want to make a program that 
"enter your name" 
then user enter his/her name in lower case character or in upper case character 
then in next line the program shows his/her name but the first character is upper and now after all the character in lower case and if name contains first and last field then the first character is upper and now after all the character in lower case for first name and first character is upper and now after all the character in lower case 
for last name how i do this i cant under stannd and trying every thing of your tutorial but stuck 
0 zain ahmed siddiqui · August 25, 2014
thank you so much Bucky em very glad to you for your help and your stuff:)


and tell me one more thing if i want to loop full program again and again what can i do means
first computer roll the dices and then ask to user that what do u think for next result h, l, s
user select any one of these and computer shows the result and exit but i want to loop this again and again what can i do.....
0 zain ahmed siddiqui · August 25, 2014
which on  while loop, do while, for while loop ?
0 zain ahmed siddiqui · August 25, 2014
ohhhh yeah!!!! thank you so much i'll try... 
  • 1

C

107,120 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator