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. pointer and switch

pointer and switch

Scheduled Pinned Locked Moved C / C++ / MFC
game-devquestionloungecareer
13 Posts 3 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

    I am trying to write a program as a math tutor. Can someone look at this code. I keep getting errors on int and pointer as well as telling me the switch case has illegal pointer. Thanks, any advice would be greatful.

    #include #include #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);
    
    		r1 = 2 + rand() % 11;
    		}
    
    
    switch (c);
    {
    	case '1':  1 = +; break;
    	CorAns = r1 + r2;
    
    	case '2':  2 = -; break;
    	CorAns = r1 - r2;
    
    	case '3':  3 = x; break;
    	CorAns = r1 x r2;
    
    }
    
    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);

    print\_menu(); //Print the menu option, and configure some needed things.
    
    
    return (0); //The end of our simple game.
    
    CPalliniC Offline
    CPalliniC Offline
    CPallini
    wrote on last edited by
    #4

    kbury wrote:

    1 = +;

    kbury wrote:

    2 = -;

    kbury wrote:

    3 = x;

    kbury wrote:

    CorAns = r1 x r2;

    kbury wrote:

    if Reply = CorAns

    I think you should read very carefully a syntax reference of the C programming language. :)

    If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
    This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
    [My articles]

    In testa che avete, signor di Ceprano?

    K 1 Reply Last reply
    0
    • CPalliniC CPallini

      kbury wrote:

      1 = +;

      kbury wrote:

      2 = -;

      kbury wrote:

      3 = x;

      kbury wrote:

      CorAns = r1 x r2;

      kbury wrote:

      if Reply = CorAns

      I think you should read very carefully a syntax reference of the C programming language. :)

      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
      [My articles]

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

      I am only trying to follow a flow chart given and it is very confusing. Thank you

      CPalliniC 1 Reply Last reply
      0
      • K kbury

        I am only trying to follow a flow chart given and it is very confusing. Thank you

        CPalliniC Offline
        CPalliniC Offline
        CPallini
        wrote on last edited by
        #6

        While following the flow, you shouldn't forget there is also the syntax... :rolleyes:

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
        [My articles]

        In testa che avete, signor di Ceprano?

        K 1 Reply Last reply
        0
        • CPalliniC CPallini

          While following the flow, you shouldn't forget there is also the syntax... :rolleyes:

          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
          This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
          [My articles]

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

          I am stuck with this section of my code so far: error codes: expected int and got pointer to int and operands of = illegaltype pointer to int and int

          /* 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);	1st error		                scanf("%d", r1);
          
          		r1 = 2 + rand() % 11;           2nd error
          		}
          
          CPalliniC 1 Reply Last reply
          0
          • K kbury

            I am stuck with this section of my code so far: error codes: expected int and got pointer to int and operands of = illegaltype pointer to int and int

            /* 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);	1st error		                scanf("%d", r1);
            
            		r1 = 2 + rand() % 11;           2nd error
            		}
            
            CPalliniC Offline
            CPalliniC Offline
            CPallini
            wrote on last edited by
            #8

            kbury wrote:

            printf("\nWhat is %d",r1);

            change to

            printf("\nWhat is %d", *r1);

            kbury wrote:

            r1 = 2 + rand() % 11;

            change to

            *r1 = 2 + rand() % 11;

            The above steps, however just fix compiler errors...I don't know if the semantic will be correct. BTW what about a good C language programming book? :)

            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
            This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
            [My articles]

            In testa che avete, signor di Ceprano?

            K 1 Reply Last reply
            0
            • CPalliniC CPallini

              kbury wrote:

              printf("\nWhat is %d",r1);

              change to

              printf("\nWhat is %d", *r1);

              kbury wrote:

              r1 = 2 + rand() % 11;

              change to

              *r1 = 2 + rand() % 11;

              The above steps, however just fix compiler errors...I don't know if the semantic will be correct. BTW what about a good C language programming book? :)

              If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
              This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
              [My articles]

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

              change to printf("\nWhat is %d", *r1); (I already changed this)I figured that out. kbury wrote: r1 = 2 + rand() % 11; change to *r1 = 2 + rand() % 11; (This defineately fixed this. The above steps, however just fix compiler errors...I don't know if the semantic will be correct.

              switch (c)

              {     (states that this is illegal type pointer to int in       switch expression.)
              
              	case '1':
              	  	printf("+");
              	   	CorAns = r1 + r2;  (operands of + have illegal of 'pointer to int' and 'pointer to int')
              
              	  break;
              
              CPalliniC 1 Reply Last reply
              0
              • K kbury

                change to printf("\nWhat is %d", *r1); (I already changed this)I figured that out. kbury wrote: r1 = 2 + rand() % 11; change to *r1 = 2 + rand() % 11; (This defineately fixed this. The above steps, however just fix compiler errors...I don't know if the semantic will be correct.

                switch (c)

                {     (states that this is illegal type pointer to int in       switch expression.)
                
                	case '1':
                	  	printf("+");
                	   	CorAns = r1 + r2;  (operands of + have illegal of 'pointer to int' and 'pointer to int')
                
                	  break;
                
                CPalliniC Offline
                CPalliniC Offline
                CPallini
                wrote on last edited by
                #10

                kbury wrote:

                CorAns = r1 + r2;

                change to

                CorAns = *r1 + *r2;

                The real solution, however, would be reading the f*#?n' book. :-D

                If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                [My articles]

                In testa che avete, signor di Ceprano?

                K 2 Replies Last reply
                0
                • CPalliniC CPallini

                  kbury wrote:

                  CorAns = r1 + r2;

                  change to

                  CorAns = *r1 + *r2;

                  The real solution, however, would be reading the f*#?n' book. :-D

                  If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                  This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                  [My articles]

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

                  I do not believe that has been in any of the chapters that we have read so far, but I will look again at the latest chapter because each assignment is associated with that chapter.

                  1 Reply Last reply
                  0
                  • CPalliniC CPallini

                    kbury wrote:

                    CorAns = r1 + r2;

                    change to

                    CorAns = *r1 + *r2;

                    The real solution, however, would be reading the f*#?n' book. :-D

                    If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                    This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                    [My articles]

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

                    and what about the brace

                    CPalliniC 1 Reply Last reply
                    0
                    • K kbury

                      and what about the brace

                      CPalliniC Offline
                      CPalliniC Offline
                      CPallini
                      wrote on last edited by
                      #13

                      I guess you're able to guess now. :)

                      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                      [My articles]

                      In testa che avete, signor di Ceprano?

                      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