Random not working properly

This is a discussion on Random not working properly within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include <conio.h> #include <stdlib.h> int main() { int ch; clrscr(); while(ch!='0') { puts ("Press the number of ...

  1. #1
    Registered User
    Join Date
    Aug 2012
    Location
    Quezon City, Philippines, Philippines
    Posts
    8

    Random not working properly

    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <stdlib.h>
    
    int main()
    {
        int ch;
        clrscr();
        while(ch!='0')
        {
            puts ("Press the number of selection");
            puts ("[1] Perfect Numbers");
            puts ("[2] Fibonacci Numbers");
            puts ("[3] Jack-En-Poy");
            puts ("[0] EXIT\n\n");
        while((ch=getch())!='0')
        {
    
            if(ch=='1')
            {
                int p,N[100] = {0},d,c,E=0;
                printf("Input a number: ");
                scanf("%d",&p);
                for(d=1,c=0;d<p;d++)
                {
                    if(p%d==0)
                    {
                        N[c]=d;
                        c++;
                    }
                }
            c=0;
            while(N[c]!=0)
            {
                E=E+N[c];
                c++;
            }
            printf("\n%d = 1",E);
            c=1;
            while(N[c]!=0)
            {
                printf(" + %d",N[c]);
                c++;
            }
            if(p==E)
            {
                printf("\nPerfect!");
            }
            if(p!=E)
            {
            printf("\nDeficient!");
            }
            getch();
            clrscr();
            break;
    
        }
        if(ch=='2')
        {
            int c,N,N2,f,temp;
            printf("Enter a Number: ");
            scanf("%d",&f);
            printf("0 1 ");
            for(N=0,N2=1,c=1;c<=f;c=c+1)
            {
                if(c==(N+N2))
                {
                    printf("%d ",c);
    
                    N=N2;
                    N2=c;
                }
    
            }
            getch();
            clrscr();
            break;
        }
        if(ch=='3')
        {
            int AI;
            char you;
            printf("[S] scissors\n[P] Paper\n[R] Rock\nSelect your move:\n");
            you=getchar();
            AI=random(3);
            if(AI==0)
            {
                printf("Computer:Scissors\n");
                if(you=='S')
                    printf("DRAW!");
                if(you=='P')
                    printf("LOSE!");
                if(you=='R')
                    printf("WIN!");
            }
            if(AI==1)
            {
                printf("Computer:Paper\n");
                if(you=='S')
                    printf("WIN!");
                if(you=='P')
                    printf("DRAW!");
                if(you=='R')
                    printf("LOSE!");
            }
            if(AI==2)
            {
                printf("Computer:Rock\n");
                if(you=='S')
                    printf("LOSE!");
                if(you=='P')
                    printf("WIN!");
                if(you=='R')
                    printf("DRAW!");
            }
            AI=0;
            getch();
            clrscr();
            break;
        }
    
    
    
    }
    }
    
      return 0;
    }
    I have a problem in my Jack-En-Poy. At first it works but the second time around the program will pass around you=getchar() and will instantly prints the result without asking the user for the input.

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Can you complie this?I got the error
    Code:
    main.c:81: error: too many arguments to function `random'

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    2,570
    Code:
    you=getchar();
    Let's say the user enters an 'S' followed by 'enter' - the input buffer will contain:

    Code:
    S\n
    The "getchar()" will take the 'S' but the newline will remain in the input buffer and get picked up by the next "getchar()", effectively bypassing it.

    A quick solution would be to place a second "getchar()" after the first to eat up this newline:

    Code:
    you=getchar();
    getchar();

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    2,570
    Quote Originally Posted by std10093 View Post
    Can you complie this?I got the error
    I got other errors myself, I'm guessing due to conflicting implementations of the "conio" header. But since these had no bearing on the question, I just bypassed (deleted or dummied) the problem functions to run the problem and verify my solution.

  5. #5
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by Matticus View Post
    I got other errors myself, I'm guessing due to conflicting implementations of the "conio" header. But since these had no bearing on the question, I just bypassed (deleted or dummied) the problem functions to run the problem and verify my solution.
    What had to do with conio.h i removed it before compiling.

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    2,570
    Quote Originally Posted by std10093 View Post
    What had to do with conio.h i removed it before compiling.
    I see. I made the assumption that the "random()" function was maybe part of the "conio" header, but I was mistaken. It appears to be part of the unix "stdlib" library (link). I tried the function in a simple program on a virtual machine running CentOS and it compiled and ran just fine.

  7. #7
    Registered User
    Join Date
    Jul 2012
    Posts
    51
    random() is not working because it's being used incorrectly. random() takes no arguments.

    Code:
    long int random(void);
    Use the same as rand(), example here. There may be other problems?

  8. #8
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by fnprintf View Post
    random() is not working because it's being used incorrectly. random() takes no arguments.

    Code:
    long int random(void);
    Use the same as rand(), example here. There may be other problems?
    Yes that was the error.MAtticus a fprintf said,the error was "too many arguments in function random()"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. for loop not working properly
    By Dawud.Beale in forum C Programming
    Replies: 18
    Last Post: 05-10-2012, 02:59 AM
  2. Not Working properly
    By Tejas Sanap in forum C Programming
    Replies: 7
    Last Post: 05-10-2011, 06:52 AM
  3. Fscanf not working properly
    By thefinalhero in forum C Programming
    Replies: 9
    Last Post: 10-05-2009, 10:06 AM
  4. Calendar not working properly?
    By kawk in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 05-19-2007, 01:15 PM
  5. tic tac toe not working properly
    By h_howee in forum Game Programming
    Replies: 2
    Last Post: 01-01-2006, 12:59 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21