-3

I am trying to create an array of object type teaStep, aswell as attempting to reorder a list to a random list.

The function makeSteps is meant to initiate the variables and store them in an array and output them. The function randomizeSteps is meant to make an array of ints and randomize their order, before outputting the new array. Just wondering if this is the right way to do it. [edited] Using a vector, I now have this:

  auto makeSteps()
{
    auto all_steps = std::vector<teaStep>{};
    all_steps.emplace_back("Pick your tea", 1);
    all_steps.emplace_back("Boil water", 2);
    all_steps.emplace_back("Get tea bag in mug or pot", 3);
    all_steps.emplace_back("Pour boiled water over teabag", 4);
    all_steps.emplace_back("Wait!", 5);
    all_steps.emplace_back("Remove teabag", 6);
    all_steps.emplace_back("Add milk and/ or sugar", 7);
    all_steps.emplace_back("Mix, drink and enjoy", 8);


    return all_steps;
}

   auto randomizeSteps()
{
    vector <int> ranstep;
    for (int i = 1; i < 9; i++)
        ranstep.push_back(i);
    auto rng = std::default_random_engine{};
    std::shuffle(std::begin(ranstep), std::end(ranstep), rng);
    return ranstep;
}

To add to this, once this is complete, I must be able to utilise the objects in the vectors, however when I try this I get an error, example below:

//should go through the object in vectors, and compare the field Order, to int a.
teaStep findStep(int a, std::vector<teaStep> vectors)

{
    for (int b = 0; b < 8; b++)
    {
        teaStep test = vectors[b];
        if (test.getOrder == a)
            return test;
    }
}

The error I get is "no conversion from int to int(..."

schorsch312
  • 5,553
  • 5
  • 28
  • 57

2 Answers2

2

You return a pointer to a local variable here:

    teaStep arr[] = /* ... */;
    return arr;
}

The lifetime of arr ends as soon as you exit makeSteps (at the closing }). And whenever you manipulate the return value of makeSteps, you get garbage.

Try and use std::vector instead of plain C arrays, for instance:

auto make_steps()
{
    auto steps = std::vector<teaStep>{};
    steps.emplace_back("Pick your tea", 1);
    steps.emplace_back("Boil water", 2);
    steps.emplace_back("Get tea bag in mug or pot", 3);
    steps.emplace_back("Pour boiled water over teabag", 4);
    steps.emplace_back("Wait!", 5);
    steps.emplace_back("Remove teabag", 6);
    steps.emplace_back("Add milk and/ or sugar", 7);
    steps.emplace_back("Mix, drink and enjoy", 8);
    return steps;
}
YSC
  • 38,212
  • 9
  • 96
  • 149
  • I don't get the emplace_back, with the code I showed above, I initiated all 8 teaSteps first, using vector, I now have this: – Daniel Peedah Jun 25 '18 at 12:12
  • `emplace_back` constructs a `teaStep` in the vector, rather than copying an existing `teaStep`. You don't need to give names (`step1`, ... `step8`) to those `teaSteps` – Caleth Jun 25 '18 at 12:15
0

In addition to what @YSC said, please don't use std:random_shuffle. It is deprecated.

Use std::shuffle instead.

#include <algorithm>
#include <random>

auto makeSteps()
{
    auto steps = std::vector<teaStep>{};
    steps.emplace_back("Pick your tea", 1);
    steps.emplace_back("Boil water", 2);
    steps.emplace_back("Get tea bag in mug or pot", 3);
    steps.emplace_back("Pour boiled water over teabag", 4);
    steps.emplace_back("Wait!", 5);
    steps.emplace_back("Remove teabag", 6);
    steps.emplace_back("Add milk and/ or sugar", 7);
    steps.emplace_back("Mix, drink and enjoy", 8);
    return steps;
}


auto randomizeSteps()
{
    std::vector<int> ranstep(10) ; // vector with 10 ints.
    std::iota (std::begin(v), std::end(v), 0); // Fill with 0, 1, ..., 9.
    auto rng = std::default_random_engine{};
    std::shuffle(std::begin(ranstep), std::end(ranstep), rng);
    return ranstep;
}
schorsch312
  • 5,553
  • 5
  • 28
  • 57