Updated program-having a pointer error
-
Can someone please look at this and tell me what I am doing wrong with my pointer.
#include #include #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 inclusiveDisplayMenu(); 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 Replyswitch (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"); }
-
Can someone please look at this and tell me what I am doing wrong with my pointer.
#include #include #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 inclusiveDisplayMenu(); 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 Replyswitch (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"); }
You've not stated what problems if any that you're having? PS: In your function prototypes, you don't need to specify any variable names for the parameters. Just the data types are OK. This is Ok:
void DrillOneProb (int, int, int);
-
You've not stated what problems if any that you're having? PS: In your function prototypes, you don't need to specify any variable names for the parameters. Just the data types are OK. This is Ok:
void DrillOneProb (int, int, int);
void Gen2Rand (int*r1p, int*r2p)
1. Error is undeclared identifier r2p 2. type error in argument 2 to 'Gen2Rand', found 'int' expected 'pointer to int' 3. possible usage of r1p and r2p before definition*r1p = r1; *r2p = r2;
1. Error is r1p and r2p is not a pointer. I met my instructor yesterday and he said that the prototypes were correct. So should I change the void Gen2Rand (int*r1, int*r2) -
Can someone please look at this and tell me what I am doing wrong with my pointer.
#include #include #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 inclusiveDisplayMenu(); 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 Replyswitch (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"); }
You are missing: 1) a closing brace in
GetMenuChoice()
, and 2) an opening brace inGen2Rand()
. There are other errors but addressing those two should get you further along."Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
-
You are missing: 1) a closing brace in
GetMenuChoice()
, and 2) an opening brace inGen2Rand()
. There are other errors but addressing those two should get you further along."Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
-
That's good. As for the prototype, this is OK:
void Gen2Rand (int*, int*);
. No need for specifying variable names in prototype. Just let the compiler know what data type to expect for that function. Cheers!
-
I figured it would. You might consider formatting your code (e.g., using spaces instead of tabs) so that errors such as mismatched braces would be more obvious. For example:
#define SENT 4 //"Quit" menu choice
/* 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 numberr1 = 2 + rand() % 11; r2 = 2 + rand() % 11; \*r1p = r1; \*r2p = r2;
}
/* 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 Replyswitch (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"); }
}
int main (void)
{
int c; //Menu Choice (1-4)
int r1, //First Random Integer: 2-12 inclusive
r2; //Second Randon Integer: 2-12 inclusiveDisplayMenu(); c = GetMenuChoice(); while (c >= 1 && c < SENT) { Gen2Rand (&r1, &r2); DrillOneProb (c, r1,r2); DisplayMenu(); c = GetMenuChoice(); printf("Program Complete\\n"); } return (0);
}
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you
-
I figured it would. You might consider formatting your code (e.g., using spaces instead of tabs) so that errors such as mismatched braces would be more obvious. For example:
#define SENT 4 //"Quit" menu choice
/* 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 numberr1 = 2 + rand() % 11; r2 = 2 + rand() % 11; \*r1p = r1; \*r2p = r2;
}
/* 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 Replyswitch (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"); }
}
int main (void)
{
int c; //Menu Choice (1-4)
int r1, //First Random Integer: 2-12 inclusive
r2; //Second Randon Integer: 2-12 inclusiveDisplayMenu(); c = GetMenuChoice(); while (c >= 1 && c < SENT) { Gen2Rand (&r1, &r2); DrillOneProb (c, r1,r2); DisplayMenu(); c = GetMenuChoice(); printf("Program Complete\\n"); } return (0);
}
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you
-
That's good. As for the prototype, this is OK:
void Gen2Rand (int*, int*);
. No need for specifying variable names in prototype. Just let the compiler know what data type to expect for that function. Cheers!
IMHO, I would say that it is good practice to specify the parameter names in the function prototype, especially when the parameters are the same type. This is not for the compiler's sake but for your own sanity when you come to call the function. It will help you to get your parameters in the right order without having to analyze your code. Tony ;)
-
IMHO, I would say that it is good practice to specify the parameter names in the function prototype, especially when the parameters are the same type. This is not for the compiler's sake but for your own sanity when you come to call the function. It will help you to get your parameters in the right order without having to analyze your code. Tony ;)
Oh yes, that's right, Guess I'm just used to leaving my parameter names out :)
-
You've not stated what problems if any that you're having? PS: In your function prototypes, you don't need to specify any variable names for the parameters. Just the data types are OK. This is Ok:
void DrillOneProb (int, int, int);
Ibrahim Bello wrote:
In your function prototypes, you don't need to specify any variable names for the parameters. Just the data types are OK. This is Ok: void DrillOneProb (int, int, int);
But not very helpful to someone looking at a function prototype in a header file. :doh:
You measure democracy by the freedom it gives its dissidents, not the freedom it gives its assimilated conformists.