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():
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <iostream>
#include <string>
#include <sstream>

#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<i; n++){
		cin.get();		
		cout<<"Enter student's name: ";
		getline(cin,st[n].Name);
		cout<<"Enter student's group: ";
		getline(cin,mystr);
		stringstream(mystr)>>st[n].Group;
		cout<<"Enter 5 student's marks: ";
		for(t=0; t<Mark; t++){
			cin>>st[n].Ses[t];
		}
	}
	
	cout<<endl;
	cout<<"Students with average mark more then 4.0:"<<endl;
	for(n=0; n<i; n++){
		if((res[n]=(double)(st[n].Ses[0]+st[n].Ses[1]+st[n].Ses[2]+st[n].Ses[3]
			+st[n].Ses[4])/5)>=4){
			cout<<st[n].Name<<endl;
			cout<<"Group ES-"<<st[n].Group<<endl;
		}
	}
	if(res[0]<=4 && res[1]<=4 && res[2]<=4 && res[3]<=4 && res[4]<=4){
		cerr<<"No one student with average mark more then 4.0"<<endl;
	}

	double temp, temp_a;
	string temp_n;
	cout<<endl;
	cout<<"The list of student ordered by group's take off:"<<endl;	
	for(n=0; n<i; n++){
		for(j=0; j<i-1; j++){
			if(st[j].Group>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<i; n++){
		cout<<"Name: "<<st[n].Name<<endl;
		cout<<"Group: ES-"<<st[n].Group<<endl;
		cout<<"Average mark: "<<res[n]<<endl;
	}

	return 0;
}


And here is the code with problems:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <iostream>
#include <string>
#include <sstream>

#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; n<i; n++){
		cin.get();
		cout<<"Enter student's name: ";
		getline(cin,p->Name);
		cout<<"Enter student's group: ";
		getline(cin,mystr);
		stringstream(mystr)>>p->Group;
		cout<<"Enter 5 student's marks: ";
		for(t=0; t<Mark; t++){
			cin>>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<<endl;
	cout<<"Students with average mark more than 4.0:"<<endl;
	for(n=0; n<i; n++){
		if((res[n]=(double)(p->Ses[0]+p->Ses[1]+p->Ses[2]+p->Ses[3]+p->Ses[4])/5)
			>=4.0){
			cout<<p->Name<<endl;
			cout<<"Group ES-"<<p->Group<<endl;
		}
	}
	cout<<res[0]<<", "<<res[1]<<", "<<res[2]<<", "<<res[3]<<", "<<res[4]<<endl;
	if(res[0]<=4 && res[1]<=4 && res[2]<=4 && res[3]<=4 && res[4]<=4){
		cerr<<"No one student with average mark more then 4.0"<<endl;
	}
		
	return *a;
}

int main()
{
	int i, n, t;
	double *a, res[10];
	sStud *p, st;
	
	cout<<"Enter number of students: ";
	cin>>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.