Click here to Skip to main content
16,015,531 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm creating a text-based adventure game but I've come up with an error.
C++
#include <iostream>
#include <string>
#include <cmath>
#include <stdlib.h>
#include <random>
#include <ctime>
#include <cctype>
#include <ctype.h>
#include "bits/stdc++.h"
#include <list>

using namespace std;

// Variables

// Abilites
string Darkvision = "Darkvision: The ability to see far within the shadows";
string Giantfoe = "Giantfoe: The skill to bring down colossal beasts";
vector<string> abilites;

// Lists
list<string> raceChoiceList = {};
list<string>::iterator it = raceChoiceList.begin();
list<string> yesList = {"Y","Y.","YES","YES."}; // All words that would count as yes. Make sure all letters are uppercase. ex. "YES", "YES."
list<string>::iterator yes = yesList.begin();
list<string> noList = {"N","N.","NO","NO."};  // All words that would count as no. Make sure all letters are uppercase. ex. "NO", "NO."
list<string>::iterator no = noList.begin();

// Booleans
bool raceChoiceHuman = false;
bool raceChoiceDwarf = false;
bool raceChoiceElf = false;

// Strings / String Streams
string yesNoInput;

// Integers
int bugFixer1 = 0; // Used to fix the bug that would say "Invalid Command." immediately.
int yesNoOut;

// End of Variables

int Die(int sides, int minVal) {
    srand((unsigned int) time(NULL));
    int roll = (rand() % sides) + minVal;
    return roll;
}

string printVector(auto vectorName) {
    for (int i = 0; i < vectorName.size(); i++) {
        cout << vectorName[i] << "\n";
    }
}

string findElement(auto vectorName, string Element) {
    string element;
    auto itr = find(Element.begin(), Element.end(), element);
    if (itr != Element.end()) {
        cout << "You have the ability of " << element << "\n";
    } else {
        cout << "You do not have the ability of " << element << "\n";
    }
}
// Classes
class Player {

    public:
        int d6_1;
        int d6_2;
        int d6_3;
        int d6_4;
        int d6_5;
        int d6_6;
        double pi = 2 * acos(0.0);
        string playerClass;
        string name;
        string race;
        string size;
        int strength = Die(18, 3);
        int dexterity = Die(18, 3);
        int constituion = Die(18, 3);
        int intelligence = Die(18, 3);
        int wisdom = Die(18, 3);
        int charisma = Die(18, 3);
        //Abilites Mods'
        int strMod;
        int dexMod;
        int conMod;
        int wisMod;
        int intMod;
        int chaMod;
        string meleeWeapon = "fists";
        string rangedWeapon;
        string magic;

        void statMods() {
            strMod = floor((strength - 10) / 2);
            dexMod = floor((dexterity - 10) / 2);
            conMod = floor((constituion - 10) / 2);
            wisMod = floor((wisdom - 10) / 2);
            intMod = floor((intelligence - 10) / 2);
            chaMod = floor((charisma - 10) / 2);
        }
};

class Item {
    
    public:
        string effect;
};

class Weapon: public Item {

    public:
        string material;
        string enchantment;
};

class Sword: public Weapon {

    public:
        int dmg;
        string dmgType;
};

class Dice {

    public:
        int sides;

        int Roll(int times){
            int rolls;
            srand(time(NULL)); //initialise random num generator using time
            for (int i = 0; i < times; i++){
                int roll_; //declare a variable to keep store the random number
                roll_ = rand() % sides + 1; // generate a random number between 1 and 6
                rolls += roll_;
            }
            return rolls;
        }
};

// Functions

//void menu() {

//}
//Takes string and makes It capitalized
void toupper(std::string &stri) {
    if (stri.length() == 0) {
        return;
    }
 
    transform(begin(stri), end(stri), begin(stri), [](char const &c) {
                    return tolower(c);
                });
    
    int counter = 0;
    for (char letter : stri){
        stri[counter] = toupper(stri[counter]);
        counter += 1;
    }
}

int yesNo(){ // Work in progress
    string yesNoChoice = "aaa";
    getline(cin, yesNoChoice);
    getline(cin, yesNoChoice);
    toupper(yesNoChoice);
    // Marker (for control + h)
    stringstream s(yesNoChoice);  
    string word;
    while (s >> word){
        for (string option : yesList){
            if (word == option){
                cout << "Press enter to continue. \n>";
                return 1;
            }
        for (string option : noList){
            if (word == option){
                return 0;
            }
        }
        }
    }
    return 2;
}

int main() {

    Dice D6;
    D6.sides = 6;
    Player player1;
    player1.statMods();
    Weapon Torch;
    Torch.effect = "light";
    Torch.material =  "wood";
    Item inventory[10] = {Torch};

    //cout << "Type everything in lowercase" << "\n"; // You can type with any order of capital or lowercase thanks to toupper()
    // Ok thanks man
    cout << "What is your name, traveler?" << "\n";
    bool nameRestart = true;
    while (nameRestart) {
        cout << "> ";
        cin >> player1.name;
        cout << player1.name << "? Are you sure that's your true name?" << "\n";
        cout << "> ";
        yesNoOut = yesNo();
        if (yesNoOut == 1) {
            nameRestart = false;
        }
    }
    bool raceRestart = true;
    while (raceRestart/* == true <--- Redundant*/) {
        raceRestart = false;
        if (bugFixer1 == 1){
        cout << "Where do you hail from " << player1.name << "?" << "\n";
        cout << "The Human Cities? Perhapes the Elven Forests? or the Mightly Dwarven Kingdoms?" << "\n";
        cout << "> ";
        }
        string raceChoice;
        getline (cin, raceChoice);
        toupper(raceChoice);
        // Marker (for control + h)
        stringstream ss(raceChoice);  
        string word;
        // What was this supposed to be????????
        // Something that decided ear shape. And you don't need to include the ones with periods.
        // Actually, I did.
        while (ss >> word) { 
            if (word == "ELF" || word == "ELF." || word == "FOREST" || word == "FOREST."|| word == "FORESTS" || word == "FORESTS." || word == "ELVEN" || word == "ELVEN."){
                raceChoiceElf = true;
            } else if (word == "DWARF" || word == "DWARF." || word == "KINGDOM" || word == "KINGDOM."|| word == "KINGDOMS" || word == "KINGDOMS." || word == "DWARVEN" || word == "DWARVEN."){
                raceChoiceDwarf = true;
            } else if (word == "HUMAN" || word == "HUMAN." || word == "CITY" || word == "CITY."|| word == "CITIES" || word == "CITIES."){
                raceChoiceHuman = true;
            }
        }
            if (raceChoiceHuman) {
                player1.race = "Human";
                player1.strength++;
                player1.dexterity++;
                player1.constituion--;
                player1.wisdom--;
                player1.intelligence--;
                player1.charisma++;
                player1.size = "M";
                break;
            } else if (raceChoiceElf) {
                player1.race = "Elf";
                player1.strength--;
                player1.strength--;
                player1.dexterity++;
                player1.dexterity++;
                player1.constituion--;
                player1.wisdom++;
                player1.size = "M";
                abilites.push_back(Darkvision);
                break;
            } else if (raceChoiceDwarf) {
                player1.race = "Dwarf";
                player1.strength++;
                player1.strength++;
                player1.dexterity--;
                player1.dexterity--;
                player1.constituion++;
                player1.charisma--;
                player1.size = "M";
                abilites.push_back(Darkvision);
                break;
            } else {
                if (bugFixer1 == 0){
                    bugFixer1 = 1;
                    raceRestart = true;
                } else {
                cout << "Invalid command" << "\n";
                raceRestart = true;
                }
            }
        }
    cout << "Hmm, so you are a " << player1.race << "?" << "\n";
    string raceCheck;
    cin >> raceCheck;
    toupper(raceCheck);
    player1.statMods();
    if (raceCheck == "No") {
        raceRestart = true;
    } else {
        cout << "Race:" << player1.race << "\n" << "Strength: " << player1.strength << "\n" << "Dexterity: "  << player1.dexterity << "\n" << "Constitution: " << player1.constituion << "\n" << "Wisdom: " << player1.wisdom << "\n" << "Intelligence: " << player1.intelligence << "\n" << "Charisma: " << player1.charisma << "\n" << "Size: " << player1.size << "\n" << player1.strMod << "\n" << player1.dexMod << "\n" << player1.conMod << "\n" << printVector(abilites);
    }
    

}

Everytime I run it I get this:

main.cpp:49:20: warning: use of ‘auto’ in parameter declaration only available with ‘-fconcepts’
   49 | string printVector(auto vectorName) {
      |                    ^~~~
main.cpp: In function ‘std::string printVector(auto:1)’:
main.cpp:53:1: warning: no return statement in function returning non-void [-Wreturn-type]
   53 | }
      | ^
main.cpp: At global scope:
main.cpp:55:20: warning: use of ‘auto’ in parameter declaration only available with ‘-fconcepts’
   55 | string findElement(auto vectorName, string Element) {
      |                    ^~~~
main.cpp: In function ‘std::string findElement(auto:2, std::string)’:
main.cpp:63:1: warning: no return statement in function returning non-void [-Wreturn-type]
   63 | }
      | ^
What is your name, traveler?
> ph 
ph? Are you sure that's your true name?
> yes
Press enter to continue. 
>
Where do you hail from ph?
The Human Cities? Perhapes the Elven Forests? or the Mightly Dwarven Kingdoms?
> elf
Hmm, so you are a Elf?
yes
Race:Elf
Strength: 13
Dexterity: 17
Constitution: 14
Wisdom: 16
Intelligence: 15
Charisma: 15
Size: M
1
3
2
Darkvision: The ability to see far within the shadows
free(): invalid size
Aborted (core dumped)

If anyone could help me It would be much appreciated.
Note: I'm new to C++ so I'm attempting to inform myself on the language and various concepts in the field. I apologize in advance for the most likely horrendous code.

What I have tried:

Spending the last 3 hours of my life trying to figure this out.
Posted
Updated 12-Aug-23 6:31am

The messages are basically telling you not to use auto in function definitions, as the compiler cannot guess what actual type the function expects. For example in the following:
C++
string printVector(auto vectorName) {
    for (int i = 0; i < vectorName.size(); i++) {
        cout << vectorName[i] << "\n";
    }
}

How does the compiler decide what type of items are contained in vectorName?

Note also, why have you defined that function as returning a string, when it does not return anything?
 
Share this answer
 
As the error message says, auto is not for use in parameter declarations. A template is more appropriate for those. Also, your findElement function isn't right. The parameter vectorName is not accessed anywhere so it is not needed. The parameter Element should probably be a vector of strings because one does not call find on a single string. The find call is passed the parameter element but that is not set to anything. Also, the function is supposed to return a string but it does not have a return statement.

That needs some help.
 
Share this answer
 
Comments
Brennon Nevels 12-Aug-23 12:57pm    
Do you have any recommendations for documentation for templates?

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