### Problem with loop

Hello, I did this function in main() and it works. Yet now I want to do it through the functions and I have a problem and don't know what to do. After execution the fucntion assigns the last value to all.
Here is the code done in main():
 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677`` ``````#include #include #include #define Mark 5 using namespace std; struct sStud{ string Name; int Group; int Ses[Mark]; }; int main() { int i, n, t, j; double res[10]; string mystr; cout<<"Enter number of students: "; cin>>i; sStud st[i]; for(n=0; n>st[n].Group; cout<<"Enter 5 student's marks: "; for(t=0; t>st[n].Ses[t]; } } cout<=4){ cout<st[j+1].Group){ temp_n=st[j].Name; temp=st[j].Group; temp_a=res[j]; st[j].Name=st[j+1].Name; st[j].Group=st[j+1].Group; res[j]=res[j+1]; st[j+1].Name=temp_n; st[j+1].Group=temp; res[j+1]=temp_a; } } } for(n=0; n

And here is the code with problems:
 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273`` ``````#include #include #include #define Mark 5 using namespace std; struct sStud{ string Name; int Group; int Ses[Mark]; }; void input(int, sStud*); double average(double*, sStud*, int); void input(int i, sStud *p) { int n, t; string mystr; for(n=0; nName); cout<<"Enter student's group: "; getline(cin,mystr); stringstream(mystr)>>p->Group; cout<<"Enter 5 student's marks: "; for(t=0; t>p->Ses[t]; } } } double average(double *a, sStud *p, int i) { int n; double res[10]; a=&res[10]; //I don't know where I should point to array here or cout<Ses[0]+p->Ses[1]+p->Ses[2]+p->Ses[3]+p->Ses[4])/5) >=4.0){ cout<Name<Group<>i; a=&res[10]; //here p=&st; input(i,p); average(a,p,i); return 0; }``````
Last edited on
I don't know what should I do at all. Is mistake in body of that function or this function is totaly wrong or the declaration in main() is wrong? Please, someone give me some direction.
Last edited on
it seems you are not changing the pointer p while reading or calculating averages. the same pointer p gets overwritten while
for(t=0; t<Mark; t++){
cin>>p->Ses[t];
}
and the average function repeatedly checks the same pointer's value p->ses[].

Also, the avergae function is supposed to return a value but no variable is assigned to it while calling.
About return I chenged. Unfortunetly, I still cann't do this function.
Last edited on
there is confusion in your pointer assignments as I said earlier. You are trying to store the data of all the students in the same pointer variable. That's why its value is getting over-written. A different variable should store each student's data.
look at some examples of pointer assignments and usage.
Topic archived. No new replies allowed.