Generating a random character/letter, stuck!

This is a discussion on Generating a random character/letter, stuck! within the C++ Programming forums, part of the General Programming Boards category; I seem to be quite stuck on this one... I have even re-written this post several times trying to explain ...

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    129

    Generating a random character/letter, stuck!

    I seem to be quite stuck on this one... I have even re-written this post several times trying to explain what I want to do!

    The code I have written so far, compiles without any trouble on Bloodsehd Dev-C++. But... It just sits there doing nothing, just a flashing cursor in the dos box. I have narrowed this down to my random part of the code.

    Code:
    // lets start generating what stars we have
        
        for (char startype001;startype001==65,66,67,71,72,76,78,82,83,86,87,88,89,112,42;)
        {
          srand(time(0));
          startype001 = rand()%256+1;
        
        char star001 = startype001;
        }
    As you can see, those are the ascii values I want, the others I do not want, and if rand turns one up that is not one of those, I want it to try another number. Once I do have a correct ascii value from the list, I then want it to become a char value, and then onwards towards the rest of my code (which works fine).

    Can anyone help with this?

  2. #2
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    The commas in the for loop are basically separating statements. What you have is equivalent to saying:

    starttype001 == 65;
    66;
    67; ... etc...

    You need to use the logical OR || and == every time:

    startype001 == 65 || startype001 == 66 || ... etc ...

    Also, just a stylistic point, a while loop might be logically more suited to the task (though it'd be functionally the same).
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  3. #3
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    Beat me Salem.

    Dan, that is pure ugly.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  4. #4
    Registered User
    Join Date
    Jun 2003
    Posts
    129
    Well, I did start last night

    lol

  5. #5
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Bah! Who wants an elegant solution anyways!
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Generating random letters
    By ominub in forum C Programming
    Replies: 6
    Last Post: 04-29-2009, 02:12 AM
  2. Generating random 2D mountains/terrain
    By Flaug in forum C++ Programming
    Replies: 7
    Last Post: 04-12-2009, 03:49 PM
  3. Help generating multiple random numbers
    By d-dub in forum C++ Programming
    Replies: 7
    Last Post: 10-30-2006, 01:00 PM
  4. Replies: 7
    Last Post: 09-26-2005, 06:09 PM
  5. Stuck on random generating
    By Vegtro in forum C++ Programming
    Replies: 3
    Last Post: 10-01-2003, 08:37 PM

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