Thread: Dice Roll

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    24

    Dice Roll

    Hi,

    I'm trying to write a simple program to roll a die. So far, here is what I have:

    Code:
    #include <stdio.h>
    #include <math.h>
    #include <time.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    #include <limits.h>
    
    int main(void)
    {
    	char *roll = "roll";
    	char *exit = "exit";
    	char input[10];
    
    	printf("Command: ");
    	gets(input);
    
    	while((strcmp(input,roll) == 0) && (strcmp(input,exit) != 0))
    	{
    		printf("%d", 1 + (srand(time(NULL)) % 4));
    	}
    
    	return 0;
    }
    However, when I try to compile, I get an error in line 20 (the print statement) saying "void value not ignored as it ought to be".

    Does anyone have any ideas?

    Thanks.
    Last edited by seanksg; 05-12-2011 at 11:57 PM. Reason: I solved one of the errors

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > Last edited by seanksg; Today at 06:57 AM. Reason: I solved one of the errors
    Which one?

    Or were there three to begin with?

    Anyway
    1. use ==, not =
    2. use rand(), not srand() - RTFM for the difference between them.

    Oh, and while you're at it, read the FAQ on why gets() is bad.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    24
    Quote Originally Posted by Salem View Post
    > Last edited by seanksg; Today at 06:57 AM. Reason: I solved one of the errors
    Which one?

    Or were there three to begin with?

    Anyway
    1. use ==, not =
    2. use rand(), not srand() - RTFM for the difference between them.

    Oh, and while you're at it, read the FAQ on why gets() is bad.
    Nope, just two. I guess you and I both saw the "==" error at the same time. Andwhy rand()? Won't that produce the same value every time?
    Also, thanks for the FAQ suggestion... I was following an example from a textbook, and this, along with a problem I had a while back with the fflush(stdin) function have made me lose a lot of faith in this book, and my professor's ability to choose an appropriate text.
    Last edited by seanksg; 05-13-2011 at 12:08 AM. Reason: addendum

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    You call srand() exactly ONCE at the start of the program.

    Then you call rand() whenever you want.

    Did you read the manual?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    24
    Quote Originally Posted by Salem View Post
    You call srand() exactly ONCE at the start of the program.

    Then you call rand() whenever you want.

    Did you read the manual?
    Ah I see. I just did upon your suggestion, but had only read what my textbook said before. I think I'm going to stray from this text from now on... at least when it is instructing me in how to use a function. Anyway, it's all working now.

    Thanks for your help Salem!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dice Roll Program
    By HeidiPagel in forum C Programming
    Replies: 7
    Last Post: 12-13-2010, 10:39 AM
  2. string roll
    By kryptkat in forum C++ Programming
    Replies: 2
    Last Post: 12-26-2009, 09:51 PM
  3. Roll on 18 wheels roll on
    By abachler in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 05-13-2009, 09:12 PM
  4. Roll Up the Rim (Canadians)
    By @nthony in forum A Brief History of Cprogramming.com
    Replies: 26
    Last Post: 03-17-2009, 04:58 PM
  5. C++ dice roll need help
    By catdieselpow in forum C++ Programming
    Replies: 1
    Last Post: 10-07-2007, 01:32 PM