Thread: Problems With Char

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

    Problems With Char

    my 5 errors for monnm are all saying lvalue required for monnm. What's wrong with this? I just don't see the problem.

    Code:
    char monnm[30];
    	int monhp;
    	int monatt;
    	int monchs=rand()%5;
    	if(monchs==0)
    	{
    		monnm="Spider";
    		monhp=55;
    		monatt=15;
    	}
    	else if(monchs==1)
    	{
    		monnm="Serpent";
    		monhp=60;
    		monatt=25;
    	}
    	else if(monchs==2)
    	{
    		monnm="Orc";
    		monhp=50;
    		monatt=25;
    	}
    	else if(monchs==3)
    	{
    		monnm="Goblin";
    		monhp=40;
    		monatt=10;
    	}
    	else if(monchs==4)
    	{
    		monnm="Hound";
    		monhp=45;
    		monatt=25;
    	}

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    You cannot make an assignment to a entire array with a string literal outside of initialization.

    // This is wrong.
    char array[10];
    array = "Testing";

    // This is correct.
    #include <cstring>

    char array[10];
    strncpy ( array, "Testing", 10 );

    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Consider a switch solution.

    Code:
    switch (monchs)
    {
    case 0: { break; }
    ...
    }
    Kuphryn

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Code:
    
    
     /*...use with C99+ or C++ compatible compilers,
     otherwise, use a #define for "count"...*/
    const int count = 5;
    
    
    
    char names[count] = { { "Spider"  },
                          { "Serpent" },
                          { "Orc"     },
                          { "Goblin"  },
                          { "Hound"   } };
    int hp[count] = { 55, 60, 50, 40, 45 };
    
    int att[count] = { 15, 25, 25, 10, 25 };
    
    int main()
     {
        char *monnm[30];
        char monnm2[30][50];
    	int monhp;
    	int monatt;
    	int monchs;
    
      monchs = rand()%5;
    
      monnm[0] = names[monchs]; //...beware! you do not "own" that string!
      strcpy(monnm2[0], names[monchs]); //...but here you do...
      monatt = att[monchs];
      monhp = hp[monchs];
    
      return 0;
    }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  5. #5
    Registered User jawwadalam's Avatar
    Join Date
    May 2002
    Posts
    131
    You Should Use the strcpy() function for... assigning the Values... to the char array.
    One day you will ask what more important to you..
    I will say my life..
    and You will leave me with even knowing
    that
    You are my Life (L)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. convering char to int problems
    By ominub in forum C Programming
    Replies: 5
    Last Post: 04-01-2009, 12:02 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, 02:25 AM
  3. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  4. Need help understanding info in a header file
    By hicpics in forum C Programming
    Replies: 8
    Last Post: 12-02-2005, 12:36 PM
  5. comparing fields in a text file
    By darfader in forum C Programming
    Replies: 9
    Last Post: 08-22-2003, 08:21 AM