[postlink]
http://uknow-iknow.blogspot.com/2016/11/c-sturctarrayloop.html[/postlink]
#include<iostream>
#include <cstdlib>
#include<string>
using namespace std;
struct students{
int id;
char name[30];
char sex[1];
float scores1;
float scores2;
float avg;
};
void input_info(struct students s[],int n)
{
int i;
for(i=0;i<n;i++)
{
cout<<"_________________________________________________________________________________________"<<endl;
cout<<"ID : "<<i+1<<endl;
cout<<"Name :";cin>>s[i].name;
gd:
cout<<"Sex (f/m) :";cin>>s[i].sex;
if(s[i].sex[0]!='f' && s[i].sex[0]!='m' && s[i].sex[0]!='F' && s[i].sex[0]!='M')
{
cout<<"\nInvalide Gender(M/F), Please Check and try again ...!"<<endl;
goto gd;
break;
}
s1:
cout<<"Score1 : ";cin>>s[i].scores1;
if(s[i].scores1<0 || s[i].scores1>100)
{
cout<<"\nInvalide Number of Score( 0p - 100p ), Please Check and try again ...!"<<endl;
goto s1;
break;
}
s2:
cout<<"Score2 : ";cin>>s[i].scores2;
if(s[i].scores2<0 || s[i].scores2>100)
{
cout<<"\nInvalide Number of Score( 0p - 100p ), Please Check and try again ...!"<<endl;
goto s2;
break;
}
s[i].avg=(s[i].scores1+s[i].scores2)/2;
}
}
void data(struct students s[],int n)
{
int i;
cout<<"_________________________________________________________________________________________"<<endl;
cout<<"ID\tName\t\t\tSex\tScore_1\t\tScore_2\t\tAvg\tResult"<<endl;
cout<<"-----------------------------------------------------------------------------------------"<<endl;
for(i=0;i<n;i++)
{
if(s[i].avg>=0&&s[i].avg<50)
{
cout<<i+1<<"\t"<<s[i].name<<"\t\t\t"<<s[i].sex<<"\t"<<s[i].scores1<<"\t\t"<<s[i].scores2<<"\t\t"<<s[i].avg<<"\tFail"<<endl;
}
else
{
cout<<i+1<<"\t"<<s[i].name<<"\t\t\t"<<s[i].sex<<"\t"<<s[i].scores1<<"\t\t"<<s[i].scores2<<"\t\t"<<s[i].avg<<"\tPass"<<endl;
}
cout<<"-----------------------------------------------------------------------------------------"<<endl;
}
}
int main(){
struct students s[50];
int n,i,j,num,d;
float av;
char search[1],ch,chpf[1];
do
{
system("cls");
en_num:
cout<<"Enter Number : "; cin>>n;
if(n>0){
input_info(s, n);
data(s,n);
number:
cout<<"\n1) Search by Sex."<<endl;
cout<<"2) Search by Avg."<<endl;
cout<<"3) Serach Follow result(p/f)"<<endl;
cout<<"Choose Number for Search(1,2,3) : ";cin>>num;
if(num==1)
{
sch:
cout<<"1) Press F for Femal, Press M for Male(F/M) : ";cin>>search;
if(search[0]=='f'||search[0]=='F')
{
cout<<"_________________________________________________________________________________________"<<endl;
cout<<"ID\tName\t\t\tSex\tScore_1\t\tScore_2\t\tAvg\tResult"<<endl;
cout<<"-----------------------------------------------------------------------------------------"<<endl;
i=0;
while(i<n)
{
int j=i;
while(j<=n)
{
if((s[j].sex)[i]=='f'||(s[j].sex)[i]=='F')
{
if(s[j].avg>=0&&s[j].avg<50)
{
cout<<j+1<<"\t"<<(s[j].name)<<"\t\t\t"<<s[j].sex<<"\t"<<s[j].scores1<<"\t\t"<<s[j].scores2<<"\t\t"<<s[j].avg<<"\tFail"<<endl;
cout<<"-----------------------------------------------------------------------------------------"<<endl;
}
if(s[j].avg>=50&&s[j].avg<=100)
{
cout<<j+1<<"\t"<<(s[j].name)<<"\t\t\t"<<s[j].sex<<"\t"<<s[j].scores1<<"\t\t"<<s[j].scores2<<"\t\t"<<s[j].avg<<"\tPass"<<endl;
cout<<"-----------------------------------------------------------------------------------------"<<endl;
}
}
j++;
}
i++;
}
}
else if(search[0]=='m'||search[0]=='M')
{
cout<<"_________________________________________________________________________________________"<<endl;
cout<<"ID\tName\t\t\tSex\tScore_1\t\tScore_2\t\tAvg\tResult"<<endl;
cout<<"-----------------------------------------------------------------------------------------"<<endl;
i=0;
while(i<n)
{
int j=i;
while(j<=n)
{
if((s[j].sex)[i]=='m'||(s[j].sex)[i]=='M')
{
if(s[j].avg>=0&&s[j].avg<50)
{
cout<<j+1<<"\t"<<(s[j].name)<<"\t\t\t"<<s[j].sex<<"\t"<<s[j].scores1<<"\t\t"<<s[j].scores2<<"\t\t"<<s[j].avg<<"\tFail"<<endl;
cout<<"-----------------------------------------------------------------------------------------"<<endl;
}
if(s[j].avg>=50&&s[j].avg<=100)
{
cout<<j+1<<"\t"<<(s[j].name)<<"\t\t\t"<<s[j].sex<<"\t"<<s[j].scores1<<"\t\t"<<s[j].scores2<<"\t\t"<<s[j].avg<<"\tPass"<<endl;
cout<<"-----------------------------------------------------------------------------------------"<<endl;
}
}
j++;
}
i++;
}
}
else
{
cout<<"Check gender again "<<endl;
goto sch;
}
}
else if(num==2)
{
cout<<"\n2) Please Enter Number of Average for search : ";cin>>av;
cout<<"_________________________________________________________________________________________"<<endl;
cout<<"ID\tName\t\t\tSex\tScore_1\t\tScore_2\t\tAvg\tResult"<<endl;
cout<<"-----------------------------------------------------------------------------------------"<<endl;
int j=0;
while(j<n)
{
if((s[j].avg)==av)
{
if(s[j].avg>=0&&s[j].avg<50)
{
cout<<j+1<<"\t"<<(s[j].name)<<"\t\t\t"<<s[j].sex<<"\t"<<s[j].scores1<<"\t\t"<<s[j].scores2<<"\t\t"<<s[j].avg<<"\tFail"<<endl;
cout<<"-----------------------------------------------------------------------------------------"<<endl;
}
if(s[j].avg>=50&&s[j].avg<=100)
{
cout<<j+1<<"\t"<<(s[j].name)<<"\t\t\t"<<s[j].sex<<"\t"<<s[j].scores1<<"\t\t"<<s[j].scores2<<"\t\t"<<s[j].avg<<"\tPass"<<endl;
cout<<"-----------------------------------------------------------------------------------------"<<endl;
}
}
j++;
}
}
else if(num==3)
{
pf:
cout<<"\n3) Press \"P\"or \"F\" for search data Pass or Fail(P/F) : ";cin>>chpf;
if(chpf[0]=='f'||chpf[0]=='F')
{
cout<<"_________________________________________________________________________________________"<<endl;
cout<<"ID\tName\t\t\tSex\tScore_1\t\tScore_2\t\tAvg\tResult"<<endl;
cout<<"-----------------------------------------------------------------------------------------"<<endl;
int j=0;
while(j<n)
{
if((s[j].avg)>=0&&(s[j].avg)<=50)
{
cout<<j+1<<"\t"<<(s[j].name)<<"\t\t\t"<<s[j].sex<<"\t"<<s[j].scores1<<"\t\t"<<s[j].scores2<<"\t\t"<<s[j].avg<<"\tFail"<<endl;
cout<<"-----------------------------------------------------------------------------------------"<<endl;
}
j++;
}
}
else if(chpf[0]=='p'||chpf[0]=='P')
{
cout<<"_________________________________________________________________________________________"<<endl;
cout<<"ID\tName\t\t\tSex\tScore_1\t\tScore_2\t\tAvg\tResult"<<endl;
cout<<"-----------------------------------------------------------------------------------------"<<endl;
int j=0;
while(j<n)
{
if((s[j].avg)>=50&&(s[j].avg)<=100)
{
cout<<j+1<<"\t"<<(s[j].name)<<"\t\t\t"<<s[j].sex<<"\t"<<s[j].scores1<<"\t\t"<<s[j].scores2<<"\t\t"<<s[j].avg<<"\tPass"<<endl;
cout<<"-----------------------------------------------------------------------------------------"<<endl;
}
j++;
}
}
else
{
cout<<"\nPlease check of you result again "<<endl;
goto pf;
break;
}
}
else
{
goto number;
}
cout<<"\nDo you want to choose number for search again ?(y/n) : ";cin>>ch;
if(ch=='y'||ch=='Y')
{
goto number;
}
}
else
{
cout<<"\nPlease check Number for Enter (1 to many )"<<endl;
break;
}
cout<<"\nDo you want to Creat Data again ?(y/n) : ";cin>>ch;
}
while(ch=='y'||ch=='Y');
return 0;
}
//output
#include <cstdlib>
#include<string>
using namespace std;
struct students{
int id;
char name[30];
char sex[1];
float scores1;
float scores2;
float avg;
};
void input_info(struct students s[],int n)
{
int i;
for(i=0;i<n;i++)
{
cout<<"_________________________________________________________________________________________"<<endl;
cout<<"ID : "<<i+1<<endl;
cout<<"Name :";cin>>s[i].name;
gd:
cout<<"Sex (f/m) :";cin>>s[i].sex;
if(s[i].sex[0]!='f' && s[i].sex[0]!='m' && s[i].sex[0]!='F' && s[i].sex[0]!='M')
{
cout<<"\nInvalide Gender(M/F), Please Check and try again ...!"<<endl;
goto gd;
break;
}
s1:
cout<<"Score1 : ";cin>>s[i].scores1;
if(s[i].scores1<0 || s[i].scores1>100)
{
cout<<"\nInvalide Number of Score( 0p - 100p ), Please Check and try again ...!"<<endl;
goto s1;
break;
}
s2:
cout<<"Score2 : ";cin>>s[i].scores2;
if(s[i].scores2<0 || s[i].scores2>100)
{
cout<<"\nInvalide Number of Score( 0p - 100p ), Please Check and try again ...!"<<endl;
goto s2;
break;
}
s[i].avg=(s[i].scores1+s[i].scores2)/2;
}
}
void data(struct students s[],int n)
{
int i;
cout<<"_________________________________________________________________________________________"<<endl;
cout<<"ID\tName\t\t\tSex\tScore_1\t\tScore_2\t\tAvg\tResult"<<endl;
cout<<"-----------------------------------------------------------------------------------------"<<endl;
for(i=0;i<n;i++)
{
if(s[i].avg>=0&&s[i].avg<50)
{
cout<<i+1<<"\t"<<s[i].name<<"\t\t\t"<<s[i].sex<<"\t"<<s[i].scores1<<"\t\t"<<s[i].scores2<<"\t\t"<<s[i].avg<<"\tFail"<<endl;
}
else
{
cout<<i+1<<"\t"<<s[i].name<<"\t\t\t"<<s[i].sex<<"\t"<<s[i].scores1<<"\t\t"<<s[i].scores2<<"\t\t"<<s[i].avg<<"\tPass"<<endl;
}
cout<<"-----------------------------------------------------------------------------------------"<<endl;
}
}
int main(){
struct students s[50];
int n,i,j,num,d;
float av;
char search[1],ch,chpf[1];
do
{
system("cls");
en_num:
cout<<"Enter Number : "; cin>>n;
if(n>0){
input_info(s, n);
data(s,n);
number:
cout<<"\n1) Search by Sex."<<endl;
cout<<"2) Search by Avg."<<endl;
cout<<"3) Serach Follow result(p/f)"<<endl;
cout<<"Choose Number for Search(1,2,3) : ";cin>>num;
if(num==1)
{
sch:
cout<<"1) Press F for Femal, Press M for Male(F/M) : ";cin>>search;
if(search[0]=='f'||search[0]=='F')
{
cout<<"_________________________________________________________________________________________"<<endl;
cout<<"ID\tName\t\t\tSex\tScore_1\t\tScore_2\t\tAvg\tResult"<<endl;
cout<<"-----------------------------------------------------------------------------------------"<<endl;
i=0;
while(i<n)
{
int j=i;
while(j<=n)
{
if((s[j].sex)[i]=='f'||(s[j].sex)[i]=='F')
{
if(s[j].avg>=0&&s[j].avg<50)
{
cout<<j+1<<"\t"<<(s[j].name)<<"\t\t\t"<<s[j].sex<<"\t"<<s[j].scores1<<"\t\t"<<s[j].scores2<<"\t\t"<<s[j].avg<<"\tFail"<<endl;
cout<<"-----------------------------------------------------------------------------------------"<<endl;
}
if(s[j].avg>=50&&s[j].avg<=100)
{
cout<<j+1<<"\t"<<(s[j].name)<<"\t\t\t"<<s[j].sex<<"\t"<<s[j].scores1<<"\t\t"<<s[j].scores2<<"\t\t"<<s[j].avg<<"\tPass"<<endl;
cout<<"-----------------------------------------------------------------------------------------"<<endl;
}
}
j++;
}
i++;
}
}
else if(search[0]=='m'||search[0]=='M')
{
cout<<"_________________________________________________________________________________________"<<endl;
cout<<"ID\tName\t\t\tSex\tScore_1\t\tScore_2\t\tAvg\tResult"<<endl;
cout<<"-----------------------------------------------------------------------------------------"<<endl;
i=0;
while(i<n)
{
int j=i;
while(j<=n)
{
if((s[j].sex)[i]=='m'||(s[j].sex)[i]=='M')
{
if(s[j].avg>=0&&s[j].avg<50)
{
cout<<j+1<<"\t"<<(s[j].name)<<"\t\t\t"<<s[j].sex<<"\t"<<s[j].scores1<<"\t\t"<<s[j].scores2<<"\t\t"<<s[j].avg<<"\tFail"<<endl;
cout<<"-----------------------------------------------------------------------------------------"<<endl;
}
if(s[j].avg>=50&&s[j].avg<=100)
{
cout<<j+1<<"\t"<<(s[j].name)<<"\t\t\t"<<s[j].sex<<"\t"<<s[j].scores1<<"\t\t"<<s[j].scores2<<"\t\t"<<s[j].avg<<"\tPass"<<endl;
cout<<"-----------------------------------------------------------------------------------------"<<endl;
}
}
j++;
}
i++;
}
}
else
{
cout<<"Check gender again "<<endl;
goto sch;
}
}
else if(num==2)
{
cout<<"\n2) Please Enter Number of Average for search : ";cin>>av;
cout<<"_________________________________________________________________________________________"<<endl;
cout<<"ID\tName\t\t\tSex\tScore_1\t\tScore_2\t\tAvg\tResult"<<endl;
cout<<"-----------------------------------------------------------------------------------------"<<endl;
int j=0;
while(j<n)
{
if((s[j].avg)==av)
{
if(s[j].avg>=0&&s[j].avg<50)
{
cout<<j+1<<"\t"<<(s[j].name)<<"\t\t\t"<<s[j].sex<<"\t"<<s[j].scores1<<"\t\t"<<s[j].scores2<<"\t\t"<<s[j].avg<<"\tFail"<<endl;
cout<<"-----------------------------------------------------------------------------------------"<<endl;
}
if(s[j].avg>=50&&s[j].avg<=100)
{
cout<<j+1<<"\t"<<(s[j].name)<<"\t\t\t"<<s[j].sex<<"\t"<<s[j].scores1<<"\t\t"<<s[j].scores2<<"\t\t"<<s[j].avg<<"\tPass"<<endl;
cout<<"-----------------------------------------------------------------------------------------"<<endl;
}
}
j++;
}
}
else if(num==3)
{
pf:
cout<<"\n3) Press \"P\"or \"F\" for search data Pass or Fail(P/F) : ";cin>>chpf;
if(chpf[0]=='f'||chpf[0]=='F')
{
cout<<"_________________________________________________________________________________________"<<endl;
cout<<"ID\tName\t\t\tSex\tScore_1\t\tScore_2\t\tAvg\tResult"<<endl;
cout<<"-----------------------------------------------------------------------------------------"<<endl;
int j=0;
while(j<n)
{
if((s[j].avg)>=0&&(s[j].avg)<=50)
{
cout<<j+1<<"\t"<<(s[j].name)<<"\t\t\t"<<s[j].sex<<"\t"<<s[j].scores1<<"\t\t"<<s[j].scores2<<"\t\t"<<s[j].avg<<"\tFail"<<endl;
cout<<"-----------------------------------------------------------------------------------------"<<endl;
}
j++;
}
}
else if(chpf[0]=='p'||chpf[0]=='P')
{
cout<<"_________________________________________________________________________________________"<<endl;
cout<<"ID\tName\t\t\tSex\tScore_1\t\tScore_2\t\tAvg\tResult"<<endl;
cout<<"-----------------------------------------------------------------------------------------"<<endl;
int j=0;
while(j<n)
{
if((s[j].avg)>=50&&(s[j].avg)<=100)
{
cout<<j+1<<"\t"<<(s[j].name)<<"\t\t\t"<<s[j].sex<<"\t"<<s[j].scores1<<"\t\t"<<s[j].scores2<<"\t\t"<<s[j].avg<<"\tPass"<<endl;
cout<<"-----------------------------------------------------------------------------------------"<<endl;
}
j++;
}
}
else
{
cout<<"\nPlease check of you result again "<<endl;
goto pf;
break;
}
}
else
{
goto number;
}
cout<<"\nDo you want to choose number for search again ?(y/n) : ";cin>>ch;
if(ch=='y'||ch=='Y')
{
goto number;
}
}
else
{
cout<<"\nPlease check Number for Enter (1 to many )"<<endl;
break;
}
cout<<"\nDo you want to Creat Data again ?(y/n) : ";cin>>ch;
}
while(ch=='y'||ch=='Y');
return 0;
}
//output