Thread: Repetition structure not working?!?

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    27

    Repetition structure not working?!?

    For some reason my repetition structure is not working correctly. I do not get any compiler errors, but the calculation only happens the first time I choose one of my options. After the result is displayed the first time, the options repeat, I select an option and enter the data, then get an error instead of calculation.

    I have used repetition before, and have not had this problem until now.
    Can anyone help me resolve this issue?


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define maxR 2
    #define maxC 2
    
    void doEval();
    int doAdd();
    int doSub();
    char doQuit(char);
    
    main()
    {
          doEval();
          return 0;
    }
    
    void doEval()
    {
          char userInput[2048];
          int row, col;
          int arrayA[maxR][maxC];
          int arrayB[maxR][maxC];
          int arrayZ[maxR][maxC];
          char optionA;
    
          printf("Enter one of the following options:\n\
                    Q - Quit the program\n\
                    A - Add 2 matrices of size   5 x 5\n\
                    S - Subtract 2 matrices of size   5 x 5\n\
                         Enter option: ");
    
            fgets(userInput,2048,stdin);
            if((strstr(userInput,"Q") != NULL) || 
                            (strstr(userInput,"q") != NULL)) {
                                 optionA = doQuit(optionA);
            } else if ((strstr(userInput,"A") != NULL) || 
                            (strstr(userInput,"a") != NULL)) {
                                 arrayZ[maxR][maxC] = doAdd(arrayZ[maxR][maxC]);
            } else if ((strstr(userInput,"S") != NULL) || 
                            (strstr(userInput,"s") != NULL)) {
                                 arrayZ[maxR][maxC] = doSub(arrayZ[maxR][maxC]);
            } else {
                   printf("Not Valid, Pleas Try Again");
                   }
        doEval();
        return;
    
    }
    
    int doAdd(int arrayZ[maxR][maxC])
    {   
          int row, col;
          int arrayA[maxR][maxC];
          int arrayB[maxR][maxC];
             
            printf("Enter Matrices\n");
            printf("Matrix 1: \n");
              for (row = 0; row < maxR; row++) {
                    for (col = 0; col < maxC; col++) {
                            scanf("%d", &arrayA[row][col]);
                    }
            }
            printf("Matrix 2: \n");
              for (row = 0; row < maxR; row++) {
                    for (col = 0; col < maxC; col++) {
                            scanf("%d", &arrayB[row][col]);
                    }
            }
            printf("\n");
            printf("Sum of Matrices:\n");
              for (row = 0; row < maxR; row++) {
                    for (col = 0; col < maxC; col++) {
                       arrayZ[row][col] = arrayA[row][col] + arrayB[row][col];
                            printf ("% 5d ", arrayZ[row][col]);
                    }
                    printf ("\n\n");
            }
       scanf("%*c");
       return arrayZ[row][col];
    }
    
    
    int doSub(int arrayZ[maxR][maxC])
    {   
          int row, col;
          int arrayA[maxR][maxC];
          int arrayB[maxR][maxC];
             
            printf("Enter Matrices\n");
            printf("Matrix 1: \n");
              for (row = 0; row < maxR; row++) {
                    for (col = 0; col < maxC; col++) {
                            scanf("%d", &arrayA[row][col]);
                    }
            }
            printf("Matrix 2: \n");
              for (row = 0; row < maxR; row++) {
                    for (col = 0; col < maxC; col++) {
                            scanf("%d", &arrayB[row][col]);
                    }
            }
            printf("\n");
            printf("Difference of Matrices:\n");
              for (row = 0; row < maxR; row++) {
                    for (col = 0; col < maxC; col++) {
                       arrayZ[row][col] = arrayA[row][col] - arrayB[row][col];
                            printf ("% 5d ", arrayZ[row][col]);
                    }
                    printf ("\n\n");
            }
       scanf("%*c");
       return arrayZ[row][col];
    }
    
    
    char doQuit(char optionA)
    {
         exit(0);
    }

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    This is a really bad way to do what you want to do. Don't play with recursion unless you know what you are doing. First of all in your case it does more harm than good because recursion is typically slower than iteration, so you may consider running doEval in main in a loop of some sort. Also, your return 0 statement is never reached because you exit manually in doQuit, which is a pretty useless function. It just involves an additional unnecessary context switch on the stack.

    My advice is to rewrite this using a do while loop.

    Also, note that your testing for user input is not accurate. You are searching the location of 'Q', 'A' etc in the user input. That means if I input "QAAAZY", the program will Quit, but only because Q is found in the first if. Your if else cases are not mutually exclusive based on the logic of the input.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    As cool as recursion is, maybe you're running out of stack memory. Start using loops for this kind of repetition and you will avoid this problem.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    27
    Thank you for the input. I am actually doing this for a class assignment, and we are practicing with functions, so I must use functions to complete all of my tasks. We also assume that the user enters everything exactly how we want them to. I did place everything in a do while loop, and got rid of the void doEval function, but I still get an error with the second time I try to enter my data.

    Are there any other things I may be overlooking?

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Using functions to complete all of your tasks does not mean "don't use flow control statements like while". If your instructor lowers your grade, just ignore it. What you're learning to do in this particular area is goofy.

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    27
    I added a do while loop, and got rid of the doEval, but my repetition is still giving the same issues. The program runs fine first time I make a selection and calculate the results, but after my printf options display again and I make my selection it does not calculate.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define maxR 2
    #define maxC 2
    
    int doAdd();
    int doSub();
    char doQuit(char);
    
    main()
    {
          doEval();
          return 0;
    
          char userInput[2048];
          int row, col;
          int arrayA[maxR][maxC];
          int arrayB[maxR][maxC];
          int arrayZ[maxR][maxC];
          char optionA;
    do {
          printf("Enter one of the following options:\n\
                    Q - Quit the program\n\
                    A - Add 2 matrices of size   5 x 5\n\
                    S - Subtract 2 matrices of size   5 x 5\n\
                         Enter option: ");
    
            fgets(userInput,2048,stdin);
            if((strstr(userInput,"Q") != NULL) || 
                            (strstr(userInput,"q") != NULL)) {
                                 optionA = doQuit(optionA);
            } else if ((strstr(userInput,"A") != NULL) || 
                            (strstr(userInput,"a") != NULL)) {
                                 arrayZ[maxR][maxC] = doAdd(arrayZ[maxR][maxC]);
            } else if ((strstr(userInput,"S") != NULL) || 
                            (strstr(userInput,"s") != NULL)) {
                                 arrayZ[maxR][maxC] = doSub(arrayZ[maxR][maxC]);
            } else {
                   printf("Not Valid, Pleas Try Again");
                   }
        }while(0 == 0);
        return;
    
    }
    
    int doAdd(int arrayZ[maxR][maxC])
    {   
          int row, col;
          int arrayA[maxR][maxC];
          int arrayB[maxR][maxC];
             
            printf("Enter Matrices\n");
            printf("Matrix 1: \n");
              for (row = 0; row < maxR; row++) {
                    for (col = 0; col < maxC; col++) {
                            scanf("%d", &arrayA[row][col]);
                    }
            }
            printf("Matrix 2: \n");
              for (row = 0; row < maxR; row++) {
                    for (col = 0; col < maxC; col++) {
                            scanf("%d", &arrayB[row][col]);
                    }
            }
            printf("\n");
            printf("Sum of Matrices:\n");
              for (row = 0; row < maxR; row++) {
                    for (col = 0; col < maxC; col++) {
                       arrayZ[row][col] = arrayA[row][col] + arrayB[row][col];
                            printf ("% 5d ", arrayZ[row][col]);
                    }
                    printf ("\n\n");
            }
       scanf("%*c");
       return arrayZ[row][col];
    }
    
    
    int doSub(int arrayZ[maxR][maxC])
    {   
          int row, col;
          int arrayA[maxR][maxC];
          int arrayB[maxR][maxC];
             
            printf("Enter Matrices\n");
            printf("Matrix 1: \n");
              for (row = 0; row < maxR; row++) {
                    for (col = 0; col < maxC; col++) {
                            scanf("%d", &arrayA[row][col]);
                    }
            }
            printf("Matrix 2: \n");
              for (row = 0; row < maxR; row++) {
                    for (col = 0; col < maxC; col++) {
                            scanf("%d", &arrayB[row][col]);
                    }
            }
            printf("\n");
            printf("Difference of Matrices:\n");
              for (row = 0; row < maxR; row++) {
                    for (col = 0; col < maxC; col++) {
                       arrayZ[row][col] = arrayA[row][col] - arrayB[row][col];
                            printf ("% 5d ", arrayZ[row][col]);
                    }
                    printf ("\n\n");
            }
       scanf("%*c");
       return arrayZ[row][col];
    }
    
    
    char doQuit(char optionA)
    {
         exit(0);
    }

  7. #7
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Can you provide the exact input you are passing to the program each time? (i.e. the commands and data) Just list it here.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    27
    i am currently inputting '1 2 3 4' for both matrices, and using the 'A' option

    I think it might be the dev-C++ compiler. I have heard that I might need to use 'int *' rather than 'int'. How do I do that?

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Start compiling with warnings on:
    Code:
     arrayZ[maxR][maxC] = doAdd(arrayZ[maxR][maxC]); /* <-- WRONG */
    You are passing a location past the end of the array. Actually, you're trying to pass the value that is at the location in memory of the place past the end of the array.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 13
    Last Post: 12-14-2007, 03:34 PM
  2. Switch structure not working
    By him61 in forum C++ Programming
    Replies: 8
    Last Post: 05-23-2007, 05:35 PM
  3. Replies: 5
    Last Post: 02-14-2006, 09:04 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Creating Dynamic Structure in C
    By SomeNath in forum C Programming
    Replies: 1
    Last Post: 03-19-2005, 09:11 AM