Random Numbers in calculator

This is a discussion on Random Numbers in calculator within the C Programming forums, part of the General Programming Boards category; I am having a problem with following a flowchart. Here is what I have so far and I keep coming ...

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    47

    Random Numbers in calculator

    I am having a problem with following a flowchart. Here is what I have so far and I keep coming up with pointer and int errors. Any help would be greatful.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    #define SENT 4 //"Quit" menu choice
    
    
    /*  Function Prototypes */
    
    
    	void DisplayMenu (void);
    	void GetMenuChoice (void);
    	void Gen2Rand (int*r1, int*r2);
    	void DrillOneProb (int*c, int*r1, int*r2);
    
    
    
    
    
    
    /*============Mainline Procedures===============*/
    
    int main (void)
    {
    	int	   c;   //Menu Choice (1-4)
    	int   r1,  //First Random Integer:  2-12 inclusive
    	      r2;  //Second Randon Integer:  2-12 inclusive
    
    
    }
    
    /*===========CHILD FUNCTIONS===============*/
    
    /*  Display Title and Menu */
    
    void DisplayMenu (void)
    
    	{
    		printf("MENU OF OPERATIONS\n\n");
    		printf("1. Addition.\n");
    		printf("2. Subtraction.\n");
    		printf("3. Multiplication.\n");
    		printf("4. Quit.\n\n");
    
        }
    
    
    /* Get Menu Choice */
    
    void GetMenuChoice (void)
    
    	{
    		int c;
    		printf ("Enter the number of the operation to try (1-4):\n");
    		scanf ("%d", &c);
    		while
    			 (c<1 || c>SENT)
    				printf("\aInput value is out of range.\n");
    
    
    	}
    
    /* Generate and return 2 integers between 2-12 inclusive */
    
    void Gen2Rand (int *r1p, int *r2p)
    
    	{
    		int c;
    		c=0;
    
    		if (c>=1 && c<SENT)
    
    		int r1; //First random number
    		int r2; //Second random number
    
    		r1 = 2 + rand() %11;
    		r2 = 2 + rand() %11;
    
    	   *r1p = r1;
    	   *r2p = r2;
    
    	   printf("Program complete\n");
    
    	}
    
    /* Display two random numbers and ask user what the answer would be after the chosen operation*/
    
    void DrillOneProb (int*c, int*r1, int*r2)
    
    {
    		int CorAns, //Correct Answer
    			Reply;  // Users Reply
    
    			{
    			printf("\nWhat is %d",r1);
    			scanf("%d", r1);
    
    			r1 = 2 + rand() % 11;
    			}
    
    
    	switch (c)
    	{
    		case '1'
    		  {	printf("+");
    		   	CorAns = r1 + r2;
    		  }
    		  break;
    
    
    		case '2':
    		   	printf("-");
    		  	CorAns = r1 - r2;
    		  break;
    
    
    		case '3':
    		  printf("x");
    		  CorAns = r1 x r2;
    		  break;
    
    	}
    
    	printf("%d ?  %d\n", r2, Reply);
    
       if
      		Reply = CorAns
    	   		{
    			   printf("Yes, that is correct. Good Job!");
    			}
    
    		else
    			printf("No, the correct answer is: %d", CorAns);
    			printf("\n\n");
    
    return c;
    
    
    
    
           }
    
    int rand()
    {
        return((rand())); //Use the function rand to generate a random number
                              //And then return-it (the random number)
    }
    
    
    
    void DisplayMenu(void)
    {
    
        printf("Menu of Operations\n\n");
        printf("1. Addition.\n");
    	printf("2. Subtraction.\n");
    	printf("3. Multiplication.\n");
    	printf("4. Quit.\n\n");
    
    	printf("Enter the number of the operation to try (1-4): ");
    	scanf ("%d", &c);
    		if (c<1 || c > SENT)
    			{
    				printf("\aInput Value is out of range.\n");
    				printf("Enter the number of the operation to try (1-4): ");
    		    }
    
    
    
    
    }

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Quote Originally Posted by kburyanek View Post
    I keep coming up with pointer and int errors.
    What are "pointer and int errors"? Give us the compiler output (i.e. these error messages), so we have a place to start, rather than reading your code line by line and "compiling" it ourselves.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    For starters, here are a just a few of the issues, after a very quick look:
    Code:
    ...
    void Gen2Rand (int *r1p, int *r2p)
    
        {
            int c;
            c=0;
    
            if (c>=1 && c<SENT) // you probably want to use a pair of {}'s around some of the code here,
    //otherwise only the next statement is exectuted if this condition is true ("int r1;")
    
            int r1; //First random number
            int r2; //Second random number
    
            r1 = 2 + rand() %11;
            r2 = 2 + rand() %11;
    
           *r1p = r1;
           *r2p = r2;
    
           printf("Program complete\n");
    
        }
    ...
    
    void DrillOneProb (int*c, int*r1, int*r2)
    
    {
    ...
            case '3':
              printf("x");
              CorAns = r1 x r2; // multiplication is "*" not "x" which is a character.  also, you probably dont mean to multiply 2 pointers.
              break;
    
        }
    
        printf("%d ?  %d\n", r2, Reply);
    
       if // syntax error
              Reply = CorAns // this assigns CorAns to Reply, maybe you want "==" instead of "="
                   {
                   printf("Yes, that is correct. Good Job!");
                }
    
            else // you probably want pair of {}s here, otherwise only next statement is executed as the else
                printf("No, the correct answer is: %d", CorAns);
                printf("\n\n");
    
    return c;// cannot return a value because youve declared it void
    
    
    
    
           }
    
    int rand()
    {infinite recursion here.  "rand()" already exists, so get rid of "your version" of it, i.e. delete this function
        return((rand())); //Use the function rand to generate a random number
                              //And then return-it (the random number)
    }
    Last edited by nadroj; 10-30-2009 at 02:37 PM.

  4. #4
    Registered User
    Join Date
    Sep 2009
    Posts
    47
    For some reason my compiler did not find those error and when I tried to add { } around the below, I got an error.
    Code:
    if (c>=1 && c<SENT) // you probably want to use a pair of {}'s around some of the code here,
    //otherwise only the next statement is exectuted if this condition is true ("int r1;")
    
    
    CorAns = r1 x r2; // multiplication is "*" not "x" which is a character
              break;
    
    This changed properly.
    
    
    It give me an error on this:
    
    r1 = 2 + rand() % 11;//Illegal expression, operands = have illegal types of 'pointer to int' and 'int' and missing semicolon before 11//
    Can you tell me why?

  5. #5
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    I dont know where you added the braces, so show your edited code.

    Its very important to realize that just because the compiler doesnt complain, i.e. give an error or warning, does not mean the code is correct.

    See my edit in my previous post. I.e, for the multiplication, your multiplying 2 pointers, but you probably dont mean to do that. You probably want to multiply the values at those two pointers. Similarly, "r1" is a pointer, so you probably dont want to modify the address it points to, rather you want to modify the value at the address it points to (i.e. "*r1 =")

  6. #6
    Registered User
    Join Date
    Sep 2009
    Posts
    47
    Thanks but I did go into my book and I had changed that.

  7. #7
    Registered User
    Join Date
    Sep 2009
    Posts
    47
    It is telling me illegal type pointer to int in switch expression on this (red) and operands of + have illegal type 'pointer to int' and 'pointer to int'. It does not do this on all of the other cases. (blue)

    Does anyone know why.

    switch (c)
    { case '1'
    { printf("+");
    CorAns = r1 + r2;

  8. #8
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    The compiler messages are usually pretty informative. Its saying the the type you gave in the switch expression is not valid (illegal), it only works on ints and chars. However, as the compiler is telling you, your passing it a "pointer to int". Use either "switch(*c)" or change the type of c to "int" instead of "int*"--do you really need all of these to be pointers?

    Edit: Similarly, again, "CorAns" is of type "int" but your trying to assign it an "int*". This message shouldnt happen, if you followed what I said above and add the "int*"s correctly, i.e. dereference them, then add. What your doing right now is adding two pointers ("r1 + r2") and assigning it to an int ("CorAns").

    You cant just throw in pointers and do operations (or in general, write code) without thinking what your telling the computer to do. Take time to understand what your doing, and more importantly, why your doing it.
    Last edited by nadroj; 10-30-2009 at 03:28 PM.

  9. #9
    Registered User
    Join Date
    Aug 2006
    Posts
    99
    Quote Originally Posted by kburyanek View Post
    I am having a problem with following a flowchart. Here is what I have so far and I keep coming up with pointer and int errors. Any help would be greatful.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    #define SENT 4 //"Quit" menu choice
    
    
    ...snip...
    
    		while
    			 (c<1 || c>SENT)
    				printf("\aInput value is out of range.\n");

    And, exactly what do you thing SENT is expanding to?
    Remember, a #define just does a pure substitution.

  10. #10
    Registered User
    Join Date
    Sep 2009
    Posts
    47
    I updated my program and have fixed most problems. I have a problem with the pointer, which I cannot get fixed.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    #define SENT 4 //"Quit" menu choice
    
    
    /*  Function Prototypes */
    
    
    	void DisplayMenu (void);
    	int GetMenuChoice (void);
    	void Gen2Rand (int*r1, int*r2);
    	void DrillOneProb (int c, int r1, int r2);
    
    
    
    
    
    /*============Mainline Procedures===============*/
    
    int main (void)
    {
    	int	   c;   //Menu Choice (1-4)
    	int   r1,  //First Random Integer:  2-12 inclusive
    	      r2;  //Second Randon Integer:  2-12 inclusive
    
    		  DisplayMenu();
    		  c = GetMenuChoice();
    	while (c >= 1 && c < SENT)
    		 { Gen2Rand (&r1, &r2);
    	  	  DrillOneProb (c, r1,r2);
    		  DisplayMenu();
    		  c = GetMenuChoice();
    		  printf("Program Complete\n");}
    		  return (0);
    }
    
    
    
    /*===========CHILD FUNCTIONS===============*/
    
    /*  Display Title and Menu */
    
    void DisplayMenu (void)
    
    {
    		printf("MENU OF OPERATIONS\n");
    		printf("1. Addition.\n");
    		printf("2. Subtraction.\n");
    		printf("3. Multiplication.\n");
    		printf("4. Quit.\n\n");
    
    
    }
    
    /* Get Menu Choice */
    
    int GetMenuChoice (void)
    
    	{
    		int c;
    		do{
    			printf ("Enter the number of the operation to try (1-4):\n");
    			scanf ("%d", &c);
    		if  (c<1 || c>SENT)
    				printf("\aInput value is out of range.\n");
    		while (c < 1 || c > SENT);
    		return (c);
    
    	}
    
    /* Generate and return 2 integers between 2-12 inclusive */
    
    
    void Gen2Rand (int*r1p, int*r2p)
    
    		int r1; //First random number
    		int r2; //Second random number
    
    		r1 = 2 + rand() % 11;
    		r2 = 2 + rand() % 11;
    
    	    *r1p = r1;
    	    *r2p = r2;
    
    return (0);
    
    
    	}
    
    /* Display two random numbers and ask user what the answer would be after the chosen operation*/
    
    void DrillOneProb (int c, int r1, int r2)
    
    {
    		int CorAns, //Correct Answer
    			Reply;  // Users Reply
    
    
    
    	switch (c)
    	{
    
    			case 1:
    		  	printf("+");
    		   	CorAns = r1 + r2;
    	        break;
    
    
    			case 2:
    		   	printf("-");
    		  	CorAns = r1 - r2;
    		  	break;
    
    
    			default:
    		    printf("x");
    		    CorAns = r1 * r2;
    		    break;
    
    	}
    
    	printf(" %d, ?", Reply);
    	scanf ("%d", &Reply);
    
       if
      		(Reply == CorAns)
    
    			   printf("Yes, that is correct. Good Job!");
    
    
    		else
    			{	printf("No, the correct answer is: %d", CorAns);
    				printf("\n\n");
    		    }
    
    
    }

  11. #11
    Registered User
    Join Date
    Sep 2009
    Posts
    47
    Problem completed.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with displaying random numbers
    By Bumps in forum C++ Programming
    Replies: 12
    Last Post: 10-03-2009, 12:29 PM
  2. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 08:47 AM
  3. Doubts regarding random numbers generation
    By girish1026 in forum C Programming
    Replies: 9
    Last Post: 12-31-2008, 09:47 PM
  4. random numbers limit
    By HAssan in forum C Programming
    Replies: 9
    Last Post: 12-06-2005, 06:51 PM
  5. Generate random numbers in Lucky7 project using C#
    By Grayson_Peddie in forum C# Programming
    Replies: 1
    Last Post: 04-11-2003, 11:03 PM

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