program inconsistency

This is a discussion on program inconsistency within the C Programming forums, part of the General Programming Boards category; So I'm writing a program for a Practice Lab Final and my professor gave us four test cases. Two out ...

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    8

    program inconsistency

    So I'm writing a program for a Practice Lab Final and my professor gave us four test cases. Two out of the four test cases work perfectly with my program. The last two test cases however give me differences between his output and my output. Here's the code for my program so far.

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <strings.h>
    
    int splitStrings( char fName[20], char lName[20], int age )
    {
      scanf( " %s %s %d", fName,lName,&age );
    
      return age;
    }
    
    int search( char names[21][20], int ages[21], char value[20], int *totAges )
    {
      char tempArr[20];
      int rCtr = 0;
      int ctr = 0;
      int ageCtr = 0;
    
      for( rCtr = 0; rCtr < 20; rCtr++ ) {
        strcpy( tempArr, names[rCtr] );
        if( !strcasecmp( tempArr, value ) ) {
          ctr++;
          ageCtr += ages[rCtr];
        }
      }
      *totAges = ageCtr;
    
      return ctr;
    }
    
    int main( void )
    {
      char firstName[21][20];
      char lastName[21][20];
      int age[21];
      char first[20];
      char last[20];
      char fValue[20];
      char lValue[20];
      int tempAge = 0;
      int tAge = 0;
      int fTotalAge = 0;
      int lTotalAge = 0;
      int people = 0;
      int rCtr = 0;
      int cCtr = 0;
      int fNameCtr = 0;
      int lNameCtr = 0;
    
      for( rCtr = 0; rCtr < 20; rCtr++,people++ ) {
        tempAge = splitStrings( first, last, tempAge );
        tAge += tempAge;
    
        for( cCtr = 0; cCtr < 3; cCtr++ ) {
          if( cCtr == 0 ) {
            strcpy( firstName[rCtr], first );
          }
          if( cCtr == 1 ) {
            strcpy( lastName[rCtr], last );
          }
          if( cCtr == 2 ) {
            age[rCtr] = tempAge;
          }
        }
    
        if( !strcmp(firstName[rCtr], "DONE") && !strcmp(lastName[rCtr], "DONE") &&
            age[rCtr] == 0 ) {
          break;
        }
      }
    
      scanf( " %s", fValue );
      scanf( " %s", lValue );
    
      fNameCtr = search( firstName, age, fValue, &fTotalAge );
      lNameCtr = search( lastName, age, lValue, &lTotalAge );
    
      printf( "%d people.  Total age is %d.\n", people, tAge );
      printf( "%d people with first name %s.  Total age is %d.\n",
              fNameCtr, fValue, fTotalAge );
      printf( "%d people with last name %s.  Total age is %d.\n",
              lNameCtr, lValue, lTotalAge );
    
      return 0;
    }
    It seems like for the last two test cases, my program never even reaches the last two scanf()'s in main, which ask for the search value. Any ideas? Thanks

  2. #2
    Registered User
    Join Date
    Dec 2008
    Posts
    8
    Also, if you need the input for the test cases, I have those available as well.

  3. #3
    Banned
    Join Date
    Dec 2008
    Posts
    49
    Example:
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <strings.h>
    
    int splitStrings( char fName[20], char lName[20], int *age )
    {
      scanf( " &#37;s %s %d", fName,lName,age );
    
      return age;
    }
    
    int search( char names[21][20], int ages[21], char value[20], int *totAges )
    {
      char tempArr[20];
      int rCtr = 0;
      int ctr = 0;
      int ageCtr = 0;
    
      for( rCtr = 0; rCtr < 20; rCtr++ ) {
        strcpy( tempArr, names[rCtr] );
        if( !strcasecmp( tempArr, value ) ) {
          ctr++;
          ageCtr += ages[rCtr];
        }
      }
      *totAges = ageCtr;
    
      return ctr;
    }
    
    int main( void )
    {
      char firstName[21][20];
      char lastName[21][20];
      int age[21];
      char first[20];
      char last[20];
      char fValue[20];
      char lValue[20];
      int tempAge = 0;
      int tAge = 0;
      int fTotalAge = 0;
      int lTotalAge = 0;
      int people = 0;
      int rCtr = 0;
      int cCtr = 0;
      int fNameCtr = 0;
      int lNameCtr = 0;
    
      for( rCtr = 0; rCtr < 20; rCtr++,people++ ) {
        tempAge = splitStrings( first, last, &tempAge );
        tAge += tempAge;
    
        for( cCtr = 0; cCtr < 3; cCtr++ ) {
          if( cCtr == 0 ) {
            strcpy( firstName[rCtr], first );
          }
          if( cCtr == 1 ) {
            strcpy( lastName[rCtr], last );
          }
          if( cCtr == 2 ) {
            age[rCtr] = tempAge;
          }
        }
    
        if( !strcmp(firstName[rCtr], "DONE") && !strcmp(lastName[rCtr], "DONE") &&
            age[rCtr] == 0 ) {
          break;
        }
      }
    
      scanf( " %s", fValue );
      scanf( " %s", lValue );
    
      fNameCtr = search( firstName, age, fValue, &fTotalAge );
      lNameCtr = search( lastName, age, lValue, &lTotalAge );
    
      printf( "%d people.  Total age is %d.\n", people, tAge );
      printf( "%d people with first name %s.  Total age is %d.\n",
              fNameCtr, fValue, fTotalAge );
      printf( "%d people with last name %s.  Total age is %d.\n",
              lNameCtr, lValue, lTotalAge );
    
      return 0;
    }

  4. #4
    Registered User
    Join Date
    Dec 2008
    Posts
    8
    nevermind, problem solved!

  5. #5
    Banned
    Join Date
    Dec 2008
    Posts
    49
    Did you catch the pointer thing? Green isn't the easiest color in the world to see against the grey. I probably should have made the font larger.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 11:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21