views:

82

answers:

4

my program makes a random name that could have a-z this code makes a 16 char name but :( my code wont make the name and idk why :( can anyone show me what's wrong with this?

char name[16];
void make_random_name()
{

  byte loop = -1;

  for(;;)
  {
      loop++;

      srand((unsigned)time(0));
      int random_integer;

      random_integer = (rand()%10)+1;

      switch(random_integer)
      {
      case '1': name[loop] = 'A';
          break;
      case '2': name[loop] = 'B';
          break;
      case '3': name[loop] = 'C';
          break;
      case '4': name[loop] = 'D';
          break;
      case '5': name[loop] = 'E';
          break;
      case '6': name[loop] = 'F';
          break;
      case '7': name[loop] = 'G';
          break;
      case '8': name[loop] = 'Z';
          break;
      case '9': name[loop] = 'H';
          break;
      }

      cout << name << "\n";

      if(loop > 15)
      {
          break;
      }

  }


 }
+4  A: 

random_integer is an integer, you are comparing it to a bunch of characters from the ASCII character set - '1' as a character literal is actually 49 in decimal. As 49 is not in the range of your random numbers, it'll never get hit.

Try changing your case statements to

case 1: ...

instead of

case '1': ...
kibibu
... lol xD nice ok let me try
blood
hmm well it runs but all of them are one char like h or z :\
blood
@blood That's probably because you are calling `srand` inside the loop. The whole loop runs before `time(0)` changes, so you seed the RNG with the same value every time and get the same random number each time. Also note that `rand()%10+1` gives numbers from 1 - 10; you haven't covered all those cases in your switch.
Nick Meyer
.. yea 1-10 i just stoped at 9 because i did not care if i had one more it will just make a random name that is really not readable anyway but ty.
blood
+2  A: 
srand((unsigned)time(0));

Take this out of the for loop, you need to seed it once only.

case '1':

1 does not mean integer 1, rather it is character 1 which translates (ascii) to integer 49. Change it to -

case 1 :
N 1.1
o lol yea this also fixed it xD tyvm
blood
+2  A: 

Or just use

char name[16];
for (i = 0; i < sizeof name; ++i) {
  name[i] = "ABCDEFGHIJ"[rand() % 10];
}
Alex
ew, character array
Inverse
A: 

There are so many things wrong with this that I'll list them:

1) You don't null terminate the string. 2) You're needlessly using a switch statement for data that could very very easily be done with array indexing. 3) You're calling srand in a loop. 4) for(;;) { i++ ... -- Really? 5) Don't bring your homework to stack overflow. No professional developer would ever post that (or if they did, I fear for their employer).

Bob
... this is a home project O_o and... your not helpfull at all...
blood
The bit about null terminating your string is important, even if this answer is pretty aggro - there are plenty of homework and beginner questions here, and most of them don't even have a crack at a solution. edit: actually, you've already taken care of null terminating your string as it's global. Nevermind.
kibibu
:\ ok =33333333
blood
Making it global does not null terminate it. The bounds of the array are known by the compiler, but any string processing library will choke on it. You absolutely have to assign '\0' or 0 to the last byte in the string.
Bob
@bob :yea i already know this, so your point is? O_o
blood