0

I wrote this, and its crap. I've been writing code for all of a week now, so please be polite. The following code was written solely because I'm trying become as familiar with the language itself before I branch out into learning the fancier features.

My question is a broad one.

In the function "sort_entries()" I use a bubblesort to shuffle the object references around in an array, based on a value pulled from the objects themselves.

However, I'm wondering if there is an easier way to do this?

In fact, i'm also wondering if there is a simpler way to keep track of objects instead of referencing them by an array of pointers, because frankly I don't like passing around pointers.

#include <iostream>
#include <stdio.h>
#include <new>
#include <cstring>
using namespace std;

using namespace std;

class Person {
    int person_num;
    int waffles_eaten;

public:

    Person(int p_num, int waffles)
    {
        person_num = p_num;
        waffles_eaten=waffles;

        if(person_num == p_num && waffles_eaten == waffles)
        {
            printf("Entry #%i created. Waffles eaten = %i\n",    person_num, waffles_eaten);
        }
    }
    int get_num()
    {
        return person_num;
    }

    int get_eaten()
    {
        return waffles_eaten;
    }

};

/* ****************************** */

int num_entries()
{
    cout<<"How many entries to create?: ";
    int count;
    cin>>count;
    return count;
}

void create_entry(Person **registry, int count)
{
    int eaten;
    for(int i =0; i<count; i++)
    {
        printf("Person #%i: Waffles eaten? \n", i);
        cin>>eaten;
        registry[i] = new Person(i, eaten);
    }

}

void view_entries(Person **registry, int count)
{
    for(int i=0; i<count; i++)
    {
        printf("Person #%i ate %i waffles\n", (registry[i])->get_num(), (registry[i])->get_eaten() );
    }

}

void delete_entries(Person **registry, int count)
{
    for(int i=0; i<count; i++)
    {
        delete [] registry[i];
    }
}

void cpy_reg(Person **registry, Person **sorted_reg, int count)
{
    for(int i=0; i<count; i++)
    {
        sorted_reg[i] = registry[i];
    }
}

void display_data(Person **sorted_reg, count int)
{

}


void sort_entries(Person **registry, Person **sorted_reg, int count) // Need to figure why this actually works
{
    cpy_reg(registry, sorted_reg, count); // copy the contents of registry[] to sorted_reg[]

    cout<<"Sorted List"<<endl;
    cout<<"------------"<<endl;

    /* does magical sorting stuff */

    int i, j;
    Person *temp;

    for(i=0; i<count; i++)
    {
        for(j=i+1; j<count; j++)cl
        {
            if( ((sorted_reg[i])->get_eaten() ) > ((sorted_reg[j])->get_eaten()) ) 
            {
                temp = *(sorted_reg + j);
                *(sorted_reg+j) = *(sorted_reg+i);
                *(sorted_reg+i) = temp;
            }
        }
    }
}

void print_values(Person **reg, int count)
{
    for(int i=0; i<count; i++)
    {
        printf("Entry #%i, --> %i waffles\n", i, (reg[i])->get_eaten() ); 
    }
}

bool ask_to_sort()
{
    string in;
    bool out;
    cout<<"Sort entries, and see data about them? (Y/N)"<<endl;
    cin>>in;
    if( in=="y" || in =="Y")
    {
        return true;
    }else if(in=="n"||in=="N")
    {
        cout<<"Exiting"<<endl;
    }
    else {
        cout<<"Enter Y/N"<<endl;
    }
}

/* **************************** */  

int main()
{
    int count = num_entries();
    Person *registry[count];
    Person *sorted_reg[count];
    create_entry(registry, count);
    view_entries(registry, count);

    if(ask_to_sort())
    {
        sort_entries( registry, sorted_reg, count);
    }

    print_values(sorted_reg, count);

    delete_entries(registry, count);

    return 0;
}
6
  • 1
    Person *registry[count]; is not allowed in C++. This illustrates well why you should learn from learning resources rather than trial-and-error. Commented Dec 18, 2015 at 2:02
  • @M.M What did you mean by the array definition is not allowed in C++? Commented Dec 18, 2015 at 2:10
  • @MehrdadMomeny which part didn't you understand? Commented Dec 18, 2015 at 2:16
  • I mean what's wrong with Person *registry[count]; ? You said it's not allowed. Commented Dec 18, 2015 at 2:24
  • @MehrdadMomeny unlike C, C++ doesn't have such a variable length array. Actually, variable length array was introduced in C99, but it was changed to an optional in C11 (not C++11) Commented Dec 18, 2015 at 3:37

1 Answer 1

1

As others mentioned, it isn't valid to create variable length array in C++. There's std::vector for these times, and you should use that.

So instead of this:

Person *registry[count];

you should do this:

std::vector<Person> registry(count);

And there's no need to work with pointers when you can use Person instances, and it makes the code easier to understand and maintain.

Thus to swap two items in your sort function, instead of this:

temp = *(sorted_reg + j);
*(sorted_reg+j) = *(sorted_reg+i);
*(sorted_reg+i) = temp;

Do this:

swap(sorted_reg[i], sorted_reg[j])

To just swap values.

After all, to sort your vector, you can rely on STL's sort function. You'd need to define a less-than operator (operator<()) for your Person structure though.

Sign up to request clarification or add additional context in comments.

6 Comments

C++ doesn't allow variable length array. Hence, Person registry[count]; is incorrect
I remember this claim, but then both GCC and MSVC2012 , which I have access to, accept this.
"GCC and MSVC2012 supports it" doesn't mean C++ allow it. GCC support it as an extension. See gcc.gnu.org/onlinedocs/gcc/Variable-Length.html Variable-length automatic arrays are allowed in ISO C99, and as an extension GCC accepts them in C90 mode and in C++
When you use g++ to compile it with flag -pedantic those code won't be able to compiled. See gcc.gnu.org/onlinedocs/gcc/Warning-Options.html for -pedantic option
so a vector is just an extendable array?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.