# Thread: Random Numbers in calculator

1. ## 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 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 */

{
printf("2. Subtraction.\n");
printf("3. Multiplication.\n");
printf("4. Quit.\n\n");

}

/* Get Menu Choice */

{
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

{
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
{
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)
}

{

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. Originally Posted by kburyanek
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. 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)
}```

4. 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. 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. Thanks but I did go into my book and I had changed that.

7. 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. 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.

9. Originally Posted by kburyanek
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. 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 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

while (c >= 1 && c < SENT)
{ Gen2Rand (&r1, &r2);
DrillOneProb (c, r1,r2);
printf("Program Complete\n");}
return (0);
}

/*===========CHILD FUNCTIONS===============*/

/*  Display Title and Menu */

{
printf("2. Subtraction.\n");
printf("3. Multiplication.\n");
printf("4. Quit.\n\n");

}

/* Get Menu Choice */

{
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

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);

if

printf("Yes, that is correct. Good Job!");

else
{	printf("No, the correct answer is: %d", CorAns);
printf("\n\n");
}

}```

11. Problem completed.

Popular pages Recent additions