Archived

This topic is now archived and is closed to further replies.

Dko

I hate char arrays >_<

Recommended Posts

ok ive got something like this. void setName(char Name) {itsName = Name;}; and I get this error c:\my documents\c++ programs\mybattle\main.cpp(166) : error C2664: 'setName' : cannot convert parameter 1 from 'char [8]' to 'char' This conversion requires a reinterpret_cast, a C-style cast or function-style cast I beleave it has something to do with how you declair the char arrays or what not but my book is not helping at all. Please help, thanks (gawd this has gota be my millionth question ) [edited by - Dko on January 24, 2004 5:26:42 PM]

Share this post


Link to post
Share on other sites
You''re also going to have to treat them as dynamic arrays since you don''t know how big the person''s name is. I''m still trying to figure this out myself, but look up calloc and realloc in your book or online. Those should be what you''re looking for to set the array up to be the right length whenever you need to change the name.

Share this post


Link to post
Share on other sites
Gday,

You might want to look at the string class in the std namespace, it can help with general string options a lot.

you can declare like this

std::string Name = "Freak";

or if you have using namespace std; before it than you can just call string Name = "Freak";

that way you are not using and memory copy functions and all that stuff that is really a pain...

Wait until you need to convert char arrays to unicode, then you will get nose bleeds.

if you are using char though then here a few handy hints.

You cna declare like this,

char * Name = "jack";
char Name[]="jack";

look up functions like strcpy, strcat, itoa and so forth.

good luck

Share this post


Link to post
Share on other sites
Still can''t figure it out. Here''s my character.h and the line that generates the error in main.cpp


#include <iostream>
#include <stdlib.h>

using namespace std;


class Creature
{
public:
void setName(char Name) {itsName = Name;};
void setHp(int Hp) {
itsHp = Hp;
MaxHp = itsHp;
};
void changeHp(int Hp) {itsHp = Hp;};
void setAtk(int Atk) {itsAtk = Atk;};
void setDef(int Def) {itsDef = Def;};
void setSpd(float Spd) {itsSpd = Spd;};
void setATB(float ATB) {itsATB = ATB;};

int CalculateATB() {
this->setATB(this->itsATB + (96 * (this->showSpd() + 20)) / 16); // My riped off ff3/6 formule :P

if (this->itsATB >= 65535)
return 1;
return 0;
};
int EnterName() {
cout << "Please enter your characters name\n";
char nameTemp ;
cin >> nameTemp;
this->setName(nameTemp);
};
char showName() {return itsName;};
int showHp() {return itsHp;};
int showMaxHp() {return MaxHp;};
int showAtk() {return itsAtk;};
int showDef() {return itsDef;};
float showSpd() {return itsSpd;};
int showATB() {
float NumOfBars = 10;
float interval = 65535 / NumOfBars;
float bars = itsATB / interval;
cout << "(";
for(int i = 0; i < bars; i++)
cout << "|";
for(float j = bars; j <= NumOfBars; j++)
cout << "-";
cout << ")";
return 0;

};

private:
char itsName;
int itsHp;
int MaxHp;
int itsAtk;
int itsDef;
float itsSpd;
float itsATB;
};



Monster.setName("Monster");

Share this post


Link to post
Share on other sites
Ok now the prob I get is this warrning
c:\my documents\c++ programs\mybattle\character.h(30) : warning C4700: local variable ''nameTemp'' used without having been initialized

and the program crashes when ever I enter a name

Share this post


Link to post
Share on other sites
I really hope you figured it out by now, but let me just give you code that works properly (at least with my trusty Dev-C++ compiler )


#include <iostream.h>

void setName(char newName[]);

int main()
{
char name[50];
cout << "name: ";
cin.getline(name, 50, ''\n''); //just gets the whole line in case name consists of more than one word


setName(name);

cout << endl;
system("pause"); //press any key to continue

return 0;
}

void setName(char newName[])
{
cout << "name: " << newName << endl;
}


As you might notice, I reiceve the parameter as a char array, not as a pointer or a char. A whole array (thus the []...)

Hope this helps...


...as I wiped her blood off my face

Share this post


Link to post
Share on other sites