Hey guys,

So I've thoroughly gone through the topics by searching this site but I couldn't find something which can help me.

I'm making a task for my friend and let me tell you that it's been a year or more since I've used C++. I've moved on to C# and JAVA.

So the problem I'm facing is with pointers and returning struct through pointer.

Here's the code below:

#include <stdio.h>
#include <conio.h>
#include <iostream.h>

    struct student
   {
    int SUB[3];
   };

   struct AVGPCT
   {
    float avg;
      float pct;
   };


AVGPCT* CALC(student *A)
{
   int x,y;
    AVGPCT D[5]={0.0,0.0,0.0,0.0,0.0};

   for(x=0;x<5;x++)
   {
    for(y=0;y<3;y++)
      {
        D[x].avg+=A[x].SUB[y];
         D[x].pct+=A[x].SUB[y];
      }
   }

   for(x=0;x<5;x++)
   {
    D[x].avg/=2;
      D[x].pct/=300;
   }
   return (D);
}

void main(void)
{
   int i,j;
   student S[5];
   AVGPCT *V;

   for(i=0;i<5;i++)
   {
    for(j=0;j<3;j++)
      {
        cout << "Enter Marks Of Subject[" << (j+1) << "] Of Student [" << (i+1) <<"] [Max=100]:";
        cin >> S[i].SUB[j];
      }
   }

   V = CALC(S);
   cout << "Average Number Of Student [1]: " << V->avg;
   cout << (*V).pct;

   getch();
}

Now the problem is, it is giving me a hard time to retrieve correct values from the location.

I'm trying to increment the pointer with V++. The funny thing is it is printing the values correctly if I use:

cout << V->avg;
cout << V->pct;
V++;
cout << V->avg;
cout << V->pct;

And so on. But if I use loop or write even a single line or use endl or \n, the values jumble up and gibberish values appear.

So what could be going wrong here?

It is important that I use struct and also please guide according to the code. I would appreciate if no one gives advice like "You should write like this or should've used this or that".

Thanx in advance.

Regards.

You can't return a pointer from off the stack. Add the error handling
#include <cstdio>
#include <iostream>

using namespace std;
    struct student
   {
    int SUB[3];
   };

   struct AVGPCT
   {
    float avg;
      float pct;
   };


AVGPCT* CALC(student *A)
{
   int x,y;
   //---------------------------------------
   // Have to allocate the memory here
   AVGPCT *D = new AVGPCT[5];
   for(x=0;x<5;x++)
   {
      D[x].avg = 0;
      D[x].pct = 0;
      for(y=0;y<3;y++)
      {
        D[x].avg+=A[x].SUB[y];
         D[x].pct+=A[x].SUB[y];
      }
   }

   for(x=0;x<5;x++)
   {
      //---------------------------------
      //   I changed the value here to 3
      //   because there are 3 grades
      D[x].avg/=3;
      D[x].pct/=300;
   }
   return (D);
}

int main(void)
{
   int i,j;
   student S[5];
   AVGPCT *V;

   for(i=0;i<5;i++)
   {
    for(j=0;j<3;j++)
      {
        cout << "Enter Marks Of Subject[" << (j+1) << "] Of Student [" << (i+1) <<"] [Max=100]:";
        cin >> S[i].SUB[j];
      }
   }

   V = CALC(S);
   for ( int i(0);i<5;i++){
     cout << "Average Number Of Student [" << i << "]: " <<  V[i].avg << ", " << V[i].pct << endl;
   }
   //--------------------
   // Remove the memory we allocated
   delete [] V;
   return 0;
}
Output:
./a.out
Enter Marks Of Subject[1] Of Student [1] [Max=100]:1 
Enter Marks Of Subject[2] Of Student [1] [Max=100]:1
Enter Marks Of Subject[3] Of Student [1] [Max=100]:1
Enter Marks Of Subject[1] Of Student [2] [Max=100]:2
Enter Marks Of Subject[2] Of Student [2] [Max=100]:2
Enter Marks Of Subject[3] Of Student [2] [Max=100]:2
Enter Marks Of Subject[1] Of Student [3] [Max=100]:3
Enter Marks Of Subject[2] Of Student [3] [Max=100]:3
Enter Marks Of Subject[3] Of Student [3] [Max=100]:3
Enter Marks Of Subject[1] Of Student [4] [Max=100]:4
Enter Marks Of Subject[2] Of Student [4] [Max=100]:4
Enter Marks Of Subject[3] Of Student [4] [Max=100]:4
Enter Marks Of Subject[1] Of Student [5] [Max=100]:5
Enter Marks Of Subject[2] Of Student [5] [Max=100]:5
Enter Marks Of Subject[3] Of Student [5] [Max=100]:5
Average Number Of Student [0]: 1, 0.01
Average Number Of Student [1]: 2, 0.02
Average Number Of Student [2]: 3, 0.03
Average Number Of Student [3]: 4, 0.04
Average Number Of Student [4]: 5, 0.05

@histrungalot

Your code is fine and it sure will solve Major Aly's problem. But your code has limitations and can initiate memory leak errors since your AVGPCT* CALC(student *A) function assumes that *A* is an array of five. What if student is a pointer to an array of less than 5 students?

To make the code dynamic, write it this way:

AVGPCT* CALC(student *A, int cStudents)
{
   int x,y;
   //---------------------------------------
   // Have to allocate the memory here
   AVGPCT *D = new AVGPCT[cStudents];
   for(x=0;x<cStudents;x++)
   {
      D[x].avg = 0;
      D[x].pct = 0;
      for(y=0;y<3;y++)
      {
        D[x].avg+=A[x].SUB[y];
         D[x].pct+=A[x].SUB[y];
      }
   }
   for(x=0;x<cStudents;x++)
   {
      //---------------------------------
      //   I changed the value here to 3
      //   because there are 3 grades
      D[x].avg/=3;
      D[x].pct/=300;
   }
   return (D);
}

And call it this way:

student S[5];
AVGPCT *D = CALC(S, 5);
...

@RonalBertogi, true.
Sometimes I fix and sometimes I rewrite. This time I fix. The rest is left as an exercise for the student.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.