0

I'm not sure my linked list code is working effectively and going to the next node (ticket). It seems to run fine for one ticket, but does not seem to either go to the next instance (ticket) when instantiating a new object, or doesn't sort again. I would appreciate if someone could take a quick look to see what could be wrong.

For example, if I test run this:

int​ ​main​() {
       SuperDraw sd(2);
 }

The output should be something like:

 2 new tickets were successfully generated.
The numbers are: 12, 14, 23, 39, 40, 44 and 1, 2, 9, 12, 28, 41

But it gives me one ordered and one non-ordered number. I'm not yet at the point where I verifySequence but the idea is that when you give an int array, the method will check if that same sequence is found in the existing linked list. If anyone would give me suggestions, that would be great. Thanks!

#include <iostream>
#include <sstream>
#include <string>
#include <algorithm> //stdin

using namespace std;

//each ticket has 6 random numbers in numbers[] and points to next element
struct ticket{
    unsigned int numbers[6];
    ticket* next;
};

class SuperDraw{

public:
    SuperDraw();
    SuperDraw(int args);
    ~SuperDraw();
SuperDraw(const SuperDraw &obj);
    void newTicket(int verbose=0);
    void printAllTicketNumbers();
    bool verifySequence(int number[]);
    void deleteSequence(int number[]);

private:
    ticket* ticketListHead;
    ticket* ticketListTail;
};


void 
SuperDraw::printAllTicketNumbers(){
    int count=0;
    ticket* current = new ticket;
    while(current){
        count++;
        current=current->next;
    }
    cout<<"We found "<<count<<" generated tickets:\n";

    while (current){
        for(int i=0; i<6; i++){
            cout<<current->numbers[i]<<", ";
        }
        cout<<endl;
        current=current->next;
    }

}

bool
SuperDraw::verifySequence(int number[]){
    //check if number[i]==number in linked list
    return false;
}

void 
SuperDraw::deleteSequence(int number[]){
    if (verifySequence(number)){

    }

}

//generates 6 random numbers and adds newTicket to linked list ticket
//if verbose==0, don't print anything. If 1, then print
 void
 SuperDraw::newTicket(int verbose){

 srand(time(NULL));
 ticket* t = new ticket;
 //if (t != NULL){
    //generate 6 random numbers between 1 to 49 and set to t->numbers
    for (int i =0; i<6; i++){
        t->numbers[i]=rand()%49+1;
    }

    //sort array in ascending order
    for (int i =0; i<6; i++){
        for (int j=i+1; j<6; j++){
            //swap temp=a; a=b; b=temp;
            if (t->numbers[i]>t->numbers[j]){ 
                int temp=t->numbers[i];
                t->numbers[i]=t->numbers[j];
                t->numbers[j]=temp;
            }
        }
    }


//print numbers if verbose is 1
 cout<<"A new ticket was successfully generated. The numbers are: ";

if (verbose==1){
    //string num="";
    for (int i=0; i<6; i++){
        cout<<t->numbers[i]<<" ";
    }
    cout<<endl;
}

t->next=NULL; 

if (ticketListHead==NULL){
    ticketListHead=t;
    ticketListTail=t;
    t=NULL; //avoid memory leak
    cout<<"ticketList==NULL\n";
}
else
{
    ticketListTail->next=t;
    ticketListTail=ticketListTail->next;
    ticketListTail->next=NULL;
    t=NULL;
}   
 }

//Constructor initializes the attributes to NULL
SuperDraw::SuperDraw(){
        //ticket* ticket = NULL;
        ticketListHead=NULL;
        ticketListTail=NULL;
 }

 //Question 3: Constructor taking in number of tickets
 SuperDraw::SuperDraw(int args){
     //if args>1, then create newTicket
     if (args==1){
        cout<<"A new ticket was successfully generated. The numbers are: ";
        newTicket();

     } else {
        cout<<args<<" new tickets were successfully generated. The numbers are: ";
       for (int i=0; i<args; i++){
           newTicket();
           if (i==args-2)
            cout<<" and ";
       }
       cout<<endl;
     }
 }

//Destructor method: frees all allocated tickets
 SuperDraw::~SuperDraw(){
     ticket* current = ticketListHead;

     while(current){
         ticket* next = current->next;
         delete current;
         current=next;
     }
 }

 //Copy constructor that copies SuperDraw object into another object
 SuperDraw::SuperDraw(const SuperDraw &obj) {
        // ticket* t = new ticket;
        // *t = *;
        ticket* head = new ticket;
        *head = *(obj.ticketListHead);

        ticket* tail = new ticket;
        *tail = *(obj.ticketListHead);
        ticket *ptr = obj.ticketListHead;
        ticket *newTicket;

 }

 /* Program that generates lotto numbers and generates
  * 6 random numbers between 1-49 */

int
main(){
        SuperDraw sd;
        sd.newTicket(1); 

        SuperDraw se(2);

        SuperDraw sy;
        sy.newTicket();
        sy.newTicket();
        sy.newTicket();

        sy.printAllTicketNumbers();

        SuperDraw sv;
        sv.newTicket();
        //as many sd.newTickets as you would like
        int myNumbers[6]={2,4,17,29,31,34};
        sv.verifySequence(myNumbers);
        sv.deleteSequence(myNumbers);

        SuperDraw sr;
        sr.newTicket();
        SuperDraw sr1(sr);
        sr1.printAllTicketNumbers();

}
mcoder
  • 83
  • 1
  • 8
  • 1
    The absolute best way I've ever seen to grok the linked list is to draw pictures. Draw the list. Draw each step as you perform an operation on it. Make sure at each step that the list is still consistent (For example, you haven't lost something, like a `next` pointer, you'll need back later). Use the drawings as the basis for your code. When you find yourself debugging, draw the list. Follow your coded instructions exactly to draw each step. If the new and old drawings don't match You found a bug and you'll probably have a good idea of what you need to do instead. – user4581301 Mar 12 '19 at 01:46
  • sorry, I just fixed that yep. How would we copy the object using this copy constructor, using memcpy or pointers? – mcoder Mar 12 '19 at 01:50
  • Please don't use `memcpy`. – paddy Mar 12 '19 at 02:00
  • `memcpy` should be avoided in C++. It does not deal well with objects that are not trivially copyable. As soon as you need a custom copy constructors, `memcpy` goes right out the window. Don't even worry about the copy constructor right now. You have bugs in the code much, much earlier than that. Fix the existing bugs before adding more. – user4581301 Mar 12 '19 at 02:07
  • I just edited it, but I know I need to access the list size, just don't know how – mcoder Mar 12 '19 at 02:08
  • You don't need the list size. You start at the beginning of the `obj` list (recommend renaming this variable. `obj` contains no information about it's purpose and use. `source` would be better.) For each `ticket`, copy the ticket and put it on the end of the list. When you hit the null pointer at the end of the source list, you stop. – user4581301 Mar 12 '19 at 02:11
  • [Read the documentation for `srand();`](https://en.cppreference.com/w/cpp/numeric/random/srand). You'll find that you almost never want to call it more than once in a program, and when you do find that odd case where you do, `rand` is almost certainly not the random number generator [you should be using](https://en.cppreference.com/w/cpp/numeric/random). Place this one call to `srand` somewhere near the start of `main`. [Better explanation here.](https://stackoverflow.com/questions/7343833/srand-why-call-it-only-once) – user4581301 Mar 12 '19 at 02:19
  • Ok thanks @user4581301. I'll take a look. – mcoder Mar 12 '19 at 02:22
  • `t = NULL; //avoid memory leak` won't prevent a memory leak. It won't really do much of anything and the compiler will probably remove it. What compiler are you using, by the way? There may be some helpful warning diagnostics you can turn on. For example, some of your constructors use uninitialized members and with a high enough warning level the compiler should tell you which ones. – user4581301 Mar 12 '19 at 02:27
  • Suggestion: up in `struct ticket`, change `ticket* next;` to `ticket* next = NULL;`. If your compiler is reasonably up to date and allows this bit of syntax you won't have to worry about having to drop stuff like `t->next = NULL;` all over the place. If you forget to do one, the program will break, so you might as well make it impossible to forget. – user4581301 Mar 12 '19 at 02:32
  • I'm using the g++ compiler (GNU) on vs code. Alright, I'll keep note of that, that struct was provided in the instructions prior, that's why. I think my SuperDraw(int args) method is complaining as it's mentioning the points aren't freed – mcoder Mar 12 '19 at 02:35
  • When building with g++ consider adding `-pedantic -Wall -Wextra` to the command line. If the compiler can catch mistakes for you, let it. Also consider adding `-Weffc++`. The stuff it finds is more suggestions than out-right rule-breaking you need to fix and some of the advice is a little dated, but the information can be really helpful while learning to program. In your case adding `-Weffc++` will point out that `SuperDraw::SuperDraw(int args)` didn't initialize `ticketListHead` and `ticketListTail`. It'll also complain that `SuperDraw::SuperDraw()` could have set them better. – user4581301 Mar 12 '19 at 02:41
  • The console says " can't link with a main executable file " for architecture x86_64 – mcoder Mar 12 '19 at 02:47
  • Let me see the build command you're using. Another handy tool is GDB. It allows you to run a program on your terms, right down to executing the program instruction by instruction so you can see what's really going on. Some versions of GDB allow you to run the program backward so you can let the program crash and then step backward to find out why. – user4581301 Mar 12 '19 at 02:50
  • g++ -o -pedantic -Wall -Wextra A2 A2.cpp – mcoder Mar 12 '19 at 02:54
  • I don't have too much time at the moment as there is about an hour left .. I'll try to debug and see where my errors are.. – mcoder Mar 12 '19 at 02:55
  • The `-o` needs to be with the name of the output, `A2`. Try `g++ -o A2 -pedantic -Wall -Wextra A2.cpp` – user4581301 Mar 12 '19 at 03:15
  • Alright, thanks. In the end, I've figured it out with some help – mcoder Mar 12 '19 at 04:11
  • Thought I forgot to reply, but thanks so much again! much appreciated the help, @user4581301! – mcoder Mar 14 '19 at 19:55

0 Answers0