New Battle Engine Problem

This is a discussion on New Battle Engine Problem within the C++ Programming forums, part of the General Programming Boards category; Why won't this code work? I think I did it right... the errors are "cannot convert int to char" and ...

  1. #1
    Student drdroid's Avatar
    Join Date
    Feb 2002
    Location
    Montreal, Quebec
    Posts
    669

    New Battle Engine Problem

    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.
    Last edited by drdroid; 10-09-2002 at 04:40 PM.

  2. #2
    Registered User harryP's Avatar
    Join Date
    Sep 2002
    Posts
    124
    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;
    	}
    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.

    Brendan
    Draco dormiens nunquam titallandus.
    Console Graphics Library: http://www.geocities.com/steve_alberto/cgl.html

  3. #3
    BMJ
    BMJ is offline
    Banal internet user BMJ's Avatar
    Join Date
    Aug 2002
    Location
    Chicagoland
    Posts
    1,380
    The strcpy function takes the arguments: (char*, const char*)

    You have given it: (unsigned char, const char*)

    See the problem?

  4. #4
    Student drdroid's Avatar
    Join Date
    Feb 2002
    Location
    Montreal, Quebec
    Posts
    669

    ...

    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.

  5. #5
    BMJ
    BMJ is offline
    Banal internet user BMJ's Avatar
    Join Date
    Aug 2002
    Location
    Chicagoland
    Posts
    1,380
    You probably have not correctly set up a proper seed.

  6. #6
    Student drdroid's Avatar
    Join Date
    Feb 2002
    Location
    Montreal, Quebec
    Posts
    669

    ?

    what do you mean by a proper seed?

  7. #7
    BMJ
    BMJ is offline
    Banal internet user BMJ's Avatar
    Join Date
    Aug 2002
    Location
    Chicagoland
    Posts
    1,380
    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.
    Code:
    #include <time.h>
    #include <stdlib.h>
    
    srand(time(0));
    int MyRandomNum = int((double(rand()))/(double(RAND_MAX+1))/double(EXCLUSIVE_BOUNDARY));
    Many will tell you to do
    Code:
    MyRandomNum = rand()%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

    EDIT: Whoops, small code error... all better
    Last edited by BMJ; 10-09-2002 at 04:17 PM.

  8. #8
    Student drdroid's Avatar
    Join Date
    Feb 2002
    Location
    Montreal, Quebec
    Posts
    669

    ...

    so your saying rand=not truely random?

  9. #9
    BMJ
    BMJ is offline
    Banal internet user BMJ's Avatar
    Join Date
    Aug 2002
    Location
    Chicagoland
    Posts
    1,380
    ... well, to be exact; the return value of rand() is not a truly random number

  10. #10
    Student drdroid's Avatar
    Join Date
    Feb 2002
    Location
    Montreal, Quebec
    Posts
    669

    ...

    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();
    	}
    }

  11. #11
    Registered User
    Join Date
    Sep 2002
    Posts
    70
    use srand((unsigned)time(0)); before your first call to rand();

  12. #12
    Registered User Azuth's Avatar
    Join Date
    Feb 2002
    Posts
    236
    so your saying rand=not truely random?
    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.
    Demonographic rhinology is not the only possible outcome, but why take the chance

  13. #13
    BMJ
    BMJ is offline
    Banal internet user BMJ's Avatar
    Join Date
    Aug 2002
    Location
    Chicagoland
    Posts
    1,380
    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.
    Ooh, I like that page; they promote the formula for getting better non-biased towards high-bit numbers Although I said the same thing.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with game code.
    By ajdspud in forum C++ Programming
    Replies: 5
    Last Post: 02-14-2006, 05:39 PM
  2. Weird problem on '02 3.4L V6 auto
    By VirtualAce in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 01-11-2006, 11:05 PM
  3. Replies: 5
    Last Post: 11-07-2005, 10:34 PM
  4. searching problem
    By DaMenge in forum C Programming
    Replies: 9
    Last Post: 09-12-2005, 01:04 AM
  5. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM

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