Why won't this code work? I think I did it right... the errors are "cannot convert int to char" and incorrect parameters... heres the code:
Code:old code; new code at the bottom.
Printable View
Why won't this code work? I think I did it right... the errors are "cannot convert int to char" and incorrect parameters... heres the code:
Code:old code; new code at the bottom.
Your problem is that you are trying to copy a string of characters into a variable that was designed to hold only ONE character. You need to change the "unsigned char monnm;" to "char monnm[15]" which will be able to hold up to 15 characters.Code:unsigned char monnm;
int monatt;
int monhp;
int monchs=rand()%5;
switch(monchs)
{
case 0:
strcpy(monnm,"Spider");
break;
case 1:
strcpy(monnm,"Serpent");
break;
case 2:
strcpy(monnm,"Orc");
break;
case 3:
strcpy(monnm,"Goblin");
break;
case 4:
strcpy(monnm,"Hound");
break;
}
Brendan
The strcpy function takes the arguments: (char*, const char*)
You have given it: (unsigned char, const char*)
See the problem? :)
Only problem is... it seems to be the spider now, everytime. I don't think the random generation check is working. But I can't seem to find the problem.
You probably have not correctly set up a proper seed.
what do you mean by a proper seed?
rand() generates random numbers from a seed; not thin air. If you are using the same seed every time you run the program, you will get the same numbers each time.Many will tell you to doCode:#include <time.h>
#include <stdlib.h>
srand(time(0));
int MyRandomNum = int((double(rand()))/(double(RAND_MAX+1))/double(EXCLUSIVE_BOUNDARY));
But I wouldn't as the modulus will focus higher bits of rand() rather than lower bits... so; it's ugly but more truly random ;)Code:MyRandomNum = rand()%EXCLUSIVE_BOUNDARY;
EDIT: Whoops, small code error... all better
so your saying rand=not truely random?
... well, to be exact; the return value of rand() is not a truly random number
Here's the updated code:
Code:unsigned char* monnm;
int monatt;
int monhp;
int monchs=rand()%5;
switch(monchs)
{
case 0:
monnm = strdup("Spider");
monhp=30;
break;
case 1:
monnm = strdup("Serpent");
monhp=30;
break;
case 2:
monnm = strdup("Orc");
monhp=40;
break;
case 3:
monnm = strdup("Goblin");
monhp=35;
break;
case 4:
monnm = strdup("Hound");
monhp=50;
break;
}
int linex;
int liney;
while(monhp>1&&hp>1)
{
clrscr();
linex=1;
liney=1;
line(linex,liney);
cout << monnm << endl;
cout << "HP: " << monhp << endl;
cout << endl << endl;
cout << firstname << endl;
cout << "HP: " << hp << "/" << hpmax << endl;
liney=10;
line(linex,liney);
cout << "Choices: " << endl;
cout << "1. Attack" << endl;
cout << "2. Defend" << endl;
cout << "3. Run Away" << endl << endl;
linex=1;
liney=17;
line(linex,liney);
getch();
}
}
use srand((unsigned)time(0)); before your first call to rand();
Looking back at your previous thread dealing with this issue, I couldn't help but notice Hammer had posted this rather useful link...Quote:
so your saying rand=not truely random?
http://www.eskimo.com/~scs/C-faq/q13.16.html
Which it appears you may have discarded at the time, assuming instead that you already knew everything you needed to about random numbers. May I suggest you take the time to have a look.
Ooh, I like that page; they promote the formula for getting better non-biased towards high-bit numbers :) Although I said the same thing.Quote:
Originally posted by Azuth
Looking back at your previous thread dealing with this issue, I couldn't help but notice Hammer had posted this rather useful link...
http://www.eskimo.com/~scs/C-faq/q13.16.html
Which it appears you may have discarded at the time, assuming instead that you already knew everything you needed to about random numbers. May I suggest you take the time to have a look.