Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Updated program-having a pointer error

Updated program-having a pointer error

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestionloungecareer
11 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K kbury

    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 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");
    	    }
    
    I Offline
    I Offline
    Ibrahim Bello
    wrote on last edited by
    #2

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

    K T 2 Replies Last reply
    0
    • I Ibrahim Bello

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

      K Offline
      K Offline
      kbury
      wrote on last edited by
      #3

      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)

      1 Reply Last reply
      0
      • K kbury

        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 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");
        	    }
        
        D Offline
        D Offline
        David Crow
        wrote on last edited by
        #4

        You are missing: 1) a closing brace in GetMenuChoice(), and 2) an opening brace in Gen2Rand(). 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

        K 1 Reply Last reply
        0
        • D David Crow

          You are missing: 1) a closing brace in GetMenuChoice(), and 2) an opening brace in Gen2Rand(). 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

          K Offline
          K Offline
          kbury
          wrote on last edited by
          #5

          Thank you, That did fix the prob.

          I D 2 Replies Last reply
          0
          • K kbury

            Thank you, That did fix the prob.

            I Offline
            I Offline
            Ibrahim Bello
            wrote on last edited by
            #6

            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!

            S 1 Reply Last reply
            0
            • K kbury

              Thank you, That did fix the prob.

              D Offline
              D Offline
              David Crow
              wrote on last edited by
              #7

              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 number

              r1 = 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 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");
              }
              

              }

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

              }

              "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

              K 1 Reply Last reply
              0
              • D David Crow

                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 number

                r1 = 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 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");
                }
                

                }

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

                }

                "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

                K Offline
                K Offline
                kbury
                wrote on last edited by
                #8

                Thanks again. I will make a notation in regards to the braces. I did make some other changes because the execute did not look like the sample given. But now works perfectly.

                1 Reply Last reply
                0
                • I Ibrahim Bello

                  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!

                  S Offline
                  S Offline
                  softwaremonkey
                  wrote on last edited by
                  #9

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

                  I 1 Reply Last reply
                  0
                  • S softwaremonkey

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

                    I Offline
                    I Offline
                    Ibrahim Bello
                    wrote on last edited by
                    #10

                    Oh yes, that's right, Guess I'm just used to leaving my parameter names out :)

                    1 Reply Last reply
                    0
                    • I Ibrahim Bello

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

                      T Offline
                      T Offline
                      Tim Craig
                      wrote on last edited by
                      #11

                      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.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups