Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Purpose: This lab introduces the use of pointers to structs.
Problem:
You are to implement the spinner example presented in recitation.
Support:
You are provided an example Main.cpp, though you are required to create your own spinner.cpp and Spinner.h files.
Approach:
Step 1: Implement your spinner code. Populate Spinner.h and implement the functions in
Spinner.cpp.
Step 2: Test your implementation with the provided Main.cpp.
Step 3: Create your own spinner by rewriting main().

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);
}


What I have tried:

Ive tried to write the code myself not fully understanding what to do.
Posted
Updated 9-Dec-21 22:22pm
v2
Comments
CHill60 8-Dec-21 12:19pm    
where is the information about your spinner.h and spinner.cpp. If you do not understand your course work fully you should really speak to your tutor about it

Quote:
you are required to create your own spinner.cpp and Spinner.h files.

That is where you start.
For the rest, read the question carefully and compare that with your last couple of lessons.
We aren't here to do your homework for you, and we couldn't if we wanted to; we have no access the important information:
Quote:
You are to implement the spinner example presented in recitation.
We have no idea what that covers!

So read the question, work out what is required, and if necessary get the question clarified by your teacher.
 
Share this answer
 
1. Check your notes or whatever information you have on that Spinner class. Apparently you should have this information. We don't.

2. Check the main program: you need to implement, at the very least, a struct or class SpinnerSlot, and three functions, like this:
C++
struct SpinnerSlot {
    // contents according to item 1 above
};
SpinnerSlot* AddSlot(const SpinnerSplot* someSpinner, const char* someString) {
    // do whatever you are supposed to do according to item 1
    // then return a pointer to some SpinnerSlot
}
void PrintSpinner(SpinnerSlot* someSpinner) {
    // probably print whatever you put together here
}
void Spin(const SpinnerSlot* someSpinner) {
    // do whatever "Spin" means
}
You should rename the function arguments to something that better reflects what these objects are or represent, depending on the information you have.

What goes into this struct or into these functions we cannot know: you have the information, and you are supposed to put it into code.

3. Bonus: Clean up after yourself! Since you are dealing with pointers to structs, you probably create such objects on the heap at some point (probably in AddSlot). But these objects are never released! They should be freed again before the program ends. So you could write another function to do this and then call it at the end of main():
C++
SpinnerSlot* Cleanup(const SpinnerSlot* firstSpinner) {
    // release all spinners
    return nullptr; // hint: do not use NULL - always use nullptr!
}
// change end of main():
int main() {
    // ...
    start = Cleanup(start);
    return 0;
}

Note how the call to Cleanup is used to initialize start back to nullptr. While not strictly needed here, it is a good practice to make sure that you clear a pointer once the object it points to is released.

On a sidenote, you should not use NULL in C++ code! NULL is an outdated symbol that was used to initialize anything in a C program, but in C++ it has been replaced by nullptr for pointers, or simply 0 for numbers. It is important to make this distinction in modern C++ code and you better get used to it earlier rather than later.
 
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