943,866 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1757
  • C++ RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Nov 23rd, 2008
0

Re: sorting array with respect to name and CGPA

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 .
Reputation Points: -1
Solved Threads: 0
Newbie Poster
sisse56 is offline Offline
17 posts
since Nov 2008
Nov 23rd, 2008
0

Re: sorting array with respect to name and CGPA

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)
  1. std::string nonMemberString;
  2. nonMemberString = "Some text";
  3. std::cout << std::endl << nonMemberString << std::endl;
  4. std::cout << "Insert some other text" << std::endl;
  5. 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)
  1. struct example {
  2. std::string memberString
  3. someType otherStuff;
  4. };
  5.  
  6. int main(void) {
  7. example myExample;
  8. myExample.memberString = "Some text";
  9. std::cout << std::endl << myExample.memberString << std::endl;
  10. std::cout << "Insert some other text" << std::endl;
  11. // ... now it should be evident how to use getline
  12. return 0;
  13. }

Compare the two and you will sort things out in a blink.
Reputation Points: 134
Solved Threads: 18
Junior Poster
mrboolf is offline Offline
182 posts
since Jun 2008
Nov 24th, 2008
0

Re: sorting array with respect to name and CGPA

Click to Expand / Collapse  Quote originally posted by mrboolf ...
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)
  1. std::string nonMemberString;
  2. nonMemberString = "Some text";
  3. std::cout << std::endl << nonMemberString << std::endl;
  4. std::cout << "Insert some other text" << std::endl;
  5. 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)
  1. struct example {
  2. std::string memberString
  3. someType otherStuff;
  4. };
  5.  
  6. int main(void) {
  7. example myExample;
  8. myExample.memberString = "Some text";
  9. std::cout << std::endl << myExample.memberString << std::endl;
  10. std::cout << "Insert some other text" << std::endl;
  11. // ... now it should be evident how to use getline
  12. return 0;
  13. }

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
Reputation Points: -1
Solved Threads: 0
Newbie Poster
sisse56 is offline Offline
17 posts
since Nov 2008
Nov 24th, 2008
-1

Re: sorting array with respect to name and CGPA

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;
}
}
Reputation Points: -1
Solved Threads: 0
Newbie Poster
sisse56 is offline Offline
17 posts
since Nov 2008
Nov 24th, 2008
0

Re: sorting array with respect to name and CGPA

Maybe this can help..
c++ Syntax (Toggle Plain Text)
  1. // ..
  2. cin.getline(data[i].name,30);
  3. // ...
Last edited by cikara21; Nov 24th, 2008 at 1:15 pm.
Reputation Points: 47
Solved Threads: 69
Posting Whiz
cikara21 is offline Offline
340 posts
since Jul 2008
Nov 25th, 2008
0

Re: sorting array with respect to name and CGPA

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?
Reputation Points: -1
Solved Threads: 0
Newbie Poster
sisse56 is offline Offline
17 posts
since Nov 2008
Nov 25th, 2008
0

Re: sorting array with respect to name and CGPA

Help how can you sort
Reputation Points: -1
Solved Threads: 0
Newbie Poster
sisse56 is offline Offline
17 posts
since Nov 2008
Nov 25th, 2008
0

Re: sorting array with respect to name and CGPA

Click to Expand / Collapse  Quote originally posted by sisse56 ...
Help how can you sort
The art of sorting
Moderator
Featured Poster
Reputation Points: 4142
Solved Threads: 394
Industrious Poster
Nick Evan is offline Offline
4,132 posts
since Oct 2006
Nov 25th, 2008
3

Re: sorting array with respect to name and CGPA

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!
Last edited by mrboolf; Nov 25th, 2008 at 7:47 am.
Reputation Points: 134
Solved Threads: 18
Junior Poster
mrboolf is offline Offline
182 posts
since Jun 2008
Nov 25th, 2008
0

Re: sorting array with respect to name and CGPA

I have solved it thank you
Reputation Points: -1
Solved Threads: 0
Newbie Poster
sisse56 is offline Offline
17 posts
since Nov 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Strange errors in program
Next Thread in C++ Forum Timeline: Overloading ++ --





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC