Question on structures, arrays and functions
Jan 14, 2015 at 7:42pm UTC
Hi, I have to write a C++ program by passing structs to three functions
Input_stud (to enter data about students eg name, id and testscores),
Calculate_over (to calculate their overall percentage marks) and
Disp_stud (to display their name, id and overall percentage marks) whilst making use of arrays to store the data. My teacher hasn't yet covered pointers so I can't use pointers.
I have to enter the data for
n students but the three functions are not able to access the
n from the main.
Here is my code:
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 78 79 80 81 82 83 84
#include <iostream>
using namespace std;
struct student
{
int id;
string surname;
string othername;
int marks[];
float total, avg;
};
void Input_stud(student []);
void Calculate_over(student []);
void Disp_stud(student []);
int main()
{
student num[5];
int n;
cout<< "Enter the number of students.\n" ;
cin>> n;
Input_stud(num);
Calculate_over(num);
Disp_stud(num);
return 0;
}
void Input_stud (student s[])
{
for (int i=0; i<=n; i++)
{
cout<< "\nEnter student's info:\n" ;
cout<< "Surname: " ;
cin>>s[i].surname;
cout<< "Othername: " ;
cin>>s[i].othername;
cout<< "ID: " ;
cin>>s[i].id;
for (int j=0;j<4;j++)
{
cout<< "Testscores in" <<endl;
cout<<"subject " <<j+1<< ":" ;
cin>> s[i].marks[j];
}
}
}
void Calculate_over(student s[])
{
for (int k=0; k<=n; k++)
{
s[k].avg= (s[k].marks[0]+ s[k].marks[1]+ s[k].marks[2]+s[k].marks[3])/4.0;
cout<< "Overall percentage marks: " <<s[k].avg<< "%" <<endl;
}
}
void Disp_stud(student s[])
{
for (int i=0; i<=n; i++)
{
cout<< "\nDisplaying student's info: \n" ;
cout<< "Name: " <<s[i].surname<< " " <<s[i].othername<<endl;
cout<< "ID: " <<s[i].id<<endl;
cout<< "Overall percentage marks: " <<s[i].avg<<endl;
}
}
Last edited on Jan 14, 2015 at 7:46pm UTC
Jan 14, 2015 at 7:58pm UTC
The first thing I notice is that the arguments your sending to your functions are of type, int , when the parameters are supposed to be receiving arrays of type, student
Jan 14, 2015 at 8:04pm UTC
There were some bugs to fix... here you go. Learn it, love it, live it!
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 78 79 80 81 82 83 84 85 86 87 88
#include <iostream>
#include <string>
using namespace std;
struct student
{
int id;
string surname;
string othername;
int marks[5];
float total, avg;
};
void Input_stud(student [], int );
void Calculate_over(student [], int );
void Disp_stud(student [], int );
int main()
{
student num[5];
int n;
cout<< "Enter the number of students.\n" ;
cin>> n;
Input_stud(num, n);
Calculate_over(num, n);
Disp_stud(num, n);
cin.clear();
cin.get();
cin.get();
return 0;
}
void Input_stud (student s[], int n)
{
for (int i=0; i<n; i++)
{
cout<< "\nEnter student's info:\n" ;
cout<< "Surname: " ;
cin>>s[i].surname;
cout<< "Othername: " ;
cin>>s[i].othername;
cout<< "ID: " ;
cin>>s[i].id;
for (int j=0;j<4;j++)
{
cout<< "Testscores in" <<endl;
cout<<"subject " <<j+1<< ":" ;
cin>> s[i].marks[j];
}
}
}
void Calculate_over(student s[], int n)
{
for (int k=0; k<n; k++)
{
s[k].avg= (s[k].marks[0]+ s[k].marks[1]+ s[k].marks[2]+s[k].marks[3])/4.0;
cout<< "Overall percentage marks: " <<s[k].avg<< "%" <<endl;
}
}
void Disp_stud(student s[], int n)
{
for (int i=0; i<n; i++)
{
cout<< "\nDisplaying student's info: \n" ;
cout<< "Name: " <<s[i].surname<< " " <<s[i].othername<<endl;
cout<< "ID: " <<s[i].id<<endl;
cout<< "Overall percentage marks: " <<s[i].avg<<endl;
}
}
Jan 14, 2015 at 8:19pm UTC
Thanks a lot, Manga and Anthony!
I have understood that I have to pass int n also as a parameter.
Topic archived. No new replies allowed.