Click here to Skip to main content
16,019,263 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Main.cpp

C++
#include <iostream>
#include "Spinner.h"

using namespace std;

int main() {
   SpinnerSlot* start = NULL;
   SpinnerSlot* current = NULL;

   start = AddSlot(NULL, "A");
   current = AddSlot(start, "A-");
   current = AddSlot(current, "B+");
   current = AddSlot(current, "B");
   current = AddSlot(current, "B-");
   current = AddSlot(current, "C+");
   current = AddSlot(current, "C");
   current = AddSlot(current, "C-");
   current = AddSlot(current, "D+");
   current = AddSlot(current, "D");
   current = AddSlot(current, "D-");
   current = AddSlot(current, "F");

   PrintSpinner(start);

   current = start;
   for (int i = 1; i <= 3; i++) {
      cout << endl << "Spin" << i << ":" << endl;
      current = Spin(current);
   }
   system("PAUSE");
   return (0);
}


Spinner.h

C++
#include <iostream>
#include "Spinner.h"

using namespace std;

int main() {
   SpinnerSlot* start = NULL;
   SpinnerSlot* current = NULL;

   start = AddSlot(NULL, "A");
   current = AddSlot(start, "A-");
   current = AddSlot(current, "B+");
   current = AddSlot(current, "B");
   current = AddSlot(current, "B-");
   current = AddSlot(current, "C+");
   current = AddSlot(current, "C");
   current = AddSlot(current, "C-");
   current = AddSlot(current, "D+");
   current = AddSlot(current, "D");
   current = AddSlot(current, "D-");
   current = AddSlot(current, "F");

   PrintSpinner(start);

   current = start;
   for (int i = 1; i <= 3; i++) {
      cout << endl << "Spin" << i << ":" << endl;
      current = Spin(current);
   }
   system("PAUSE");
   return (0);
}


Spinner.cpp

C++
#include <iostream>
#include "Spinner.h"
using namespace std;

SpinnerSlot* AddSlot(SpinnerSlot* last, string name) {

    SpinnerSlot* newSlot = (SpinnerSlot*)malloc(sizeof(struct SpinnerSlot));

    if (newSlot)

    {

        newSlot->name = name;



    }


    if (last == NULL) {
        newSlot->next = newSlot;
    }

    else {
        newSlot->next = last->next;
        last->next = newSlot;
    }

    return newSlot;
}

SpinnerSlot* Spin(SpinnerSlot* current) {
    srand(time(NULL));

    int spin = (rand() % maxSpin) + 1;

    for (int i = 0; i < spin; i++) {
        current = current->next;
    }

    cout << current->name;

    return current;
}

void PrintSpinner(SpinnerSlot* start) {
    SpinnerSlot* current = start;
    cout << current->name << " ";
    current = current->next;

    while (current != start) {
        cout << current->name << " ";

        current = current->next;
    }
    cout << "\n";
}


What I have tried:

ive tried many ways to have the code find information for "newSlot" pointer.
Posted
Updated 8-Dec-21 19:06pm
v3
Comments
Rick York 8-Dec-21 16:12pm    
You have not included the code for Spinner.h. The code for main.cpp was included twice.

1 solution

I'll take a guess because your "question" is rather vague. This appears to be an attempt at making a singly-linked list. They way you are doing is unusual and I don't understand how it could work correctly. Here is your function tweaked a bit to act more like a standard linked list.
C++
SpinnerSlot* AddSlot( SpinnerSlot* last, string name )
{
    // calloc zeros the newly allocated block of memory

    SpinnerSlot* newSlot = (SpinnerSlot*)calloc( sizeof( struct SpinnerSlot ), 1 );
    if( newSlot )
    {
        newSlot->name = name;
        newSlot->next = nullptr;  // new one is the end of the list so it has no next
    }

    if( last )
    {
        last->next = newSlot;     // the new one is now the old end's next
    }

    return newSlot;
}
Typically, the end of the list is the newest one added so it will have a null next pointer and the previous end of the list will now have the newest one as its next. This way should still work with your Print and Spin functions.

One other thing - typically when one uses a PRNG it is seeded only once at the start of the program. That is the call to srand. It should be called just once as one of the first things done in main.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900