views:

392

answers:

4

Got it working, final code: Pushes back data and sorts both in synchronization

#include "std_lib_facilities.h"

struct Name_pairs
{
       vector<string>names;
       vector<double>ages;
       void quicksort(vector<string>& num, vector<double>& num2, int top, int bottom);
       int divide(vector<string>& array, vector<double>& array2, int top, int bottom);
       bool test();
       string read_names();
       double read_ages();
       void print();
};

string Name_pairs::read_names()
{
       string name;
     cout << "Enter name: ";
     cin >> name;
     names.push_back(name);
     return name;
}

double Name_pairs::read_ages()
{
     double age;
     cout << "Enter corresponding age: ";
     cin >> age;
     ages.push_back(age);
     cout << endl;
     return age;
}

int Name_pairs::divide(vector<string>& array, vector<double>& array2, int top, int bottom)
{
    string x = array[top];
    int i = top-1;
    int j = bottom+1;
    string temp;
    double temp2;
    do{
        do
        {
             j--;
             }while(x<array[j]);

        do
        {
             i++;
             }while(x>array[i]);

        if(i<j)
        {
               temp = array[i];
               temp2 = array2[i];
               array[i] = array[j];
               array2[i] = array2[j];
               array[j] = temp;
               array2[j] = temp2;
               }
               }while(i<j);
        return j;
}


void Name_pairs::quicksort(vector<string>& num, vector<double>& num2, int top, int bottom)
{
     int middle;
     if(top < bottom)
     {
            middle = divide(num, num2, top, bottom);
            quicksort(num, num2, top, middle);
            quicksort(num, num2, middle+1, bottom);
            }
     return;
}

void Name_pairs::print()
{
     for(int i = 0; i < (names.size()-1) && i < (ages.size()-1); ++i)
             cout << names[i] << " , " << ages[i] << endl;
}

int main(){
    Name_pairs np;
    cout << "Enter names and ages. Use 0 to cancel.\n";
    bool finished = false;
    while(!finished){
    finished = "0" == np.read_names();
    finished = 0 == np.read_ages();}
    np.quicksort(np.names, np.ages, 0, (np.names.size()-2));
    np.print();
    keep_window_open();}
A: 

The following lines in main() should be deleted:

np.vector<string>names; // error possibly here?    
np.vector<double>ages;

They already exist as part of instantiating Name_pairs.

Also, remove the parameter names from quicksort since it isn't really necessary and will make your code not function the way you think it should. The names you want to sort are members of Name_pairs. The ones you declared in main() are not the ones you read in with read_names().

EDIT: Actually, it might be easier for you if you declare a single vector with a struct that contains the age and name and sort that array. The entirety of your program implies that you want to sort the names and then print out each name with the associated age, but you only sort the name array.

EDIT2: And from the other answers, I think you need to learn more about how to declare member functions before you can fully solve this problem.

MSN
Then when I call names in np.quicksort, it's undeclared.
trikker
I have to use 2 vectors since the exercise dictates it, plus it's kind of fun implementing a sort function. I am now passing the correct arguments when I call quicksort in main, but I am now getting a runtime error during the sorting process. Going to try and catch an exception and see how it goes.
trikker
+1  A: 

If you're trying to define additional vector variables within main, then the lines

np.vector<string>names; // error possibly here?
np.vector<double>ages;

Should be:

vector<string> names;
vector<double> ages;

You're redefining your struct members, or so it seems. But I don't see where you'd actually use those. Also, size() returns an unsigned variable of some sort, so you may need to cast it, or change the way you're comparing it to prevent compiler errors/warnings.

It's also worth noting, that if you want a couple more vectors declared within main() then it's good practice to name them something unique instead of having them share names with the struct members. Also, your main() doesn't have a return (needs to return int).

John D.
I thought if I did this then that would just be creating new vectors because when I did do this the print output after the quicksort is just the same as if nothing was ever sorted.
trikker
You've already declared two vectors as part of your struct, you don't need to re-declare them. You should be able to simply use them.
John D.
Looking into your other functions, you are using your names variable as though it's a global. Perhaps you meant to implement Name_pairs as a class?
John D.
Not sure what you mean. If you mean changing from a struct to a class, I just used a struct because all of the members ended up having to be public.
trikker
if main doesn't have a return it will automatically return int(0) ;)
csiz
I think that'll depend on whether or not the compiler is standard-compliant. Since he's getting an unspecified error with main() then it's best to cover all bases.
John D.
+1  A: 

Get rid of the following lines in main() which are apparently causing your compiler to crash:

np.vector<string>names; // error possibly here?    
np.vector<double>ages;

Then, a couple of your function definitions need to have their signatures changed:

int divide(vector<string>& array, int top, int bottom)

and

void quicksort(vector<string>& num, int top, int bottom)

need to change to

int Name_pairs::divide(vector<string>& array, int top, int bottom)

void Name_pairs::quicksort(vector<string>& num, int top, int bottom)

so they are seen as part of the Name_pairs struct instead of free functions.

Michael Burr
Ah okay. Hmm. I've edited the code above with the changes, but still receiving same output. I'm still thinking that declaring vector<string>names and vector<double>ages in int main isn't referring back to the vectors in the struct, but I'm probably wrong.
trikker
You are right - you should not be declaring vector<string>names and vector<double>ages in main(). They're members of the Name_pairs struct.
Michael Burr
But for some reason if I don't declare them and I use names in the quicksort argument it says names is undeclared.
trikker
Your quicksort() member function should probably not take an argument for names - it should simply use the names member that's already a member of the struct/class. Alternatively pass in np.names (which it looks like you've done in a recent edit to the code).
Michael Burr
Yeah, having trouble with a range error now. Range error -1. Basically playing guess and check at the moment since I have no idea what could be wrong.
trikker
A: 
csiz
I need to use vectors. I actually considered the first option but I really wanted to implement a sort function to try it out.
trikker