viewing a text string for a member of a class....

This is a discussion on viewing a text string for a member of a class.... within the C++ Programming forums, part of the General Programming Boards category; ok... I have a class called cMob. I have members of the class ie cMob troll.... I also have a ...

  1. #1
    plzduntlakliekthiskthx
    Join Date
    Oct 2002
    Posts
    138

    viewing a text string for a member of a class....

    ok... I have a class called cMob. I have members of the class ie cMob troll.... I also have a member called currentMob. For instance, if the monster the user is fighting is a troll, then currentMob = troll... I need to display the text "troll". Anyone know how I can do this? Here is all the sourcecode for my monsters.....

    Code:
    //class of monsters
    class cMob
    {
    public:
    	int attack;
    	int itsAttackPower;
    	int itsHP;
    	char itsName;
    };              
    
    // All this monster stuff is in a function call fight()
                    //Monsters
    	cMob currentMob;
    	cMob rabbit;
    	cMob rat;
    	cMob sTroll;
    	cMob lTroll;
    	cMob dragon;
    	cMob boss1;
    	//monsters damages
    	rabbit.itsAttackPower= 1;
    	rat.itsAttackPower = 3;
    	sTroll.itsAttackPower = 6;
    	lTroll.itsAttackPower = 8;
    	dragon.itsAttackPower = 12;
    	boss1.itsAttackPower = 30;
    	//monsters Health
    	rabbit.itsHP = 4;
    	rat.itsHP = 7;
    	sTroll.itsHP = 15;
    	lTroll.itsHP = 25;
    	dragon.itsHP = 40;
    	boss1.itsHP = 80;
    Last edited by o0obruceleeo0o; 04-13-2003 at 10:46 AM.

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    a) It's name should be either a char array or a std::string.

    b) std::cout<<currentMob.itsName<<std::endl;
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    plzduntlakliekthiskthx
    Join Date
    Oct 2002
    Posts
    138
    I did this...
    Code:
    //monsters name
    rabbit.itsName = "Rabbit";
    rat.itsName = "Rat";
    sTroll.itsName = "Small Troll";
    lTroll.itsName = "Large Troll";
    dragon.itsName = "Dragon";
    boss1.itsName = "The Boss";
    and I got these errors...


    --------------------Configuration: rpgfight - Win32 Debug--------------------
    Compiling...
    rpgfight.cpp
    C:\PROGRAM FILES\PROGRAMMING\MICROSOFT VISUAL STUDIO\MYPROJECTS\rpgfight\rpgfight.cpp(104) : error C2440: '=' : cannot convert from 'char [7]' to 'char'
    This conversion requires a reinterpret_cast, a C-style cast or function-style cast
    C:\PROGRAM FILES\PROGRAMMING\MICROSOFT VISUAL STUDIO\MYPROJECTS\rpgfight\rpgfight.cpp(105) : error C2440: '=' : cannot convert from 'char [4]' to 'char'
    This conversion requires a reinterpret_cast, a C-style cast or function-style cast
    C:\PROGRAM FILES\PROGRAMMING\MICROSOFT VISUAL STUDIO\MYPROJECTS\rpgfight\rpgfight.cpp(106) : error C2440: '=' : cannot convert from 'char [12]' to 'char'
    This conversion requires a reinterpret_cast, a C-style cast or function-style cast
    C:\PROGRAM FILES\PROGRAMMING\MICROSOFT VISUAL STUDIO\MYPROJECTS\rpgfight\rpgfight.cpp(107) : error C2440: '=' : cannot convert from 'char [12]' to 'char'
    This conversion requires a reinterpret_cast, a C-style cast or function-style cast
    C:\PROGRAM FILES\PROGRAMMING\MICROSOFT VISUAL STUDIO\MYPROJECTS\rpgfight\rpgfight.cpp(108) : error C2440: '=' : cannot convert from 'char [7]' to 'char'
    This conversion requires a reinterpret_cast, a C-style cast or function-style cast
    C:\PROGRAM FILES\PROGRAMMING\MICROSOFT VISUAL STUDIO\MYPROJECTS\rpgfight\rpgfight.cpp(109) : error C2440: '=' : cannot convert from 'char [9]' to 'char'
    This conversion requires a reinterpret_cast, a C-style cast or function-style cast
    Error executing cl.exe.

    rpgfight.exe - 6 error(s), 0 warning(s)

  4. #4
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    See my first point.

    Also, if you do end up using char arrays, you have to use strcpy to copy a string to them.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  5. #5
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    either do this:
    Code:
    char itsName[50];
    or
    Code:
    std::string itsName;
    you can stick any number in instead of fifty, as long as it allows for enough space for the longest name + 1;

  6. #6
    plzduntlakliekthiskthx
    Join Date
    Oct 2002
    Posts
    138
    ahhh I see! I changed it from char to string itsName; (am using namepace std) Thanks for the help guys, I really appreciate it!

  7. #7
    plzduntlakliekthiskthx
    Join Date
    Oct 2002
    Posts
    138
    I did this to display the name...
    Code:
    cout << "Select a weapon to use:" << setw(40) << string (currentMob.itsName) << endl << setw(67) << string (currentMob.itsHP) << endl << endl
    and getting these errors

    --------------------Configuration: rpgfight - Win32 Debug--------------------
    Compiling...
    rpgfight.cpp
    C:\PROGRAM FILES\PROGRAMMING\MICROSOFT VISUAL STUDIO\MYPROJECTS\rpgfight\rpgfight.cpp(254) : error C2065: 'currentMob' : undeclared identifier
    C:\PROGRAM FILES\PROGRAMMING\MICROSOFT VISUAL STUDIO\MYPROJECTS\rpgfight\rpgfight.cpp(254) : error C2228: left of '.itsName' must have class/struct/union type
    C:\PROGRAM FILES\PROGRAMMING\MICROSOFT VISUAL STUDIO\MYPROJECTS\rpgfight\rpgfight.cpp(254) : error C2228: left of '.itsHP' must have class/struct/union type
    Error executing cl.exe.

    rpgfight.exe - 3 error(s), 0 warning(s)

    sorry for my ignorance, this is my first time making a program this advanced... (this is hard for me )

  8. #8
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    That means that the variable currentMob is not in scope. Could you post some more code please?
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  9. #9
    plzduntlakliekthiskthx
    Join Date
    Oct 2002
    Posts
    138
    here is the whole program :
    Code:
    //rpgfight.cpp
    //An RPG-Style fighting game....
    //If you have any comments or suggestions please email me at o0obruceleeo0o@hotmail.com
    
    #include <iostream>
    #include <cstdlib>
    #include <iomanip>
    #include <cstring>
    
    using namespace std;
    
    //declare functions and variables...
    void menu();
    void yourStatus();
    void buyPotions();
    void buyArmor();
    void buyWeapons();
    void fight();
    void usePotion();
    void attack();
    void getName();
    char fightSelect;
    char menuSelect;
    int mobsKilled = 0;
    
    
    
    //class for the user
    class you
    {
    public:
    	int yourHP;
    	
    };
    
    //class of monsters
    class cMob
    {
    public:
    	int attack;
    	int itsAttackPower;
    	int itsHP;
    	string itsName;
    };
    
    
    
    //class of weapons
    class cWeapons
    {
    public:
    	int itsDmg;
    
    };
    
    //Use Potion *************************************
    void usePotion()
    {
    	system("CLS");
    }
    
    
    
    //FIGHT ******************************************
    void fight()
    {
    	system("CLS");
    
    	//Monsters
    	cMob currentMob;
    	cMob rabbit;
    	cMob rat;
    	cMob sTroll;
    	cMob lTroll;
    	cMob dragon;
    	cMob boss1;
    	//monsters damages
    	rabbit.itsAttackPower= 1;
    	rat.itsAttackPower = 3;
    	sTroll.itsAttackPower = 6;
    	lTroll.itsAttackPower = 8;
    	dragon.itsAttackPower = 12;
    	boss1.itsAttackPower = 30;
    	//monsters Health
    	rabbit.itsHP = 4;
    	rat.itsHP = 7;
    	sTroll.itsHP = 15;
    	lTroll.itsHP = 25;
    	dragon.itsHP = 40;
    	boss1.itsHP = 80;
    	//monsters name
    	currentMob.itsName;
    	rabbit.itsName = "Rabbit";
    	rat.itsName = "Rat";
    	sTroll.itsName = "Small Troll";
    	lTroll.itsName = "Large Troll";
    	dragon.itsName = "Dragon";
    	boss1.itsName = "The Boss";
    	
    	//declare weapons
    	cWeapons fists;
    	cWeapons butterKnife;
        cWeapons dagger;
    	cWeapons shortsword;
    	cWeapons longblade;
    	cWeapons mythrilLongblade;
    	cWeapons bow;
    	cWeapons crossbow;
    	//weapons damages
    	fists.itsDmg = 1;
    	butterKnife.itsDmg = 2; 
    	dagger.itsDmg = 4;
    	shortsword.itsDmg = 8;
    	longblade.itsDmg = 15;
    	mythrilLongblade.itsDmg = 25;
    	bow.itsDmg = 10;
    	crossbow.itsDmg = 10;
    	
    	//Statements to determine which monster the user fights....
    	if(mobsKilled < 3)
    	{
    		currentMob=rabbit;
    	}
    	else if(mobsKilled > 3 && mobsKilled < 6)
    	{
    		currentMob=rat;
    	}
    	else if(mobsKilled > 6 && mobsKilled < 9)
    	{
    		currentMob=sTroll;
    
    	}
    	else if(mobsKilled > 9 && mobsKilled < 12)
    	{
    		currentMob=lTroll;
    	}
    	else if(mobsKilled > 12 && mobsKilled < 15)
    	{
    		currentMob=dragon;
    	}
    	else if(mobsKilled > 15 && mobsKilled < 18)
    	{
    		currentMob=boss1;
    	}
    	
    
    		
    		
    	cout << "A: Attack" << setw(40) << "Monster" << endl
    		<< "P: Use Potion" << setw(40) << "Monster HP:" << endl 
             << "R: Run" << endl << endl
    		 << "Enter the appropriate letter:";
    	cin >> fightSelect;
    	
    	switch(fightSelect)
    	{
    	case 'a': attack();
    		break;
    	case 'A': attack();
    		break;
    	case 'p': usePotion();
    		break;
    	case 'P': usePotion();
    		break;
    	case 'r': menu();
    		break;
    	case 'R': menu();
    		break;
    	default: cout << "Please enter a valid input:";
    		break;
    	};
        
        
    	//(rand()%10);
    
    }
    
    //Buy Weapon ******************************************
    void buyWeapons()
    {
    	system("CLS");
    	cout << "Buy Weapons" << endl << endl;
    }
    
    //Buy Armor ******************************************
    void buyArmor()
    {
    	system("CLS");
    	cout << "Buy Armor" << endl << endl;
    }
    
    //Buy Potions ******************************************
    void buyPotions()
    {
    	system("CLS");
    	cout << "Buy Potions" << endl << endl;
    }
    
    //Your Status ******************************************
    void yourStatus()
    {
    	system("CLS");
    	cout << "Your Status" << endl << endl;
    }
    
    //MENU *******************************************
    void menu()
    {
    	system("CLS");
    	cout << "F: Fight"
    		 << endl;
    	cout << "W: Buy Weapos"
    		 << endl;
    	cout << "A: Buy Armor"
    		 << endl;
    	cout << "P: Buy Potions"
    	     << endl;
    	cout << "S: Your Status"
    		 << endl << endl;
    	cout << "Enter the appropriate letter:";
    	cin  >> menuSelect;
    	switch(menuSelect)
    	{
    		case 'f': fight();
    			break;
    		case 'w': buyWeapons(); 
    			break;
    		case 'a': buyArmor();
    			break;
    		case 'p': buyPotions();
    			break;
    		case 's': yourStatus(); 
    			break;
    		case 'F': fight();
    			break;
    		case 'W': buyWeapons();
    			break;
    		case 'A': buyArmor();
    			break;
    		case 'P': buyPotions();
    			break;
    		case 'S': yourStatus();
    			break;
    		default: cout << "Please enter a valid input:";
    			break;
    	}
    		
    }
    
    //Attack *****************************************
    void attack()
    {
    	
    	system("CLS");
    	cout << "Select a weapon to use:" << setw(40) << string (currentMob.itsName) << endl << setw(67) << string (currentMob.itsHP) << endl << endl
    		 << "F: Your Fists" << endl
    		 << "B: Butter Knife" << endl
    		 << "D: Dagger" << endl
    		 << "S: Shortsword" << endl
    		 << "L: Longblade" << endl
    		 << "M: Mythril Longblade" << endl
    		 << "B: Bow" << endl
    		 << "C: Crossbow" << endl;
    }
    
    
    //MAIN *******************************************
    int main()
    {
    	
    	menu();
    	
    
    	
    	
    
    
    	return 0;
    }

  10. #10
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    The enemies are only defined in the fight function, so they aren't available in the attack function. It would probably be easiest to make them global, and to initalize their values at the start of the program.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  11. #11
    plzduntlakliekthiskthx
    Join Date
    Oct 2002
    Posts
    138
    I tried that but I got a ton of errors... Any tips on where they should go? I just put em up with all my function/variable declarations

  12. #12
    plzduntlakliekthiskthx
    Join Date
    Oct 2002
    Posts
    138
    ok I got rid of those errors... But now I got another ....

    --------------------Configuration: rpgfight - Win32 Debug--------------------
    Compiling...
    rpgfight.cpp
    C:\PROGRAM FILES\PROGRAMMING\MICROSOFT VISUAL STUDIO\MYPROJECTS\rpgfight\rpgfight.cpp(275) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class st
    d::allocator<char> >' (or there is no acceptable conversion)
    Error executing cl.exe.

    rpgfight.exe - 1 error(s), 0 warning(s)


    I labeled the line with the error using //comment... (the line with the cout statement....)


    Code:
    //Attack *****************************************
    void attack()
    {
    	string mobName = currentMob.itsName;
    	int mobHP = currentMob.itsHP;
    	system("CLS"); //Clear screen
    	SetConsoleTextAttribute ( h, FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED); //Sets the new text color
    
    	cout << "Select a weapon to use:" << setw(40) << mobName << endl << setw(67) << mobHP << endl << endl //ERROR IS ON THIS LINE
    		 << "F: Your Fists" << endl
    		 << "B: Butter Knife" << endl
    		 << "D: Dagger" << endl
    		 << "S: Shortsword" << endl
    		 << "L: Longblade" << endl
    		 << "M: Mythril Longblade" << endl
    		 << "B: Bow" << endl
    		 << "C: Crossbow" << endl;
    }
    thank you guys so much for your help!

  13. #13
    Registered User
    Join Date
    Feb 2003
    Posts
    162
    Code:
    //Attack *****************************************
    void attack()
    {
    	char mobName[40];
                    strcpy (mobName, currentMob.itsName);
    	int mobHP = currentMob.itsHP;
    	system("CLS"); //Clear screen
    	SetConsoleTextAttribute ( h, FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED); //Sets the new text color
    
    	cout << "Select a weapon to use:" << setw(40) << mobName << endl << setw(67) << mobHP << endl << endl
    		 << "F: Your Fists" << endl
    		 << "B: Butter Knife" << endl
    		 << "D: Dagger" << endl
    		 << "S: Shortsword" << endl
    		 << "L: Longblade" << endl
    		 << "M: Mythril Longblade" << endl
    		 << "B: Bow" << endl
    		 << "C: Crossbow" << endl;
    }

  14. #14
    plzduntlakliekthiskthx
    Join Date
    Oct 2002
    Posts
    138
    Originally posted by Jamsan
    Code:
    //Attack *****************************************
    void attack()
    {
    	char mobName[40];
                    strcpy (mobName, currentMob.itsName);
    	int mobHP = currentMob.itsHP;
    	system("CLS"); //Clear screen
    	SetConsoleTextAttribute ( h, FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED); //Sets the new text color
    
    	cout << "Select a weapon to use:" << setw(40) << mobName << endl << setw(67) << mobHP << endl << endl
    		 << "F: Your Fists" << endl
    		 << "B: Butter Knife" << endl
    		 << "D: Dagger" << endl
    		 << "S: Shortsword" << endl
    		 << "L: Longblade" << endl
    		 << "M: Mythril Longblade" << endl
    		 << "B: Bow" << endl
    		 << "C: Crossbow" << endl;
    }
    I put that in there, and got this error on the line with strcpy :
    --------------------Configuration: rpgfight - Win32 Debug--------------------
    Compiling...
    rpgfight.cpp
    C:\PROGRAM FILES\PROGRAMMING\MICROSOFT VISUAL STUDIO\MYPROJECTS\rpgfight\rpgfight.cpp(271) : error C2664: 'strcpy' : cannot convert parameter 2 from 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' to 'const
    char *'
    No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
    Error executing cl.exe.

    rpgfight.exe - 1 error(s), 0 warning(s)



    ty for reply!!!!

  15. #15
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Either make mobName a std::string, or use strcpy( mobName, currentMob.c_str( ) );
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  3. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  4. A bunch of Linker Errors...
    By Junior89 in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2006, 01:59 PM
  5. Ok, Structs, I need help I am not familiar with them
    By incognito in forum C++ Programming
    Replies: 7
    Last Post: 06-29-2002, 09:45 PM

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