please help me by doing the following program.
I want to feed 80 students name,gender and CGPA.and want to sort in two cases.case 1 sorting with respect to their name
case 2 sorting with respect to their CGPA.
I would stat it like this.
*******************************

#include<iostream.h>
void main()
{
char name[20];
char gender[80];
int CGPA[80];
for(int i=0;i<80;i++)
{
cout<<"enter the name of student:"<<i+1<<endl;
cin>>name;


cout<<"enter the gender of student"<<i+1<<endl;
cin>>gender;


cout<<"enter the CGPA of student"<<i+1<<endl;
cin>>CGPA;
}


}

I want to do it like.

enter name of student 1:
enter gender of student 1:
enter CGPA of student 1;

for each 80 students,but I encountered a problem how can I solve it and sort it?

Edited 3 Years Ago by happygeek: fixed formatting

Two things first. Use <iostream> and not <iostream.h>. Also use int main() and not void main().

What problem did you encounter? It might be best to encapsulate the variables in a structure, and then make an array of 80 of these structures so that the name, gender and CGPA of one student remains with that student, ie so you don't have to rearrange the genders and CGPAs in relation to the name. std::sort might be of use.

Edited 3 Years Ago by Nick Evan: Fixed formatting

Thank you for your reply.I have gotten many idea from you message,but the problem is how can i start with that

As a start:

struct student {
     std::string name;
     char gender;
     int CGPA;
};

and then access the members like this:

int main(void) {
     student myStudent;
     myStudent.name = "Sarah";
     myStudent.gender = 'F';
     myStudent.CGPA = 1; // or whathever
}

Manipulate this to your needs:

#include <algorithm>
#include <iostream>

struct thing {
  int a, b;
};

std::ostream& operator << (std::ostream &o, const thing &th ) {
  o << "a: " << th.a << ", b: " << th.b;
  return o;
}


// Comparison functions
bool comp_thing_a (thing i,thing j) { 
  return (i.a<j.a); 
}
bool comp_thing_b (thing i,thing j) { 
  return (i.b<j.b); 
}


int main( int argc, char *argv[] ) {
  thing i_thing[10];
  int myints[] = {32,71,12,45,26,80,53,33, 26, 11};
  for( int i=0; i<10; i++ ) {
    i_thing[i].a = myints[i];
    i_thing[i].b = myints[9-i];
  }
 
  // Arrange according to i_thing[x].a
  std::sort( i_thing, i_thing+10, comp_thing_a );
  std::copy( 
    i_thing, 
    i_thing+10, 
    std::ostream_iterator<thing>(std::cout, "\n") );
  
  std::cout<< "\n\n";

  // Arrange according to i_thing[x].b
  std::sort( i_thing, i_thing+10, comp_thing_b );
  std::copy( 
    i_thing, 
    i_thing+10, 
    std::ostream_iterator<thing>(std::cout, "\n") );


  return 0;
}

I do not understand it well. what I want is the name,gender and CGPA of eachh student is input by the user.

I know, but I'm not going to give you the complete answer. Modify the code I gave there to work with your stuff. The std::copy function just prints the array to the console. The thing is that you need to define, yourself, the functions which can determine whether one element of the array can be considered bigger/smaller than another.

Well, if you put an using namespace std; in the code somewhere you don't need the std in front of things.

I have tried many times but I do able to solve my problem.
please write me how can i begin with structure and cin.getline
for three data(name,gender and age)
the sorting part is simple I try it by bible sort .

If I understood correctly your question you are asking how to use getline with some member variable of a struct.

Look at this and you should figure out with ease how to finish:

std::string nonMemberString;
nonMemberString = "Some text";
std::cout << std::endl << nonMemberString << std::endl;
std::cout << "Insert some other text" << std::endl;
getline(cin, nonMemberString);

this will store the hard coded string "Some text" into the non member string nonMemberString and print it out, and then will get user input with getline.

On the other hand, we have

struct example {
     std::string memberString
     someType otherStuff;
};

int main(void) {
     example myExample;
     myExample.memberString = "Some text";
     std::cout << std::endl << myExample.memberString << std::endl;
     std::cout << "Insert some other text" << std::endl;
     // ... now it should be evident how to use getline
     return 0;
}

Compare the two and you will sort things out in a blink.

If I understood correctly your question you are asking how to use getline with some member variable of a struct.

Look at this and you should figure out with ease how to finish:

std::string nonMemberString;
nonMemberString = "Some text";
std::cout << std::endl << nonMemberString << std::endl;
std::cout << "Insert some other text" << std::endl;
getline(cin, nonMemberString);

this will store the hard coded string "Some text" into the non member string nonMemberString and print it out, and then will get user input with getline.

On the other hand, we have

struct example {
     std::string memberString
     someType otherStuff;
};

int main(void) {
     example myExample;
     myExample.memberString = "Some text";
     std::cout << std::endl << myExample.memberString << std::endl;
     std::cout << "Insert some other text" << std::endl;
     // ... now it should be evident how to use getline
     return 0;
}

Compare the two and you will sort things out in a blink.

*********************************************
Please help me I have try As i can but I solve some part from your explanation help me so much
and it is here
#include<iostream.h>
struct student{
char name[20];
char gender;
float cgpa;
};
void main()
{
student data[20];
for(int i=0;i<20;i++)
{
cout<<"enter student name"<<i+1<<endl;
cin>>data.name;
cout<<"enter student gender"<<i+1;
cin>>data.gender;
cout<<"enter student cgpa"<<i+1;
cin>>data.cgpa;
}
}
please I will submitt it tomorrow
so I have to oirder it now
thank you

I have been working on it for thee days but I do not come across my broblem
Please help me by writing full program how can I order with respect to students name or CGPA depending on the user interest I try it like this.

#include<iostream.h>
struct student{
char name[20];
char gender;
float cgpa;
};
void main()
{
student data[20];
for(int i=0;i<20;i++)
{
cout<<"enter student name"<<i+1<<endl;
cin>>data.name;
cout<<"enter student gender"<<i+1;
cin>>data.gender;
cout<<"enter student cgpa"<<i+1;
cin>>data.cgpa;
}
}

Edited 3 Years Ago by happygeek: fixed formatting

Comments
learn how to use code-tags

I have try it so many times and write the following program but it is still not working as i want to program.
please add some mising program to it .
**************************

#include<iostream>
#include<iomanip>
struct student{
    char name[20];
    char gender;
    int cgpa;
};
using namespace std;
int main()
{
    student data[4];
    student de;
    for(int i=0;i<3;i++)
    {
        cout<<"enter student "<<i+1<<" name :";
        cin>>data[i].name;
        cout<<"enter student "<<i+1<<" gender :";
        cin>>data[i].gender;
        cout<<"enter student "<<i+1<<" cgpa :";
        cin>>data[i].cgpa;

}
for(int r=1;r<=4;r++)

        for(int j=1;j<=4;j++)
        {
                int  k;
                k=strncmp(data[j].name,data[j+1].name,25);
                if(k>0)
                {
                    int v;
                    v=data[j].cgpa;
                    data[j].cgpa =data[j+1].cgpa;
                    data[j+1].cgpa =v;
                    char c;
                    c=data[j].gender ;
                    data[j].gender =data[j+1].gender ;
                    data[j+1].gender =c;
                    strcpy(de.name,data[j].name);
                    strcpy(data[j].name,data[j+1].name);
                    strcpy(data[j+1].name,de.name);
                }
        }


            for(int  m=1;m<=4;m++)
            {
                cout<<"Name    "<<data[m].name<<endl;
                cout<<"Gender  "<<data[m].gender<<endl;
                cout<<"CGPA    "<<data[m].cgpa<<endl;
            }

            return  0;

}

how can I modify it?

Edited 3 Years Ago by Nick Evan: Fixed formatting

Ok, I'll try to be nice:

1 - Don't (repeat: don't) PM for help. I'm not your friend nor your tutor.

2 - Moreover, don't (repeat: don't) ever ask things like "please write for me the full program. ". I was tempted to write on purpose a program full of errors and then send it to you. There's not a single person that has to do you homework except you.

I hope this is clear enough (either it is or not: read the forum rules, all of them)
Also niek_e's signature is a very valid starting point.

Coming back to your program:

1 - Ask specific question. Don't just throw in a piece of code asking things like "How can you sort" or "How can I modify it".

2 - Try something on your own and post it with explanation of what it does and what you expected it to do instead. Include error messages, if any.

3 - If absolutely clueless on where to start, explain what you have considered and why you have discarded that idea(s)

4 - Last but not least: wrap your code in code tags!

Comments
Now that's what I call a post! - Bravo
Deserves Rep
Excellent advice
This article has been dead for over six months. Start a new discussion instead.