Char Variable Probelm

This is a discussion on Char Variable Probelm within the C++ Programming forums, part of the General Programming Boards category; I'm new to char variables, and they're quite a bit different from int variables. Code: bool NewWeap1 = false; bool ...

  1. #1
    People Love Me
    Join Date
    Jan 2003
    Posts
    412

    Angry Char Variable Probelm

    I'm new to char variables, and they're quite a bit different from int variables.

    Code:
    bool NewWeap1 = false;
    bool NewWeap2 = false;
    bool NewWeap3 = false;
    bool NewWeap4 = false;
    bool NewWeap5 = false;
    char WeaponName[15] = "Shotgun";
    void CheckCondition()
    {
    	if (damage < pGuy->GetHP() / 3)
    	{
    		SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED);
    		MessageBox(0, "Condition Critical!", "Krak's RPG - Krak Productions, 2003", MB_OK);
    	}
    	else
    		SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY);
    	YourTurn();
    }
    
    void CheckWinning()
    {
    	if (damage >= pGuy->GetHP() / 2)
    		MessageBox (0, "You Defeated the enemy!", "Krak's RPG - Krak Productions, 2003", MB_OK);
    	else if ( damage >= pGuy->GetHP() / 3)
    		MessageBox (0, "You Defeated the enemy...barely!", "Krak's RPG - Krak Productions, 2003", MB_OK);
    }
    
    void  CheckUpgrades()
    {
    	if (pGuy->GetAtt() >= 25 && !NewWeap1)
    	{
    		MessageBox(0, "Weapon Evolved!!!", "Krak's RPG - Krak Productions, 2003", MB_OK);
    		MessageBox(0, "Shotgun became Blaster!", "Krak's RPG - Krak Productions, 2003", MB_OK);
    		MessageBox(0, "Attack power augmented!", "Krak's RPG - Krak Productions, 2003", MB_OK);
    		WeaponName[15] = "Blaster";
    		pGuy->SetAtt(5);
    		NewWeap1 = true;
    	}
    	if (pGuy->GetAtt() >= 39 && !NewWeap2)
    	{
    		MessageBox(0, "Weapon Evolved!!!", "Krak's RPG - Krak Productions, 2003", MB_OK);
    		MessageBox(0, "Blaster became Avenger!", "Krak's RPG - Krak Productions, 2003", MB_OK);
    		MessageBox(0, "Attack power augmented!", "Krak's RPG - Krak Productions, 2003", MB_OK);
    		WeaponName[15] = "Avenger";
    		pGuy->SetAtt(12);
    		NewWeap2 = true;
    	}
    		if (pGuy->GetAtt() >= 52 && !NewWeap3)
    	{
    		MessageBox(0, "Weapon Evolved!!!", "Krak's RPG - Krak Productions, 2003", MB_OK);
    		MessageBox(0, "Attack power augmented!", "Krak's RPG - Krak Productions, 2003", MB_OK);
    		pGuy->SetAtt(18);
    		NewWeap3 = true;
    	}
    			if (pGuy->GetAtt() >= 74 && !NewWeap4)
    	{
    		MessageBox(0, "Weapon Evolved!!!", "Krak's RPG - Krak Productions, 2003", MB_OK);
    		MessageBox(0, "Avenger became Fallen Angel!", "Krak's RPG - Krak Productions, 2003", MB_OK);
    		MessageBox(0, "Attack power augmented!", "Krak's RPG - Krak Productions, 2003", MB_OK);
    		WeaponName[15] = "Fallen Angel";
    		pGuy->SetAtt(26);
    		NewWeap4 = true;
    	}
    				if (pGuy->GetAtt() >= 90 && !NewWeap5)
    	{
    		MessageBox(0, "Weapon Evolved!!!", "Krak's RPG - Krak Productions, 2003", MB_OK);
    		MessageBox(0, "Fallen Angel became Damien's Wrath!", "Krak's RPG - Krak Productions, 2003", MB_OK);
    		MessageBox(0, "Attack power augmented!", "Krak's RPG - Krak Productions, 2003", MB_OK);
    		WeaponName[15] = "Damien's Wrath";
    		pGuy->SetAtt(40);
    		NewWeap5 = true;
    	}
    }
    When compiled, I get the following errors:

    error C2440: '=' : cannot convert from 'char [8]' to 'char'
    This conversion requires a reinterpret_cast, a C-style cast or function-style cast
    error C2440: '=' : cannot convert from 'char [8]' to 'char'
    This conversion requires a reinterpret_cast, a C-style cast or function-style cast
    error C2440: '=' : cannot convert from 'char [13]' to 'char'
    This conversion requires a reinterpret_cast, a C-style cast or function-style cast
    error C2440: '=' : cannot convert from 'char [15]' to 'char'
    This conversion requires a reinterpret_cast, a C-style cast or function-style cast
    What gives?

  2. #2
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    >> WeaponName[15] = "Damien's Wrath";
    That won't work. You're trying to assign an entire string to memory the size of a char, which you don't even own.

    Read up on how arrays work.

    After that, use strcpy to assign one string to the other. strcpy(WeaponName, "Damien's Wrath")
    Or, use an object of the std::string type, then assignments like WeaponName = "Damien's Wrath"; will be valid.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  2. The Interactive Animation - my first released C program
    By ulillillia in forum A Brief History of Cprogramming.com
    Replies: 48
    Last Post: 05-10-2007, 03:25 AM
  3. Obtaining source & destination IP,details of ICMP Header & each of field of it ???
    By cromologic in forum Networking/Device Communication
    Replies: 1
    Last Post: 04-29-2006, 03:49 PM
  4. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 01:18 AM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM

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