C/C++ >> Extra Stuff in char array
Posted by Doom2pro on 14:11:00 09-30-2002
Hello, When I run this code:

Code:
#include <iostream.h>
#include <stdlib.h>

int rnd(int range)
{
int a = rand()%range;

return a;
}
//>
char itoc(int a)
{
switch (a)
{
case 1:
return '1';
case 2:
return '2';
case 3:
return '3';
case 4:
return '4';
case 5:
return '5';
case 6:
return '6';
case 7:
return '7';
case 8:
return '8';
case 9:
return '9';
case 0:
return '0';
}

return '0';
}

int main(int argc, char *argv[])
{
char* arrayKey = new char[128];
for (int i=0; i < 128; i++)
{
arrayKey[i] = itoc(rnd(9));
}
cout << arrayKey << endl;
system("PAUSE");
return 0;
}


The output is this:

[output]
58748130728276757830065047658520206481732623623721551637237442560145445056753632
050105758163212183680243630223885166233616868834²²²²¦¦¦¦_
[/output]

Where is the extra crap coming from? Why is this happening?

P.S. I am using Microsoft Visual C++ 6 Enterprise.

Thanks in advance, Richard.


[ This Message was edited by: Doom2pro on 2002-09-30 14:12 ]
Posted by Smerdyakov on 21:04:00 09-30-2002
I suggest that you read about what a C string is, specifically the bit about being zero-terminated.

You can also rewrite that "itoc" function in one line of about 15 characters.
Posted by dxprog on 23:14:00 09-30-2002
I don't know much about C++, but could it be that you forgot breaks in you switch? I've had problems like that when I don't put my breaks in. [addsig]
Posted by KaGez on 16:45:00 10-01-2002
_always_ use break; in switches, _always_!! example:

switch (stuff) {
case ("blah"):
/* do stuff */
[b]break;[/]
}

use it like this, or else you will "sicker" through to the next "case()", which is not really the idea, is it?
[addsig]
Posted by fsvara on 20:34:00 10-01-2002
yeah but as he's doing a return in the cases, that won't be the problem.
Posted by KaGez on 15:58:00 10-02-2002
hmmm.... true too.... then I have no idea
[addsig]
Posted by fsvara on 03:20:00 10-03-2002
i suppose, without reading the sourcecode, that he messed something up about the string termination (as smerdyakov said, too)..

QRblah: in c, stirngs are arrays of chars, which need to be temrinated with a NULL, (\0)... that means the last item in the array needs to be NULL... or at least, item just after the last character of the text in your char array
Posted by KaGez on 17:58:00 10-03-2002
I'm not sure about this, but isn't that done automatically?
or is that just in case you declare a char?
[addsig]
Posted by Neu[Mann] on 22:18:00 10-03-2002
KaGeZ: It's not.

The guy is declaring an array of char (a string) but an array of char must always end by \0. Simply declaring a char array[] doesn't automatically add a \0, hence the line:

cout
[addsig]
Posted by JWalker on 08:23:00 11-15-2002
Why the big switch statement? You can just convert an integer value to the corresponding chararacter value by adding '0':
Code:
char itoc ( int a )
{
if ( a < 0 || 9 < a )
return '0'; // Do you really want to return '0' on error?

return a + '0';
}

-->int a = rand()%range;
This really isn't the best way to create a random number, especially if the range is small.

http://www.eskimo.com/~scs/C-faq/q13.16.html

-->char* arrayKey = new char[128];
There's little point in using dynamic memory allocation for this, especially since you neglect to delete it before terminating the program.

-->cout int main(int argc, char *argv[])
I don't see you use either argc, or argv in the program, there's no need to declare them.

-JW