Chalange #2

+4 Thalinda Bandara · August 11, 2015
#include
#include
#include
#include

main(){

int diceroll1,diceroll2,diceroll3,sum=0,sum1;
int rolls;
char high;


    diceroll1=(rand()%6)+1;
    diceroll2=(rand()%6)+1;
    diceroll3=(rand()%6)+1;
printf("%d\n",diceroll1);
printf("%d\n",diceroll2);
printf("%d\n",diceroll3);

sum=diceroll1+diceroll2+diceroll3;
printf("%d\n\n\n",sum);

printf("Pleace Enter you input with simpl letters/n");
scanf("%c",&high);


    diceroll1=(rand()%6)+1;
    diceroll2=(rand()%6)+1;
    diceroll3=(rand()%6)+1;
printf("%d\n",diceroll1);
printf("%d\n",diceroll2);
printf("%d\n",diceroll3);
sum1=diceroll1+diceroll2+diceroll3;
printf("%d\n",sum1);




if(high=='h'&&sum>sum1){
        printf("Good Gues it's high value");
}
else if(high=='l'&&sum

Post a Reply

Replies

- page 1
Oldest  Newest  Rating
0 Toyen . · August 26, 2015
I see, thanks for the link, but what can I do with it and how can I use the srand function in my program?

And why did Bucky and Thalinda did not need to use it?
0 c student · August 24, 2015
there's a detailed description of time_t here: http://stackoverflow.com/questions/471248/what-is-ultimately-a-time-t-typedef-to

ultimately, time_t is a data type like a char or int.  in unix or posix type systems, it is typedef as a signed int.
0 Toyen . · August 24, 2015
Oh hey there again!

Thanks for clearing that up for me about the strings : )

I have no idea what seeding is and I dont know that function since Bucky did not use it but I looked it up here - http://www.tutorialspoint.com/c_standard_library/c_function_srand.htm

and I don´t quite get the part with the time_t t; thing.

What is that supposed to do?

Could you please tell me how would I incorporate it into my dice program? And why did Bucky not use it?

Thanks a lot! : ))
0 c student · August 19, 2015
in c, any sequence of characters which ends with a null terminator can be considered as a string be it one or thirty characters long.  double quotes represents a string with an implied null terminator at the end like so:
"this is a string"    // == this is a string\0


here is the man page for scanf: http://pubs.opengroup.org/onlinepubs/009695399/functions/scanf.html

if you called rand, you will need to seed it with srand to alter the calculated value.  if you use a static seed, your rand will output a static value, if your seed is dynamic, your rand will output dynamic values.  the most common seed is the following:

#include 

srand (time (NULL));

you will only be required to call it once
0 Toyen . · August 19, 2015
But isnt r a string as well? If not is it because its just one character?
If I asked the user to enter their name that would be stored in that char variable then I would use double quotations because that would be a string?

Thats really strange that it picks up the line feed like that. I remember at the beginning it didn´t do this.

And thanks for the tip" suppressing the new line this way worked so every time I need to suppress something, I put the asterisk in front of it?

Also what about that roll? Any idea why am I getting the same number all the time?

Thanks very much for all your feedback : )
0 c student · August 17, 2015
double quotes means a string.  "r" would be
r\0    // 'r' and null terminator

you're getting two prints because your scanf only reads on one character.  when you type a character and press space, your stdin is:
r\n    // 'r' and a line feed

so on the second run, your scanf would pick up the line feed.  to fix this, you can suppress the line feed using this:
scanf ("%c%*c", roll);    // scan in first character and suppress the next
0 Toyen . · August 16, 2015
Thank you very much!

Why are the single quotations used here though? Is it because I am trying to access a certain element of an array?

Also the program still does not work properly, it asks me twice to press r if I do not press r the first time not just once as intended and the roll it prints is always equal to 144, is there anything wrong with my dice roll ints?
0 c student · August 16, 2015
you're comparing a character with a string.  it should be
while (roll[0] != 'r')    // single quotes
0 Toyen . · August 15, 2015
Hey there and thanks for your response!

I changed the roll to %c  in the scan function and compared the first element of the array to r but it still doesnt work.

It keeps looping and never prints the dice roll even when I enter r.

Instead it asks me twice to roll the dice again.

I also do not know the strncmp function yet so I looked it up but I dont really get what your intention was, why to use it here to compare only 1 character?

Anyways, here is what my program looks like now:

/images/forum/upload/2015-08-15/fc63f5029cbf5edde4e2f46d8715ac3a.png
0 c student · August 15, 2015
you cannot compare an array with a string like that.  you have three choices in this situation

1. make roll of type char:
char roll;

scanf ("%c", &roll);

which will now allow you to compare roll and a character with the comparison operator

while (roll != 'r');


2. compare only the first element of the array with the character.  this also allows the use of the comparison operator

while (roll[0] != 'r');


3. use the strcmp function to compare strings

while (strncmp (roll, "r", 1) != 0);
  • 1
  • 2

C

126,626 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator