Thread: What am I missing?

  1. #1
    Registered User
    Join Date
    Sep 2014
    Posts
    6

    What am I missing?

    So I'm trying to run a dice program where I'm playing with the computer.
    So here it is .
    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<math.h>
    #include<stdlib.h>
    
    
    void main()
    {
    	
    	int dice,user,comp;
    	long *a;
    	srand(*a);
    	dice=abs(rand()%7);
    	printf("Enter User Guess:\n");
    	scanf("%d",&user);
    	user=abs(user%7);
    	comp=abs(rand()%6);
    	printf("Dice val:%d\nUser Guess:%d\nComputer Guess:%d\n", dic ,user,comp);
    	
            if(abs(dice-user)<abs(dice-comp))
    	printf("Winner: user");
    	else if(abs(dice-user)==abs(dice-comp))
    	printf("Winner: Computer");
    	else
    	printf("Its a draw");
    	getch();
    	
    }
    So when I run I get an error then the program exits. What am I missing? I can't seem to see it. Thanks in Advance

  2. #2
    Registered User
    Join Date
    Sep 2014
    Posts
    6
    Never mind I found it. lol

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    It might help us if you told us what error you are getting -- what is the message or what is the incorrect behavior/output. But, I don't know how you're even getting that program to run, when the code you posted doesn't even compile. Do you mean you can't compile the code? If that's the case, posting the exact error message(s) -- copied and pasted, not re-typed -- including line numbers, is a good idea.
    Code:
    $ make foo
    gcc -Wall -ggdb3 -pedantic -std=gnu99 -O0 -o foo foo.c -lm -lpthread -lrt
    foo.c: In function ‘main’:
    foo.c:18:63: error: ‘dic’ undeclared (first use in this function)
    foo.c:18:63: note: each undeclared identifier is reported only once for each function it appears in
    foo.c:26:5: warning: implicit declaration of function ‘getch’ [-Wimplicit-function-declaration]
    make: *** [foo] Error 1
    Some other things I notice:

    • You should fix up your indentation and formatting. If your code is easy to read, it's harder to make mistakes, and easier to find/fix them when you do.
    • conio.h and getch() are non-standard. You can use the standard getchar() function and get rid of conio.h
    • Regarding the above, it seems you are probably using Turbo C, which is old and no longer maintained. Try updating to a modern compiler/IDE such as Code::Blocks with MinGW or Pelles C.
    • void main() is wrong. It's int main(void), and you should return an int at the end. Read this: FAQ > main() / void main() / int main() / int main(void) / int main(int argc, char *argv[]) - Cprogramming.com.
    • Your variable 'a' is an uninitialized pointer, so it points to some arbitrary place in memory. You then try to read the contents of that that memory when you pass *a to srand(). This results in undefined behavior meaning anything or nothing at all can happen, but that will likely it will be a seg fault on any modern OS.
    • A better srand idiom for getting a different seed each time, is to use the current time -- look up the time() function in time.h.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what am i missing here??
    By xabhi in forum C Programming
    Replies: 6
    Last Post: 09-03-2010, 10:03 AM
  2. Am I missing something?
    By ke121885 in forum C Programming
    Replies: 8
    Last Post: 10-11-2009, 09:45 PM
  3. pdh.a Missing?
    By P4R4N01D in forum Windows Programming
    Replies: 5
    Last Post: 06-04-2008, 12:29 AM
  4. Someone tell me what I'm missing here?
    By Furious_George in forum C++ Programming
    Replies: 7
    Last Post: 10-06-2003, 12:27 AM
  5. Missing <mem.h>?
    By thePope in forum C++ Programming
    Replies: 12
    Last Post: 05-07-2003, 11:44 AM