-1

This question is most likely common and has been asked before. So, after spending a mind-numbing 30 minutes trying to find the solution to what I believe is a false negative, I'm resulting to posting my issue here.

I'm relatively new to C++ Coding and figured I'd create a simple random item generator. Unfortunately, when grasping a random index in my arrays, I am given a c6385 error. This error usually pertains to an invalid or overloaded buffer from the research I've found. I believe this means that the index I'm trying to access is too large thanks to rand().

I will continue looking for a solution but would like some others' opinions of the situation. I may be overlooking a small detail or am failing to grasp a concept. All help is greatly appreciated. If I come across a solution, I will lock/remove this to overpopulate the forums.

Here is my code: ItemGeneration.h

#pragma once
#include <random>
#include <iostream>
using namespace std;

class ItemGeneration
{
public:
    /*
        Creating the cosntant variables for the items main effects.
        For instance, if you were to roll the main attribute of "Item1",
        this code would dictate what possible attributes that could be chosen.

        In addition to the main attributes, the code for the avaliable sub-attributes is also created below.

        Also creating the const array that hold the rarity of items.
        For instance, an item that rolls a higher weighted value will have a lower rating.
        More rare or 'uncommon' the value, the higher rating the item will recieve.
        Item rarity dictates the value of the attributes assigned to ie, i.e the higher the
        rarity the higher the attributes multiplier in addition to the number of sub-attributes.
    */

    const char* Item1[4]
        = { "Attack %", "Health %", "Defense %", "Elemental Damage %" };

    const char* Item2[4]
        = { "Mana Regeneration", "Cooldown Reduction", "Healing Efficacy", "Elemental Resonance" };

    const char* Item3[4]
        = { "Raw Attack", "Raw Health", "Raw Defense", "Raw Elemental Damage" };

    const char* Item4[4]
        = { "Elemental Resonance", "Critical Chance", "Critical Multiplier", "Mana Regeneration" };

    const char* Item5[4]
        = { "Elemental Damage %", "Critial Chance", "Critical Multiplier", "Cooldown Reduction" };

    const char* SubAttributeList[10]
        = { "Raw Attack", "Raw Health", "Raw Defense", "Raw Elemental Damage", "Elemental Damage %",
            "Elemental Resonance", "Mana Regeneration", "Cooldown Reduction", "Critical Chance", "Critical Multiplier" };

    const char* RarityChart[6]
        = { "Common", "Uncommon", "Rare", "Unique", "Legendary", "Mythical" };

    const int InventoryLimit = 256;
    int InventorySize = 0;

    int ItemCreate(int x, int y) {

        int randNum = rand() % 4 + 1;

        if (InventorySize < InventoryLimit) {
            switch (x) {
            case 1:
                cout << "You have generated an Item1! Here are the resulting attributes:" << endl;
                cout << "Main Attribute: " << Item1[randNum] << endl;
                cout << "Sub-Atrributes: " << endl;
                break;

            case 2:
                cout << "You have generated an Item2! Here are the resulting attributes:" << endl;
                cout << "Main Attribute: " << Item2[randNum] << endl;
                cout << "Sub-Atrributes: " << endl;
                break;

            case 3:
                cout << "You have generated an Item3! Here are the resulting attributes:" << endl;
                cout << "Main Attribute: " << Item3[randNum] << endl;
                cout << "Sub-Atrributes: " << endl;
                break;

            case 4:
                cout << "You have generated an Item4! Here are the resulting attributes:" << endl;
                cout << "Main Attribute: " << Item4[randNum] << endl;
                cout << "Sub-Atrributes: " << endl;
                break;

            case 5:
                cout << "You have generated an Item5! Here are the resulting attributes:" << endl;
                cout << "Main Attribute: " << Item5[randNum] << endl;
                cout << "Sub-Atrributes: " << endl;
                break;

            default:
                cout << "They item you tried to generate doesn't seem to exist.\nPlease enter a number between 1 and 5 to generate a random item." << endl;

            }
        }
        else {
            cout << "Sorry, your inventory is too full\nPlease clear some space before attempting to create another item." << endl;
        }
    }
};

Source.cpp

#include <iostream>
#include "ItemGeneration.h"
using namespace std;

int main() {
    int x, y;

    cout << "Welcome to the random item generator!" <<
        "\nPlease enter a number between 1 and 5, " <<
        "\nfollowed by a number between 1 and 10 to select its rarity, " <<
        "\nto receive your own randomly generated item.\n" << endl;

    cin >> x;
    cin >> y;

    ItemGeneration item;
    item.ItemCreate(x, y);
    return;
}

** Update with errors **

I should have been more concise. However, I do not believe there to be an actual buffer error, a false negative. As for the error messages.

My main function is returning a c2561 error, but I believe that to be a side effect of the item generation, not functioning. MEaning it simply is returning the value as the function isn't operating. Here is my terminal readout when attempting to build the solution:

Build started...
1>------ Build started: Project: RandomItemGenerator, Configuration: Debug x64 ------
1>ItemGeneration.cpp
1>Source.cpp
1>C:\Users\Alex\source\repos\RandomItemGenerator\RandomItemGenerator\Source.cpp(18,2): error C2561: 'main': function must return a value
1>C:\Users\Alex\source\repos\RandomItemGenerator\RandomItemGenerator\Source.cpp(5): message : see declaration of 'main'
1>Generating Code...
1>Done building project "RandomItemGenerator.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

The quick solution error description:

(const char [17]) "Main Attribute: "
C6385: Reding Invalid Data from 'this->item5'.
  • What is the actual full error message? – NathanOliver May 26 '22 at 13:15
  • 1
    Please copy the error verbatim into the question. Also, I think you mean `false positive`, since `false negative` would be not reporting a warning/error when there actually is one. – ChrisMM May 26 '22 at 13:15
  • Note that in order to call `rand()` reasonable, you should ensure that `srand()` was called exactly once before. – πάντα ῥεῖ May 26 '22 at 13:17
  • Also, when you post code, a half-screen of comments is not necessary. Remove the comments. In addition, you really shouldn't use `using namespace std;` in a header file. – PaulMcKenzie May 26 '22 at 13:17
  • 1
    Your `ItemCreate` function is supposed to return an `int`, and you failed to do so. The program is broken. – PaulMcKenzie May 26 '22 at 13:20
  • 1
    Your issue comes from syntax errors... fix them and everything will work: just return 0 both inside `ItemCreate` and `main` – Marco Beninca May 26 '22 at 13:30
  • For the 1 error you have you can delete `return;` in a modern version of visual studio or change it to `return 0;` Here is the reason why you can omit the return on `int main()` : [https://stackoverflow.com/questions/19293642/why-does-the-main-function-work-with-no-return-value](https://stackoverflow.com/questions/19293642/why-does-the-main-function-work-with-no-return-value) – drescherjm May 26 '22 at 13:31
  • I have removed the exptected returned int from the function. In addition, I originally had the srand() function called previous to the rand() fucntion, however when testing various things to resolve the issue, I forgot to replace the code. Thank you to everyone for responding so quickly to this question. – Papa Frozie May 26 '22 at 13:31
  • I have fiddled some more with the code and found a working solution. I changed the Item creation function to a void function and removed the return from the main. Again, thank you all for the prompt responses. I will educate myself futher to avoid using std namespace in the future, make such obvious over sights, and to pay further attention to my declarations. – Papa Frozie May 26 '22 at 13:34

1 Answers1

0
int main() {
   // stuff
   return;  // <- always an error
}

Here's your problem. You promised that main would return an int, and then you reneged on that promise with just a plain return. main is a little special in that it should return an int, so, failing all else, make it return 0.

int main() {
   // stuff
   return 0; 
}

You did it again in ItemGeneration::ItemCreate

int ItemCreate(int x, int y) 
{
   // stuff
   return;    // nope
}

But in this case, there isn't really anything to return. Your main routine ignores the return value anyway. So you should declare it to return void instead:

void ItemCreate(int x, int y) 
{
   // stuff
   return;    // OK now (not actually needed, however)
}
Spencer
  • 1,924
  • 15
  • 27