Character Creation Problemo

This is a discussion on Character Creation Problemo within the C++ Programming forums, part of the General Programming Boards category; Anyone see what's wrong with this code? I really don't... I assume it's with the pointers, though. Because this is ...

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

    Question Character Creation Problemo

    Anyone see what's wrong with this code? I really don't... I assume it's with the pointers, though. Because this is only the second or third time i've used them.

    Code:
    #include <stdlib.h>
    #include <iostream.h>
    #include <conio.h>
    
    //Define Bool
    typedef int bool;
    #define true 1;
    #define false 0;
    
    //Stat Pointers
    int two=2;
    int zero=0;
    int *hp;
    int *xp;
    int *ac;
    int *dmg;
    char *nme;
    char *cls;
    char *rce;
    
    //Character Definition
    int chrdefine()
    {
    	//Name
       char name[100];
    	cout << "Name: ";
    	cin >> nme;
    	nme=name;
    
    	//Class
    	clrscr();
    	cout << "Class: " << endl;
    	cout << "1. Mage" << endl;
    	cout << "2. Fighter" << endl;
    	cout << "3. Monk" << endl;
    	cout << "Other Key To Quit" << endl;
    	char ch1=getch();
    	switch(ch1)
    	{
    		case 1:
    		cls="Mage";
    		break;
    
    		case 2:
    		cls="Fighter";
    		break;
    
    		case 3:
    		cls="Monk";
    		break;
    
    		default:
    		return 0;
    		break;
    	}
    
    
    	//Race
    	clrscr();
    	cout << "Race: " << endl;
    	cout << "1. Human" << endl;
    	cout << "2. Elf" << endl;
    	cout << "3. Halfling" << endl;
    	cout << "4. Half-Elf" << endl;
    	cout << "5. Half-Orc" << endl;
    	cout << "Other Key To Quit" << endl;
    	char ch2=getch();
    	switch(ch2)
    	{
    		case 1:
    		rce="Human";
    		break;
    
    		case 2:
    		rce="Elf";
    		break;
    
    		case 3:
    		rce="Halfling";
    		ac=&two;
    		break;
    
    		case 4:
    		rce="Half-Elf";
    		break;
    
    		case 5:
    		rce="Half-Orc";
    		break;
    
    		default:
    		return 0;
    		break;
    	}
    
    	//Xp
    	xp=&zero;
    
    	//Hp
    	int random=rand()%10;
    	random=random+11;
    	hp=&random;
    	return 0;
    }
    
    int main()
    {
    	chrdefine();
    	cout << "HP: " << *hp << endl;
    	cout << "Name: " << *nme << endl;
    	cout << "Race: " << *rce << endl;
    	cout << "Class: " << *cls << endl;
    	cout << "EXP: " << *xp << endl;
    	cout << "AC: +" << *ac << endl;
    	cout << "Damage: " << *dmg << endl;
    	return 0;
    }

    It runs the beginning of the character function fine, the inputing of the name. It never makes it to where you input your race. Then when it goes to output the data it shows... strange symbols.

  2. #2
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511
    I see somethings>>

    First a lot of mixed C/C++ code:
    Code:
    #include <stdlib.h>
    #include <iostream.h>
    #include <conio.h>
    you can change to:
    Code:
    #include <conio.h>
    #include <stdlib>
    #include <iostream>
    using namespace std;
    there is a boolean data type called bool

    so you do not need:

    Code:
    //Define Bool
    typedef int bool;
    #define true 1;
    #define false 0;
    Also, use C++ strings.
    Also, why are you using pointers here!!

    Mr. C.

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

    ...

    namespace doesn't work on borland v4.52 and neither does bool.

  4. #4
    Evil Member
    Join Date
    Jan 2002
    Posts
    638

    Then I would advise you to get a more comformant compiler...

    As Stroustrup said:

    "Using a feature-poor older implementation also warps the novice's programming style and gives a biased view of what C++ is."

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

    ...

    so I should change compilers to something more up to date?

  6. #6
    Evil Member
    Join Date
    Jan 2002
    Posts
    638

    Yeah

    For a Windows platform I would recommend either DJGPP (a nice, basic port of g++) or Dev-C++ (another compiler with a frilly visual interface and which can do Win32 apps)

    Both of which are free, btw

    DJGPP is 9x only tho, XP and such break it

  7. #7
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    cin >> nme;
    should be
    cin>>name;
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Code:
    char ch2=getch();
    switch(ch2)
    {
       case 1:
    <snip>
    This is a problem. getch() will return the ASCII value, so if the user hits 1 on the keyboard, the value of ch2 will be 0x31 (49), not 1. Also, the return is of type int not char.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  9. #9
    Student drdroid's Avatar
    Join Date
    Feb 2002
    Location
    Montreal, Quebec
    Posts
    669
    I'll look into dev... but the only problem is it doesn't have the gotoxy function. What could I use instead of that?

  10. #10
    . Driveway's Avatar
    Join Date
    May 2002
    Posts
    469
    You could read the FAQ
    Go all the way down to the bottom of that page

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 05:23 AM
  2. Character literals incorrectly interpreted
    By DL1 in forum C Programming
    Replies: 11
    Last Post: 04-05-2009, 06:35 PM
  3. <string> to LPCSTR? Also, character encoding: UNICODE vs ?
    By Kurisu33 in forum C++ Programming
    Replies: 7
    Last Post: 10-09-2006, 01:48 AM
  4. Remove character from string
    By nooksooncau in forum C Programming
    Replies: 11
    Last Post: 06-05-2006, 10:37 AM
  5. Character handling help
    By vandalay in forum C Programming
    Replies: 18
    Last Post: 03-29-2004, 05:32 PM

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