| | |
sorting array with respect to name and CGPA
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jun 2008
Posts: 182
Reputation:
Solved Threads: 18
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:
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
Compare the two and you will sort things out in a blink.
Look at this and you should figure out with ease how to finish:
C++ Syntax (Toggle Plain Text)
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
C++ Syntax (Toggle Plain Text)
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.
•
•
Join Date: Nov 2008
Posts: 12
Reputation:
Solved Threads: 0
•
•
•
•
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:
C++ Syntax (Toggle Plain Text)
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
C++ Syntax (Toggle Plain Text)
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[i].name;
cout<<"enter student gender"<<i+1;
cin>>data[i].gender;
cout<<"enter student cgpa"<<i+1;
cin>>data[i].cgpa;
}
}
please I will submitt it tomorrow
so I have to oirder it now
thank you
•
•
Join Date: Nov 2008
Posts: 12
Reputation:
Solved Threads: 0
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[i].name;
cout<<"enter student gender"<<i+1;
cin>>data[i].gender;
cout<<"enter student cgpa"<<i+1;
cin>>data[i].cgpa;
}
}
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[i].name;
cout<<"enter student gender"<<i+1;
cin>>data[i].gender;
cout<<"enter student cgpa"<<i+1;
cin>>data[i].cgpa;
}
}
Maybe this can help..
c++ Syntax (Toggle Plain Text)
// .. cin.getline(data[i].name,30); // ...
Last edited by cikara21; Nov 24th, 2008 at 1:15 pm.
•
•
Join Date: Nov 2008
Posts: 12
Reputation:
Solved Threads: 0
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?
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?
•
•
Join Date: Jun 2008
Posts: 182
Reputation:
Solved Threads: 18
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!
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!
Last edited by mrboolf; Nov 25th, 2008 at 7:47 am.
![]() |
Other Threads in the C++ Forum
- Previous Thread: Strange errors in program
- Next Thread: Overloading ++ --
| Thread Tools | Search this Thread |
api array arrays beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamic dynamiccharacterarray email encryption error file forms fstream function functions game getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






