Fleckism
Fleckism

Reputation: 15

For loop only executes once. Other posts didn't help me

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;
}

Upvotes: 0

Views: 55

Answers (2)

Jabberwocky
Jabberwocky

Reputation: 50775

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;
}

Upvotes: 4

Z. Fralish
Z. Fralish

Reputation: 458

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;
}

Upvotes: 0

Related Questions