int on end of a char array

This is a discussion on int on end of a char array within the C++ Programming forums, part of the General Programming Boards category; I'm trying to add an int to the end of an char array. I've tried it this way and well ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    20

    int on end of a char array

    I'm trying to add an int to the end of an char array.
    I've tried it this way and well I can't get it to work.
    Any help is apprecated!

    Code:
    char boss=(boss_beaten+1);//where boss_beaten is a int
    char helm[80]= "Helm level "boss;
    char plate[80]="Steel plate level " boss;
    char gloves[80]="Gloves level " boss;
    char boots[80]="Boots level " boss;
    char sword[80]="Sword level " boss;
    char shield[80]= "Shield level"  boss;

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,506
    Since it's c++, how about

    string helm = "Helm level " + boss;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    20
    Quote Originally Posted by Salem
    Since it's c++, how about

    string helm = "Helm level " + boss;
    Sorry man it still gives me a "Declaration syntax error"

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,662
    Salem's example assumes that you have made boss a std::string as well. If it has to be an int, you could convert the int to a string using say, a stringstream.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,506
    Or maybe even
    Code:
      char ch = '!';
      string message;
      message = "hello world";
      message += ch;
      cout << message << endl;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Nov 2005
    Posts
    85
    Maybe you can cast it?
    Code:
    char boss=((char)(boss_beaten+1));//where boss_beaten is a int

  7. #7
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,673
    >Maybe you can cast it?
    No. When you cast what was originally a bigger type down to a smaller type, you're bound to lose some data. The integer was maybe four bytes, and it can't necessarily smash everything into a single-byte character.

  8. #8
    Registered User
    Join Date
    May 2006
    Posts
    20
    Code:
    item item_list[6]=
    {"Helm", 50*(boss_beaten+1), 0, 25*(boss_beaten+1), -5*(boss_beaten+1),-5*(boss_beaten+1), 0,
    "Steel plate", 100*(boss_beaten+1), 0, 50*(boss_beaten+1), -10*(boss_beaten+1),-10*(boss_beaten+1), 1*(boss_beaten+1),
    "Gloves", 25*(boss_beaten+1), 0, 0, 5*(boss_beaten+1), 0, 2*(boss_beaten+1),
    "Boots", 40*(boss_beaten+1), 0, 0, 3*(boss_beaten+1), 5*(boss_beaten+1), 3*(boss_beaten+1),
    "Sword", 100*(boss_beaten+1), 8*(boss_beaten+1), 0, 0, 0, 4*(boss_beaten+1),
    "Shield", 30*(boss_beaten+1), 0, 13*(boss_beaten+1), 0, 0, 5*(boss_beaten+1)};
    
    item_list[0].name= item_list[0].name + (boss_beaten+1);  // .name is a char[80]
    ok ive changed the declaration but the compiler gives me a "Lvalue required" error
    Last edited by Whizza; 05-22-2006 at 05:22 AM. Reason: error copying code

  9. #9
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    What do you mean "add an int"? If you simply mean appending a string containing a number then stringstreams are the proper C++ way to go:
    Code:
    int Level = 4;
    float Str = 43.5f;
    std::stringstream Stream;
    
    Stream << "Sword lvl " << Level << " (str: " << Str << ")";
    
    std::string String = Stream.str();
    const char* CString = String.c_str();
    const char* CString2 = Stream.str().c_str();
    ...will produce: "Sword lvl 4 (str: 43.5)"
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  10. #10
    Registered User
    Join Date
    May 2006
    Posts
    41
    Hey,
    Looks like u want to use C stuff rather than C++
    well, if u don't want to use Strings then try this:


    Code:
    char ch[20] = "somestring";
    
    	int buf_len = 2;
    	
    	sprintf(ch,"%s%d",ch,buf_len);
    Note: u have to include stdio.h

  11. #11
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,506
    > item_list[0].name= item_list[0].name + (boss_beaten+1); // .name is a char[80]
    Well make it a std::string then.

    Or use say sprintf() to a temporary array to construct a new string, then copy that temporary back to item_list[0].name

    > sprintf(ch,"%s%d",ch,buf_len);
    This kind of thing is undefined - that is, using the output buffer of sprintf() as one of the input parameters.
    Very few of the standard C library functions work well on overlapping data.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  12. #12
    Registered User
    Join Date
    May 2006
    Posts
    20
    ok thx for your help guys but
    I have decided to skirt around the problem

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C problem with legacy code
    By andy_baptiste in forum C Programming
    Replies: 4
    Last Post: 05-19-2008, 06:14 AM
  2. Replies: 8
    Last Post: 03-10-2008, 11:57 AM
  3. Replies: 3
    Last Post: 05-13-2007, 08:55 AM
  4. Replies: 4
    Last Post: 11-23-2003, 06:15 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM

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