Thread: random generator of chars help

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    2

    random generator of chars help

    how do i make a random generator so that my program can randomly choose from an array of operations? I know that it is not right, but I don't really know how to fully do it so I kind of made some things things up

    this is part of my code in my .cpp file

    Code:
    {
      Problem result;
      char problem.m_arithmOp[4] = {'-','+','%','*'};
      srand( (unsigned)time(0));
      for(int i=0; i<1 ; i++)
      {
       result.m_arithmOp[i] = (rand()%3)+1;
      }
      return result;
    and this is my struct in my header

    Code:
    struct Problem
    {
      int m_number1;
      int m_number2;
      int m_answer;
      int m_level;
      char m_arithmOp;
    } problem;

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Your 'op' is a single char, not an array.

    So try something like
    result.m_arithmOp = "+-*/"[rand()%4];
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. server-client injecting random char's?
    By abraham2119 in forum C Programming
    Replies: 3
    Last Post: 01-10-2009, 12:30 AM
  2. File Copying - Random chars at the end
    By Govalant in forum C Programming
    Replies: 4
    Last Post: 05-26-2007, 07:33 AM
  3. Random chars in a certain range
    By cboard_member in forum C++ Programming
    Replies: 3
    Last Post: 02-04-2006, 04:09 PM
  4. Another brain block... Random Numbers
    By DanFraser in forum C# Programming
    Replies: 2
    Last Post: 01-23-2005, 05:51 PM
  5. Best way to generate a random double?
    By The V. in forum C Programming
    Replies: 3
    Last Post: 10-16-2001, 04:11 PM