-
Array
What's wrong with this array? The error is that it can't conver char to char in function main for each lname variable.
Code:
#include <fstream.h>
#include <conio.h>
#include <stdlib.h>
void main()
{
char lname[5];
lname[0]="T'pnon";
lname[1]="Nem'on";
lname[2]="K'nou";
lname[3]="Clau";
lname[4]="Tnon";
cout << lname[1];
getch();
}
Code tags fixed (they were off by a '/') by kermi3
-
You could change char lname[5] to char* lname[5], that should work. Otherwise you are attempting to assign what are essentially pointers to individual characters in the array. That's the problem with what you were trying to do.
-
ok...
now, that I got that working why when I try and display one of thoughs in another function why would it only display one letter, the first letter of every monster?
-
Use std::string and your problems will go away.
-
How are you trying to display these character strings?
-
...
Heres the code:
Code:
#include <iostream.h>
#include <stdlib.h>
#include <conio.h>
int battle(int hp, char monnm, int dmg)
{
cout << monnm;
return 0;
}
void main()
{
int lp=1;
while(lp!=0)
{
int hp=40;
int dmg=10;
char* monster[5];
monster[0]="Spider";
monster[1]="Goblin";
monster[2]="Troll";
monster[3]="Ogre";
monster[4]="Hound";
int monpick=rand()%5;
battle(hp,*monster[monpick],dmg);
getch();
}
}
-
?
what's wrong with this code? it only outputs the first letter of every monster?
-
int battle(int hp, char monnm, int dmg)
battle(hp,*monster[monpick],dmg);
Your battle function is asking for a char (a single character) and that is what you are correctly passing when you call it. It should be:
int battle(int hp,const char *monnm,int dmg)