0

I'm trying to have the program out put either a color or a number at random.

Any help would be greatly appreciated.

Thank You in advance for not being evil.

I originally wasn't using a function so I created one and that didn't help. I also tried changing i from 0 to one. I thought a simple for loop would be easy

#include <iostream>
#include <cstdlib>
#include <time.h>
using namespace std;

int rand_num = rand();

int rand_color = rand() % 6; // should output color
int rand_distance = rand() % 5; // should output distance

int srand(time(NULL)); // initialize random seed
const string color[6] = { "red", "blue", "green", "orange", "yellow", "brown" };
const int distance[5] = { 7,25,20,10,15 };


    void random_function()
    {

        if (rand_num % 2)
        {
        cout << color[rand_color];
        }
        else
        cout << distance[rand_distance];
    }

int main()
{



    for (int i = 1; i < 30; i++)
    {
        random_function();

    }
    return 0;
}
Fleckism
  • 15
  • 6
  • 1
    Your code doesn't compile, so there are no loop executions. – Yksisarvinen Nov 13 '20 at 15:20
  • 2
    You need to assign those variables inside the function. – 001 Nov 13 '20 at 15:21
  • 2
    `int rand_num = rand();` is not an equation that makes `rand_num` equivalent to calling `rand`. You need a [good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – molbdnilo Nov 13 '20 at 15:22
  • I also get "error: ‘int srand’ redeclared as different kind of symbol". That's because you're declaring an `int` variable called "srand", initializing it with `time(NULL)`. – molbdnilo Nov 13 '20 at 15:25
  • It compiles and runs for me in VS19, but it only runs once. I moved all the variables inside the function and that didn't change anything either. – Fleckism Nov 13 '20 at 15:28
  • 1
    MSVC doesn't compile it either: https://godbolt.org/z/b4rT4Y – Yksisarvinen Nov 13 '20 at 15:33
  • 1
    Curious. My VS2109 says "error C2365: 'srand': redefinition; ..." and "error C2872: 'distance': ambiguous symbol..." Are you sure that you're not compiling something else? – molbdnilo Nov 13 '20 at 15:35
  • It doesn't compile on my MSVC either. There is something you're not telling us. – Jabberwocky Nov 13 '20 at 15:37

2 Answers2

4

You probably want this:

#include <iostream>
#include <cstdlib>
#include <time.h>

using namespace std;

const string color[6] = { "red", "blue", "green", "orange", "yellow", "brown" };
const int distance[5] = { 7,25,20,10,15 };

void random_function()
{
  // you must assign your variables each time
  int rand_num = rand();
  int rand_color = rand() % 6; // should output color
  int rand_distance = rand() % 5; // should output distance

  if (rand_num % 2)
  {
    cout << color[rand_color] << " ";  // output also a space so make it readable
                                       // the :: before distance is needed because
                                       // distance is member of the std namespace
                                       // and you have using namespace std;
  }
  else
  {
    cout << ::distance[rand_distance] << " ";   // output also a space so make it readable
  }
}

int main()
{
  srand(time(NULL));     // call srand in main

  for (int i = 1; i < 30; i++)
  {
    random_function();    
  }
  return 0;
}
Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
  • FYI, you don't need to specify the capacities of the global arrays. The compiler will figure it out by the quantity of initializers. This eliminates defects regarding capacity too large or too small. – Thomas Matthews Nov 13 '20 at 18:57
  • 1
    You can remove symbol collisions, such as `distance`, by not having `using namespace std;`. – Thomas Matthews Nov 13 '20 at 18:59
0

The way you are using static storage with global variables is throwing exceptions that are not caught, your code does not compile, I fixed it below.

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




void random_function(int rand_num, int rand_color, int rand_distance, int srand, const string color[], const int distance[])
{

    if (rand_num % 2)
    {
        cout << color[rand_color];
    }
    else
        cout << distance[rand_distance];
}

int main()
{

    int rand_num = rand();

    int rand_color = rand() % 6; // should output color
    int rand_distance = rand() % 5; // should output distance

    int srand(time(NULL)); // initialize random seed
    const string color[6] = { "red", "blue", "green", "orange", "yellow", "brown" };
    const int distance[5] = { 7,25,20,10,15 };

    for (int i = 1; i < 30; i++)
    {
        random_function(rand_num, rand_color, rand_distance, srand, color, distance);
        cout << i << endl;

    }
    return 0;
}
Z. Fralish
  • 438
  • 4
  • 9