| TIP: Click on subject to list as thread! | ANSI |
| echo: | |
|---|---|
| to: | |
| from: | |
| date: | |
| subject: | Re: c++ help |
From: John Beckett
"Geo" wrote in message
news::
> Ok, going nuts here. I'm trying to write a simple pick 3 lotto function.
OMG! Please read this slowly -- a lot of details!
Problem 1(a):
VERY important to get the habit of accounting for the zero byte used to
terminate C strings!!!
int i = 7;
char a; // a is uninitialised (junk data)
itoa(i, a, 10); // TWO serious bugs - see below
(1) a is a single char but you want to store a STRING which happens to hold
a single character. You need TWO chars for that (example below).
(2) itoa expects the ADDRESS of a character array as the second argument.
The above passes the contents of a (junk). Suppose that a happens to hold
123 (whatever data was previously in the memory location occupied by a).
itoa will convert i (7) to two bytes and store them at address 123. The two
bytes are:
= byte holding 55 (ASCII code for '7')
= byte holding 0 (indicates end-of-string)
// Corrected code.
int i = 7;
char a[2]; // valid but stupid; safer is a[20]
itoa(i, a, 10);
In the last line, a is an array. By definition, the name of an array is
replaced with its address. That is:
a = &a[0] // address of first element of a
Note that this is totally different from example: char a;
itoa(i, a, 10); // two bugs
In the last line, a is a char (holding junk). itoa is given the contents of
variable a (junk).
Problem 1(b):
Your temp string variable s is five chars long. However, you store five
chars -- you need room for a sixth terminating char (the zero terminator).
Problem 2:
In C/C++, while conditions are evaluated EVERY TIME. This includes the
following for loop:
for (int i=1; i<=rand(); i++) { /* do something */ }
I guess it doesn't matter, but this loop will call rand every time the loop
is entered. This does nothing helpful.
// Fixed code.
int limit = rand();
for (int i = 1; i <= limit; i++) { /* do something */ }
I don't undertand why you want the code to loop.
// New code incorporating above points.
char RandChar()
{
int i = rand() % 10;
return (char)(i + '0');
}
void OnPlay3()
{
char s[5+1];
srand(time(NULL));
int limit = rand();
for (int i = 1; i <= limit; i++)
{
s[0] = RandChar();
s[1] = '-';
s[2] = RandChar();
s[3] = '-';
s[4] = RandChar();
s[5] = '\0';
m_pick3=s;
}
UpdateData(FALSE);
}
Re the RandChar function:
'0' means the ASCII code for zero (it actually means the
character code using whatever this computer's code is, e.g. ASCII or
EBCDIC). On a PC, '0' is 48 (ASCII).
The "(char)" is a cast which means convert the value on the right
(which is an int holding an ASCII code) to a char.
John
--- BBBS/NT v4.01 Flag-5
* Origin: Barktopia BBS Site http://HarborWebs.com:8081 (1:379/45)SEEN-BY: 633/267 270 5030/786 @PATH: 379/45 1 396/45 106/2000 633/267 |
|
| SOURCE: echomail via fidonet.ozzmosis.com | |
Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.