Array

This is a discussion on Array within the C++ Programming forums, part of the General Programming Boards category; What's wrong with this array? The error is that it can't conver char to char in function main for each ...

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

    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

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    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.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

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

    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?

  4. #4
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Use std::string and your problems will go away.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    How are you trying to display these character strings?
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

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

    ...

    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();
    	 }
    }

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

    ?

    what's wrong with this code? it only outputs the first letter of every monster?

  8. #8
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    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)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 08:25 PM
  2. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 08:31 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 08:48 PM

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