Challenge 2 problem!

+1 Tomislav Keškić · January 27, 2015
http://pastebin.com/QM58zJAG <---- this is the code can someone help me please?

Post a Reply

Replies

Oldest  Newest  Rating
+2 Lazar Bulic · January 28, 2015
The problem was in the If in the switch. If user's input is H they think next sum of dices will bi bigger than the first one. So the test should be 

case 'H':
if(sum1<sum2){
//correct
else
//wrong
}
break;



and if they input L they think that the next sum of dices will be lower than the first one. 


case 'L':
if(sum1>sum1){
//correct
else
//wrong
}
break;



Also their are few others problems in your code, compiler is able to work over them but that isn't good for your code.

1)You are declaring a string and then filling it with 1 character on unsigned place.
2) You are scanning string type in character variable.

There are also some thing to make your code more smaller and more efficient, and be more functional.
Try to reduce the number of variables. 
Implement srand so the program doesn't give same results.
Give user option to make mistake. If your input doesn't fulfills the requirements (L or S or H) give him another chance don't end the program.
Remove the libraries that you aren't using.

If you have hard time with anything let me know and I will try to help.
0 Tomislav Keškić · January 29, 2015
I kinda knew the problems with switch statmens I just had to change the <,>....and that 1) if you mean about scanf("%c", ready) its just for checking if the user is ready and if he is to press any key to continue...2) isn't string an array of characters?.........
and about the variables could you please tell me which ones are unnecessary?
and I implemented srand, didn't know about this function that was the main problem I was always getting same numbers and didn't know what  was causing that problem...
and I'll do the thing with giving the user few chances... thanks for advices!
+2 Lazar Bulic · January 29, 2015
1) Well the correct format of your scanf("%c", ready) would be


scanf("%c", &ready[0]);
fflush(stdin);// I always use it after scaning strings and characters with scanf,


Do you get the point. Why create a string and take computer memory just to check something and the input only 1 character . You can create a character type (ot string) and pass it on to it. Even better option is to use just getchar(); So just replace it with and remove that string ready as it isn't needed. Also if you are making app for windows just use system("pause"); but don't forget to include #include <stdlib.h> With this method you can even remove text Press any key to continue as this function will do it automatically.

getchar();


2) Yes that is tru. But in this case you are passing a string in to a character type and that is wrong.
char pogadjanje; // single character value
scanf("%s", &pogadjanje); // %s that is a string type
There are 2 solutions:
scanf("%c", &pogadjanje);
fflush(stdin);



pogadjanje = getchar();


Ok now about reducing the number of variables:

dice1 = (rand()%6)+1;
dice2 = (rand()%6)+1;
dice3 = (rand()%6)+1;
printf("The first dice is: %d \nsecond one: %d \nand the third one is: %d",dice1,dice2,dice3);
sum1 = dice1 + dice2 + dice3;
printf("\nThe first sum is %d", sum1);
dice4 = (rand()%6)+1;
dice5 = (rand()%6)+1;
dice6 = (rand()%6)+1;
sum2 = dice4 + dice6 + dice5;



you can write this:
dice1 = (rand()%6)+1;
dice2 = (rand()%6)+1;
dice3 = (rand()%6)+1;
printf("The first dice is: %d \nsecond one: %d \nand the third one is: %d",dice1,dice2,dice3);
sum1 = dice1 + dice2 + dice3;
printf("\nThe first sum is %d", sum1);
dice1 = (rand()%6)+1;
dice2 = (rand()%6)+1;
dice3 = (rand()%6)+1;
sum2 = dice1 + dice2 + dice3;


So you just need 3 dice variables, I leave you to figure out why. You can even remove all dice variables :) but try to do it by your self.

Uzivaj :)
0 Tomislav Keškić · January 31, 2015
You're right! Thank you so much!
  • 1

C

107,156 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator