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;
}
Person *registry[count];is not allowed in C++. This illustrates well why you should learn from learning resources rather than trial-and-error.Person *registry[count];? You said it's not allowed.variable length array. Actually, variable length array was introduced in C99, but it was changed to an optional in C11 (not C++11)