0

I am attempting to make a program where people put in their preferences file what they would want to do for that week. I made a huge if else statement and need help with my else statement. My else statement is fair to the users/members as their list is randomly picked and the file is created. However, the else statement creates the file, but does not write any of the roles to it. Here is the code :

else {

    string positions[23] =
    {   "Day_Clean" "Dinner_Cook" "Dinner_Cook" "Dinner_Clean" "Dinner_Clean" "HEB_Shop" "Costco_Shop" "Silver_Fridge" "Stove_Micowave" "Appliance_Clean" "LH_Bathrooms" "Laundry_Room" "Upstairs_Commons" "Bikeroom_Entrance_Stairs" "Little_House_Commons" "Porch_Grounds" "Recycling" "Gardening" "Condi_Fridge_Personal_Fridge" "Freezer" "Downstairs_Bathroom_1" "Downstairs_Bathroom_2" "Upstairs_Bathroom" "Big_House_Hallways"  };
      ofstream randomPrefs;
  randomPrefs.open(foo);

  int randomPrefloopcount;
  do {
  int randomPrefs1 = rand() % 25;
  randomPrefs << positions[randomPrefs1] << "\n";

  randomPrefloopcount++;
} while(randomPrefloopcount <= 24);
  randomPrefs.close();

The files are created, but they are all blank. Please help!

Yo Bro
  • 59
  • 1
  • 7
  • There are 23 elements in the array. The loop thinks there are 24. Undefined behavior. `randomPrefloopcount` is not initialized, then incremented by the loop. Undefined behavior. – Sam Varshavchik May 30 '16 at 01:39
  • I put 23, because all arrays begin at 0. int randomPrefloopcount; is enough to initialize itself. – Yo Bro May 30 '16 at 01:42
  • 2
    `rand(0 % 25)` will result in a maximum value of 24. positions[24] will be undefined behavior, and a likely crash. And, no, "int randomPrefloopcount" is not "enough to initialize itself". – Sam Varshavchik May 30 '16 at 01:44
  • There is no commas on initializer list of array `positions`. – Piotr Siupa May 30 '16 at 01:45
  • Can you provide a valid rewrite of some of my code to clarify what I did wrong? Provide it in an answer form, although, so I can mark you as the correct answer owner or not.. Thanks! – Yo Bro May 30 '16 at 01:46
  • There don't need to be commas, @NO_NAME – Yo Bro May 30 '16 at 01:47
  • 1
    Yo Bro, w/o commas, the compiler *concatenates* the string items into one string. – Thomas Matthews May 30 '16 at 01:52
  • Adding commas and changing it to 24 outputs only one random job to a text, but I need it to repeat and output a list randomly using the do function. How? – Yo Bro May 30 '16 at 01:58
  • Did you initialize `randomPrefloopcount`? BTW, `do` is a loop, not a function :) – Piotr Siupa May 30 '16 at 02:01

2 Answers2

0

A simple program to verify a concept:

#include <iostream>

int main()
{
  static const char *tokens[] = {"Tory" "Grant"};
  static const char *mythbusters[] = {"Adam", "Jamie"};

  // Print out the quantity of elements:
  std::cout << "Elements in tokens: " << (sizeof(tokens) / sizeof(tokens[0])) << "\n";
  std::cout << "Elements in mythbusters: "
            << (sizeof(mythbusters) / sizeof(mythbusters[0]))
            << "\n";
  return 0;
}

This will show you the difference between concatenation of string literals and specifying more than one literal.

Edit: Added '*' before tokens and mythbusters.

vianney
  • 161
  • 6
Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
0

As stated by Sam Varshavchik your problem lies between the number of elements in your array and your loop.

Let's suppose your values never change, there is a safer way to declare and use an array like this in c++11.

#include <array>
#include <iostream>

int main()
{
    constexpr std::size_t                           kArraySize{ 3 };
    constexpr std::array<const char *, kArraySize>  kArray {
          "hehe"
        , "hehe"
//      , "hehe" // this one would work since there is 3 elements defined
//      , "hehe" // this one would fail to compile
    };

    for (auto element : kArray)
    {
        if (element != nullptr)
        { std::cout << "element: " << element << std::endl; }
        else
        { std::cout << "element is null" << std::endl; }
    }
}

std::array ensures that values not defined by user will be zero-initialized, in this case it means that they will be nullptr'ed.

Specified in C++11 §8.5/8:

To value-initialize an object of type T means:

— if T is a (possibly cv-qualified) class type without a user-provided or deleted default constructor, then the object is zero-initialized …, and if T has a non-trivial default constructor, the object is default-initialized;

Quote stolen from this post.

Edit, applied to your code it will look like this:

else {
    constexpr std::size_t                           kArraySize{ 23 };
    constexpr std::array<const char *, kArraySize>  kPositions { "Day_Clean", "Dinner_Cook", "Dinner_Cook", "Dinner_Clean", "Dinner_Clean", "HEB_Shop", "Costco_Shop", "Silver_Fridge", "Stove_Micowave", "Appliance_Clean", "LH_Bathrooms", "Laundry_Room", "Upstairs_Commons", "Bikeroom_Entrance_Stairs", "Little_House_Commons", "Porch_Grounds", "Recycling", "Gardening", "Condi_Fridge_Personal_Fridge", "Freezer", "Downstairs_Bathroom_1", "Downstairs_Bathroom_2", "Upstairs_Bathroom", "Big_House_Hallways"  };

    ofstream randomPrefs;
    randomPrefs.open(foo);

    std::size_t maxRandomLoop { 25 };
    for (std::size_t i = 0 ; i < maxRandomLoop ; ++i)
    {
        int randomPrefs1 = rand() % kArraySize;
        if (kPositions[randomPrefs1] != nullptr)
        { randomPrefs << kPositions[randomPrefs1] << "\n"; }
    }
    randomPrefs.close();
}

Re-edited, you forgot the commas in your array.

Community
  • 1
  • 1
vianney
  • 161
  • 6
  • Can someone apply all this knowledge to my code? I have no idea what you people are saying, and seeing raw code would help me understand. Try to apply it to my code, so I can see the difference. – Yo Bro May 30 '16 at 02:15
  • Only outputs one time into the text file. The for loop was to output it 24 times so all the jobs would be in a different order in the loop. Is the loop not working? What's wrong with it? – Yo Bro May 30 '16 at 02:57
  • There were some typos, should work as intended now. Note that I put `std::size_t maxRandomLoop { 25 };` without necessarily knowing what was wanted, maybe `25` should be `kArraySize`. – vianney May 30 '16 at 03:23
  • I figured that out beforehand and changed the integers, it seems they were the problem. – Yo Bro May 30 '16 at 13:50