my Text-Adventure Problem with class D20 and it doesn't want to seem to work at all causing the whole start up not to work

71 Views Asked by At

I have been having trouble with calling upon my class/constructor in C++. And I can't exactly figure out why, [my game title.cpp]

#include <iostream>
#include <vector>
#include <cstdlib>
#include "Header1.h"
#include <string>
using namespace std;

int main() {
    string playerName;

    vector<string> options = { "Frodo", "Bilbo", "Wongle", "Aragorn", "-TYPE YOUR NAME-" };

    cout << "Welcome, my friend!" << endl;

    for (int i = 0; i < options.size(); ++i) {
        cout << static_cast<char>('A' + i) << " - " << options[i] << endl;
    }

    char playerAnswer;
    cin >> playerAnswer;

    int choiceIndex = toupper(playerAnswer) - 'A';

    if (choiceIndex <= 3 && choiceIndex < options.size() - 1) {
        playerName = options[choiceIndex];
    }
    else {
        cout << "You haven't picked any of the default options. What name would you like instead?" << endl;
        cout << "Enter your custom name below:" << endl << "> ";
        cin >> playerName;
    }

    cout << "Welcome to your new world, or your death. We hope you have fun, " << playerName << "." << endl;

    cout << "In this world, you can decide whether you shall let it prosper, be its hero, or end its suffering. What will you choose?" << endl;

    cout << "TYPE ANY LETTER TO CONTINUE" << endl << "> ";
    char CONTINUE;
    cin >> CONTINUE;

    cout << "\nYou wake up on the soft padded grass. When you take a look around, all you see are layers of trees surrounding you." << endl;

    cout << "As soon as you take a closer look around, you find an opening between the trees. What will you do?" << endl;
    cout << "1. Look around more in your area\n2. Examine the opening\n3. Walk through the opening\n> ";

    int decisionOpening1 = 0;
    cin >> decisionOpening1;

    switch (decisionOpening1) {
    case 1: {
        cout << "You soon find a shrine right behind you, covered in moss. You feel some type of gravitational pull towards it. What shall you do?" << endl;
        cout << "1. Attempt to destroy the shrine\n2. Examine the shrine\n3. Examine the opening in the tree line\n4. Walk through the opening of the tree line\n> ";

        int decisionOpening1C1 = 0;
        cin >> decisionOpening1C1;

        switch (decisionOpening1C1) {
        case 1:
            cout << "\nYou managed to muster all the energy you have to destroy this shrine with a single punch. Upon landing a blow to the shrine, your punch immediately got reflected back towards your face." << endl;
            cout << "Causing your whole body to go into cardiac arrest from your amazing punch getting redirected to you. Not soon after, you then died due to a lack of oxygen going to that tiny little brain of yours." << endl;
            cout << "\nENDING 1 OF 927\nWould you like to try again, " << playerName << "? ";

            char ending1;
            cin >> ending1;
            break;
        case 2: {
            D20 myDice;

            break;
        }
        default:
            cout << "INVALID INPUT";
        }
    }
          break;

    case 2:
        cout << "You soon come to the conclusion that, once you've taken a closer look at the opening, there was a malnourished elf by the exit awaiting for someone to appear." << endl;
        break;

    case 3:
        // other code
        break;

    default:
        cout << "INVALID INPUT";
    }

    return 0;
}

and here is my other files too [header1.h]

#ifndef HEADER_H
#define HEADER_H

#include <string>

class D20 {
public:
    D20();

    void setRoller(const std::string& newRoller);
    std::string getRoller() const;

private:
    std::string roller; 
};

#endif

[my other source code for the constructor]

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
using namespace std;

class D20 {
public:
    D20() {
        setRoller("DefaultRoller");
    }

    void setRoller(string newRoller) {
        roller = newRoller;
    }

    string getRoller() const {
        return roller;
    }

private:
    string roller;
};

int main() {
    D20 myDice;
    srand(static_cast<unsigned>(time(nullptr)));

    for (int i = 1; i <= 1; i++) {
        int random = 1 + (rand() % 20);

        cout << random << endl;
    }


    myDice.setRoller("NewRoller");
    string rollerValue = myDice.getRoller();

    cout << "Roller: " << rollerValue << endl;

    return 0;
}

Can you please help me out?

I've tried using chatGPT to help and no use was working sadly, or I might be doing something wrong when I'm asking for help from it.

1

There are 1 best solutions below

1
tbxfreeware On

Change int main() to int dice()

When I changed the second int main() to int dice(), the link error went away.

I was able to compile, and run a little bit, although I am not sure everything was as you wanted. Part of the problem is that I am unsure what you want the program to do.

I won't be able to solve all your problems, but I think I can help with class D20 that you asked about in your question.

File structure for class D20

In many programs, a class like D20 is broken into two files:

  1. D20.h contains the class declaration, and
  2. D20.cpp contains the definitions for the member functions declared in D20.h.

The header file D20.h is the same as what you called header1.h. Changing the file name and include guards gives the following:

// D20.h
#ifndef D20_H
#define D20_H

#include <string>

class D20 {
public:
    D20();

    void setRoller(const std::string& newRoller);
    std::string getRoller() const;

private:
    std::string roller;
};

#endif
// end file: D20.h

The .cpp file uses the scope resolution operator :: to reference the function names from class D20:

  • D20::D20 is the constructor
  • D20::setRoller is the setter
  • D20::getRoller is the getter

Note, as well, that the .cpp file includes the header D20.h. This gives it access to the class declaration.

The definition you gave for function setRoller had a different function signature from the one used in header file D20.h. In the code below, I changed them to match.

// D20.cpp
#include <string>

#include "D20.h"  // Include the declaration for class D20

D20::D20() {
    setRoller("DefaultRoller");
}

void D20::setRoller(const std::string& newRoller) {
    roller = newRoller;
}

std::string D20::getRoller() const {
    return roller;
}
// end file: D20.cpp

With these two files in place, you can erase class D20 from what you called "[my other source code for the constructor]". Afterwards, be sure to add #include "D20.h in its place.

In addition, you can include the header D20.h in any file that needs to use class D20. One of those is the file you called my game title.cpp.

#include "Header1.h"    // DELETE THIS

#include "D20.h"        // AND REPLACE IT WITH THIS