random generator of chars help

This is a discussion on random generator of chars help within the C++ Programming forums, part of the General Programming Boards category; how do i make a random generator so that my program can randomly choose from an array of operations? I ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,592
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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-09-2009, 11:30 PM
  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, 03:09 PM
  4. Another brain block... Random Numbers
    By DanFraser in forum C# Programming
    Replies: 2
    Last Post: 01-23-2005, 04: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21