okay, so I'm a little lost on how to use selection sort and i/o files for this project. the program is supposed to sort a list of first and last names into alphabetical order. here's the catch. each name has a GPA that goes with it. so not only do you sort the name, you also sort the GPA, then after that, you sort from highest to lowest GPA with the corresponding name. you read from a file to get name and GPA info.

example:

data.txt:
Jim Johnson 3.50 
Mike Smith 3.99 
Cynthia Jones 3.25 
Lake Martin 3.75 
Olga Gavrylyako 3.00 
Jim Brown 4.00

not much luck so far. this is all I've figured out and it doesn't even compile. I was trying to sort the names alphabetically first then I was going to work on the code for GPA.

#include <iostream>
#include <string>
#include <fstream>
using namespace std;

int main(){
 int length=20;
 string temp, name[20];
	ifstream students.open("data.txt");
	while(students>>temp){
	for (int a=1; a<length; a++)
    temp=name[a];
    for (int k=a-1; k>=0 && name[k]>temp; k--){
      name[k+1] = name[k];
	name[k+1]=temp;
  for(int a=0;a<length;a++) cout<<name[a]<<" ";
  cout<<endl;
	}
	students.close();
  return 0;
}

Why doesn't it compile? Don't you have a compiler? If you do, did you get errors? If you did, are we supposed to guess what they are?

#include <iostream>
#include <string>
#include <fstream>
using namespace std;

int main()
{
    int length=20;
    string temp, name[20];
    ifstream students.open("data.txt");
    while(students>>temp)
    {
        for (int a=1; a<length; a++)
            temp=name[a];
        for (int k=a-1; k>=0 && name[k]>temp; k--)
        {
            name[k+1] = name[k];
            name[k+1]=temp;
            for(int a=0;a<length;a++) cout<<name[a]<<" ";
            cout<<endl;
	 }
	 students.close();
         return 0;
}

I reformatted your code, so you can easily see that you have mismatching parantheses. If you have corrected this, repost here and we'll have a look at the remaining errors (and there are many).
And please read http://www.daniweb.com/forums/thread78223.html before posting!!!

This is what I always say: Use the standard library for the standard jobs.
If I assume you are familiar with std::vectors, std::maps, std::sort, there is no reason not to use them.
1.Create a std::map of <string,double>
2.Transverse the file and store the GPA of the particular name in the std::map and also store the list of name in a vector of string
3.std::sort the std::vector of names
4.Output the std::map according in the order of the vector by statement like

for(std::vector<std::string>::iterator i=name_vector.begin() ; 
i != name_vector.end() ; ++i)
std::cout<<the_map_used[*i] <<std::enl;

It would result more efficient and small code.
If you don't know how to use STL, better learn it now. You will have to learn it sooner or later.

This is what I always say: Use the standard library for the standard jobs.
If I assume you are familiar with std::vectors, std::maps, std::sort, there is no reason not to use them.

And this is what I say -- if you look at the code posted it quite obvious that std::vectors, std::maps, std::sort are not in their toolkit yet. And using these things nullify the learning process on how these things work -- the instructor is obviously trying to teach them to understand techniques. When you get into another language, all the STL knowledge is totally useless. You need to actually know things.

>>And using these things nullify the learning process on how these things work -- the instructor is obviously trying to teach them to understand techniques

Well, I never thought it is a homework. The OP never mentioned. He told it is some kind of his project.
And besides, considering that it is a project, rather than a HW, I think STL is just the tool one should be equipped with.
I see a lot of (so-called-)c++ programmers not using standard library and making their program just less efficient.

Also, if they are having a course on C++, the instructor is expected to teach the language rather than programming. These are off course two different things.

Well, I never thought it is a homework. The OP never mentioned. He told it is some kind of his project.

I've been here long enough to tell.

Also, if they are having a course on C++, the instructor is expected to teach the language rather than programming. These are off course two different things.

Not in my world. Instructors teach programming and use the language as a tool. At least they did in my day. Maybe they don't today -- which is why there are so few real programmers today, eh? Just coders who can't figure out why things work... :icon_wink:

Not in my world. Instructors teach programming and use the language as a tool. At least they did in my day. Maybe they don't today -- which is why there are so few real programmers today, eh? Just coders who can't figure out why things work..

Yo champ! I am in your city this time. There are hardly any good programmers right here in this world.
That's why I liked the TAOCP. Knuth uses a assembly language, thats awesome considering that he is not bloating us with the 'features' of the high level programming languages not taking side of any particular language.
I rather conclude else making this thread dirty. This is my last post on this thread regarding. Any further criticism is addressed to my Inbox
This is a C++ forum ( seems to me atleast). So I have full right to suggest and promote the use of STL for the 'project doers'. Had he told me that it is a HW, I would myself encouraged him to learn from it.
Of course you've(waltP) passed a good amount of your life here on this forum ( considering the BIG number of posts you have) but really I don't consider. What I consider is that you post good. You do good. And are sensible. (luckily you qualifies) so Hats of to you.
Ffor me, every thread is a isolated entity.:)

I really get annoyed when the OP is not there to care care of his thread.
THE END

okay, so my algorithm was alll out of wack so I think I've got something going here, although I'm completely lost on selection sort still. I tried writing the function to sort GPA and I'm working on sorting names, with no luck so far =\

//Tabitha Holcombe
//11149035
//Project 7/8
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
void selectionSort1(string[],int size);
void selectionSort2(double[],int size);
int main(){
 string firstName[20], lastName[20];
 double gpa[20];
 int a=0;
 for(a;a<20;a++){
	ifstream students;
	students.open("data.txt"); 
		while (!students.eof()){
		students>>firstName[a]>>lastName[a]>>gpa[a];}
 }
		int choice;
		cout<<"Menu:\t"<<endl;
		cout<<"1:Print alphabetically by last name"<<endl;
		cout<<"2:Print GPA from smallest to highest"<<endl;
		cout<<"3:quit"<<endl;
		cin>>choice;
		 while (choice!=3){
			 if (choice==1){
				 selectionSort1(lastName,20);
				 for(int h=0;h<20;h++){
					 cout<<lastName[h]<<","<<firstName[h]<<gpa[h]<<" ";}}
			 else{//(choice==2)
				 selectionSort2(gpa,20);
				 for(int h=0;h<20;h++){
					 cout<<lastName[h]<<","<<firstName[h]<<gpa[h]<<" ";}}
 cin>>choice;}
 cout<<"quit"<<endl;
 students.close();
  return 0; 
}// end main() 
//Selection Sort function 
void selectionSort2(double array[],int size){ 
int min; 
int b; 

//This loop goes through the whole array 
for(int a=0;a<size-1;a++) 
{ 
  b=a; 
  min=array[b]; //Get the current value 

  //...and check if any of the rest numbers is lower 
  for(int j=a+1;j<size;j++) 
  { 
   if(array[j]<min) 
   { 
    b=j; min=array[b]; //...and if yes, then get it 
   } 
  } 

  //Switch the values... 
  array[b]=array[a]; 
  array[a]=min; 

} 
} 
void selectionSort1(string s, int size){
}

and it's sort of homework, I guess? I'm VERY new to C++, I've only been doing it for about 5 weeks or more so STL isn't something I know anything about.

as far as C++ goes, I understand how it works, I just don't understand how to use it, which makes it more frustrating.

>>and it's sort of homework, I guess?
Are you asking me that? You told me it was a project.
>>I'm VERY new to C++, I've only been doing it for about 5 weeks or more so STL isn't something I know anything about.
Exactly THIS is the problem of the (so called)C++ programmer and the (so called) instructors, they tend to think that STL should be taught the the end of the course as it is a BIG thing. The result is that the students practice a lot of non-STL codes and they thus do not use STL in their future projects.
I am not blaming it on you, but nearly everyone.
I really liked the teaching methodology of Andrew Koenig and Barbara E. Moo (author: Accelerated C++
They have, from the beginning, made students comfortable with the use of Standard library, and then in the later chapters they teach actually how these things work.


Now regarding the problem:
You have just made selectionSort2 maybe correctly but it is unlikely to work. Why? Because it just sorts the GPA array on the basis of ascending GPA but you didn't sort the corresponding First Name and Last name!!
Consider the data you posted in the first post. Your current program will do something like this:
Johnson Jim 3.00
Smith Mike3.25
Jones Cynthia 3.50
Martin Lake 3.75
Gavrylyako Olga 3.99
Brown Jim 4.00
Hence only GPA are in sorted order, but have lost the correspondence to the respective name.

The solution is, that while swapping the elements of GPA while doing selection sort, you should swap the corresponding elements of lastname and firstname array too. This will change the array lastname and firstname correspondingly.
To do this, in your selectionSort2, you not only have to keep the track of min but also the index it is pointing to.

The selectionSort1 can be written as same way as selectionSort2 as even in strings, you can have <,>,== operators working.

The syntax of if-else-if statement is:

if (condition1)
{
    //statements
}
else if (condition2)
{
    //statements
}
else if (condition3)
{
    //statements
}
else
{
    //statements
}
This article has been dead for over six months. Start a new discussion instead.