Little help?

This is a discussion on Little help? within the C++ Programming forums, part of the General Programming Boards category; Code: invalid operands of types `const char[12]' and `char[200]' to binary `operator+' --- on # line (filename = "characters\" + ...

  1. #16
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Code:
     invalid operands of types `const char[12]' and `char[200]' to binary `operator+' 
    
    --- on # line
    (filename = "characters\\" + cfullname +".html";)
    You need to use strcat for char arrays, or strings:
    Code:
    (filename = string("characters\\") + cfullname + string(".html");
    or
    Code:
    filename = "characters";
    filename += cfullname;
    filename += ".html";
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  2. #17
    Registered User
    Join Date
    May 2006
    Posts
    9
    Thanks that worked perfectly! i know i think i'm being a bother... and i hate to ask this (hopefully) last question.. as i was continuing on with the program, while waiting for replies i came up with another problem... (( it would seem every 30 lines i run into another problem... ))

    I'm back at the dice rolls that were random between 10 and 20... based off the rolls and why i needed them into variables.. depending on the roll i want to add to it.. in a string like:
    if roll is a 10 i want to append 10-0 and thats how it would look on the html file and 11 and 12 ... -1 11-1//12-1 ect all the way up to 20-5

    so if the rolls are
    12
    14
    11
    20
    19
    16
    10

    the output would read

    12-1
    14-2
    11-1
    20-5
    19-5
    16-3
    10-0

    i tried a few pssobilties, but i keep getting the erros cannont covert bewtween char and int.. the char being '-' also those -3, -4 i want to be added like the above, the some of those is '17'... any ideas on this part..

    (( i hope this is the last thing wrong with the code...... I'm fairly new to c++ i have worked with it before.. but this is something my friend requested...
    i do thank you all for the help.))

  3. #18
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,669
    I just have a question. Why is it important to format the dice roll this way? Is the second number random like the first? I've not seen a 20-sided die before.

  4. #19
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    Quote Originally Posted by citizen
    I just have a question. Why is it important to format the dice roll this way? Is the second number random like the first? I've not seen a 20-sided die before.
    You've obviously not played enough role playing games

  5. #20
    Registered User
    Join Date
    May 2006
    Posts
    9
    its just the way we play... it adds to armor... and no that number is not random its acorrding to the first random number if its 10 -0 is appendded to it..
    10-0
    11 12 -1
    13 14 -2
    15 16 -3
    17 18 -4
    19 20 -5

    like that...

  6. #21
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,669
    well here's what I would do. Not sure if you'd like it, but Prelude taught me how to deal with random numbers. You should probably learn about what I'm doing.
    Code:
    #include <iostream>
    #include <cstdlib>
    
    using namespace std;
    
    // This function helps seed rand() more properly
    unsigned time_seed(void)
    {
      time_t now = time ( 0 );
      unsigned char *p = (unsigned char *)&now;
      unsigned seed = 0;
      size_t i;
    
      for ( i = 0; i < sizeof now; i++ )
        seed = seed * ( UCHAR_MAX + 2U ) + p[i];
    
      return seed;
    }
    
    // This function just makes sure that we don't make other rolls 
    // occur more common than others
    double uniform_distribution (int seed)
    {
       return seed * 1. / (RAND_MAX + 1.);
    }
    
    int main(void) 
    {
       const char *die[10] = 
       { "10-0", "11-1", "12-1", "13-2", "14-2", "15-3", 
         "16-3", "17-4", "18-4", "19-5", "20-5" };
    
       srand(time_seed());
    
    // Now, the dice roll still works the same, except you are working 
    // within the bounds of a string array.
       int r =  uniform_distribution(rand()) * 10;
       cout << die[r];
    
       cin.get();
    
       return 0;
    }
    Last edited by whiteflags; 05-04-2006 at 11:22 PM.

  7. #22
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    There's a much simpler way which doesn't involve translating the dice roll to a string - look at this psuedo-program:
    Code:
    int main ()
    {    
        int x = // Some number from 10 to 20;
        x = (x-9)/2;
        cout << x;
    }
    Last edited by Bench82; 05-05-2006 at 08:02 AM.

  8. #23
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,669
    Right Bench! Wow, that was neat... of course there was nothing wrong with my solution, it just looks complex because that's how I learned random numbers. My only criticism is that you overwrote the value for x. Shouldn't it be?
    Code:
    int die = 10 + uniform_distribution(rand()) * (10-20); // some random number 10 to 20;
    cout << die << "-" << (die-9)/2 << "\n";
    Last edited by whiteflags; 05-05-2006 at 09:29 AM.

  9. #24
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    Quote Originally Posted by citizen
    . My only criticism is that you overwrote the value for x. Shouldn't it be?
    Yep, you're right, I should have stored the result in another variable instead of overwriting x. my arithmetic is sometimes better than my programming

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

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