Invalid conversion from 'char' tp 'char*'

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tim Johansson

    Invalid conversion from 'char' tp 'char*'

    I'm new to C++, and tried to start making a script that will shuffle an
    array. Can someone please tell me what's wrong?

    #include <iostream.h>
    #include <string.h>

    int main () {
    srand(time(0));
    int array_length;
    int count;
    int randm;
    char temp[30];
    cout << "How many items in array? ";
    cin >> array_length;
    char items [30][array_length + 1];
    for (count = 0; count <= array_length; count++) {
    if (count != 0) {
    cout << "\nWhat shall item " << count << " be?\n\t";
    }
    cin.getline (items[count], 30);
    }
    for (count = 0; count < array_length; count++) {
    randm = rand() % array_length;
    strcpy (temp[30], items[30][count]);
    //ERROR
    strcpy (items[30][count], items[30][randm]); //ERROR
    strcpy (items[30][randm], temp[30]); //ERROR
    }
    return 0;
    }


    Can someone please tell me what is wrong? The errors I get are:
    21: error: invalid conversion from 'char' to 'char*'
    21: error: invalid conversion from 'char' to 'const char*'
    22: error: invalid conversion from 'char' to 'char*'
    22: error: invalid conversion from 'char' to 'const char*'
    23: error: invalid conversion from 'char' to 'char*'
    23: error: invalid conversion from 'char' to 'const char*'


  • Petec

    #2
    Re: Invalid conversion from 'char' tp 'char*'

    Tim Johansson wrote:[color=blue]
    > I'm new to C++, and tried to start making a script[/color]
    ^^^^^^
    FYI, C++ programs are not "scripts", they're programs.
    [color=blue]
    > that will shuffle
    > an array. Can someone please tell me what's wrong?
    >[/color]
    <snip>

    I'm not sure what was wrong (I couldn't understand it) but here's a little
    shuffle program:

    int main()
    {
    // a vector of strings to hold the items
    std::vector<std ::string> items;

    // get items
    std::cout << "Enter items, or blank to finish:" << std::endl;
    for(;;)
    {
    std::string item;
    std::getline(st d::cin, item);
    if(item == "")
    break;
    items.push_back (item);
    }


    std::cout << std::endl << "Shuffled items:" << std::endl;

    // use the random_shuffle algorith in <algorithm> to shuffe the sequence
    std::random_shu ffle(items.begi n(), items.end());

    // copy the shuffled vector to std::cout
    std::copy(items .begin(), items.end(),
    std::ostream_it erator<std::str ing>(std::cout, "\n"));
    }

    Also, I suggest you read the free & legal "Thinking in C++" book by Bruce
    Eckel, availible at the author's website, www.mindview.net.

    - Pete


    Comment

    • Petec

      #3
      Re: Invalid conversion from 'char' tp 'char*'

      Tim Johansson wrote:[color=blue]
      > I'm new to C++, and tried to start making a script[/color]
      ^^^^^^
      FYI, C++ programs are not "scripts", they're programs.
      [color=blue]
      > that will shuffle
      > an array. Can someone please tell me what's wrong?
      >[/color]
      <snip>

      I'm not sure what was wrong (I couldn't understand it) but here's a little
      shuffle program:

      int main()
      {
      // a vector of strings to hold the items
      std::vector<std ::string> items;

      // get items
      std::cout << "Enter items, or blank to finish:" << std::endl;
      for(;;)
      {
      std::string item;
      std::getline(st d::cin, item);
      if(item == "")
      break;
      items.push_back (item);
      }


      std::cout << std::endl << "Shuffled items:" << std::endl;

      // use the random_shuffle algorith in <algorithm> to shuffe the sequence
      std::random_shu ffle(items.begi n(), items.end());

      // copy the shuffled vector to std::cout
      std::copy(items .begin(), items.end(),
      std::ostream_it erator<std::str ing>(std::cout, "\n"));
      }

      Also, I suggest you read the free & legal "Thinking in C++" book by Bruce
      Eckel, availible at the author's website, www.mindview.net.

      - Pete


      Comment

      • Ragemare

        #4
        Re: Invalid conversion from 'char' tp 'char*'


        "Tim Johansson" <spam@gurka.s e> wrote in message
        news:c51l09$2no sfd$1@ID-224346.news.uni-berlin.de...[color=blue]
        > I'm new to C++, and tried to start making a script that will shuffle an
        > array. Can someone please tell me what's wrong?
        >
        > #include <iostream.h>
        > #include <string.h>
        >
        > int main () {
        > srand(time(0));
        > int array_length;
        > int count;
        > int randm;
        > char temp[30];
        > cout << "How many items in array? ";
        > cin >> array_length;
        > char items [30][array_length + 1];
        > for (count = 0; count <= array_length; count++) {
        > if (count != 0) {
        > cout << "\nWhat shall item " << count << " be?\n\t";
        > }
        > cin.getline (items[count], 30);
        > }
        > for (count = 0; count < array_length; count++) {
        > randm = rand() % array_length;
        > strcpy (temp[30], items[30][count]);
        > //ERROR
        > strcpy (items[30][count], items[30][randm]); //ERROR
        > strcpy (items[30][randm], temp[30]);[/color]
        //ERROR[color=blue]
        > }
        > return 0;
        > }
        >
        >
        > Can someone please tell me what is wrong? The errors I get are:
        > 21: error: invalid conversion from 'char' to 'char*'
        > 21: error: invalid conversion from 'char' to 'const char*'
        > 22: error: invalid conversion from 'char' to 'char*'
        > 22: error: invalid conversion from 'char' to 'const char*'
        > 23: error: invalid conversion from 'char' to 'char*'
        > 23: error: invalid conversion from 'char' to 'const char*'
        >
        >[/color]

        Not that I'm a expert but you appear to be trying to pass a single segment
        of the array instead of all of the array, get rid of the '[30]'. Also I
        don't think you can pass a two dimentional array to strcpy.


        Comment

        • Ragemare

          #5
          Re: Invalid conversion from 'char' tp 'char*'


          "Tim Johansson" <spam@gurka.s e> wrote in message
          news:c51l09$2no sfd$1@ID-224346.news.uni-berlin.de...[color=blue]
          > I'm new to C++, and tried to start making a script that will shuffle an
          > array. Can someone please tell me what's wrong?
          >
          > #include <iostream.h>
          > #include <string.h>
          >
          > int main () {
          > srand(time(0));
          > int array_length;
          > int count;
          > int randm;
          > char temp[30];
          > cout << "How many items in array? ";
          > cin >> array_length;
          > char items [30][array_length + 1];
          > for (count = 0; count <= array_length; count++) {
          > if (count != 0) {
          > cout << "\nWhat shall item " << count << " be?\n\t";
          > }
          > cin.getline (items[count], 30);
          > }
          > for (count = 0; count < array_length; count++) {
          > randm = rand() % array_length;
          > strcpy (temp[30], items[30][count]);
          > //ERROR
          > strcpy (items[30][count], items[30][randm]); //ERROR
          > strcpy (items[30][randm], temp[30]);[/color]
          //ERROR[color=blue]
          > }
          > return 0;
          > }
          >
          >
          > Can someone please tell me what is wrong? The errors I get are:
          > 21: error: invalid conversion from 'char' to 'char*'
          > 21: error: invalid conversion from 'char' to 'const char*'
          > 22: error: invalid conversion from 'char' to 'char*'
          > 22: error: invalid conversion from 'char' to 'const char*'
          > 23: error: invalid conversion from 'char' to 'char*'
          > 23: error: invalid conversion from 'char' to 'const char*'
          >
          >[/color]

          Not that I'm a expert but you appear to be trying to pass a single segment
          of the array instead of all of the array, get rid of the '[30]'. Also I
          don't think you can pass a two dimentional array to strcpy.


          Comment

          • Kevin Goodsell

            #6
            Re: Invalid conversion from 'char' tp 'char*'

            Tim Johansson wrote:
            [color=blue]
            > I'm new to C++, and tried to start making a script that will shuffle an
            > array. Can someone please tell me what's wrong?
            >
            > #include <iostream.h>[/color]

            This is not a standard header. Standard C++ uses <iostream>. This
            suggests that you are learning from a source that is either very
            outdated, inaccurate, or (most likely) both.
            [color=blue]
            > #include <string.h>[/color]

            This is standard, but deprecated. <cstring> is the replacement.
            [color=blue]
            >
            > int main () {
            > srand(time(0));[/color]

            Where did srand() and time() come from? You haven't #included the
            appropriate headers for these. Those headers are <cstdlib> and <ctime>,
            respectively. Alternatively, the old deprecated versions are <stdlib.h>
            and <time.h>.

            If you use the first, non-deprecated versions, the functions will be in
            namespace std::, and you need to account for that. So, for example, you
            could do this:

            std::srand(std: :time(0));

            Note also, that seeding the random number generator isn't always a good
            idea. You are debugging your program -- consistency is good during this
            phase. If you don't seed the random number generator (or seed it with
            some constant value, like 7), then you'll get the same sequence of
            numbers on each run, which makes debugging much easier.
            [color=blue]
            > int array_length;
            > int count;
            > int randm;
            > char temp[30];[/color]

            Consider using std::strings instead of char arrays. They are safer, more
            flexible, easier, and less error-prone.

            Don't declare everything up front. Declare things where you use them. It
            makes the code more clear, and someone reading the code does not need to
            search as much for the declaration of an object.
            [color=blue]
            > cout << "How many items in array? ";[/color]

            cout, cin, etc. are all in namespace std::, and you need to account for
            this somehow. So, for example, you could use this:

            std::cout << "some output" << std::endl;
            std::cin >> some_var;
            [color=blue]
            > cin >> array_length;[/color]

            What if this input operation fails? You should check for success. If it
            fails, array_length will have an undefined value. Using it for anything
            other than giving it a defined value would be bad.
            [color=blue]
            > char items [30][array_length + 1];[/color]

            This is illegal. Your compiler should reject it. If it doesn't, check
            the documentation about how to invoke it in standards-compliant mode.

            The problem is that array sizes must be compile-time constants.
            array_length + 1 is not known at compile-time.

            You should also prefer standard containers to arrays. std::vector is
            almost a drop-in replacement for arrays, and is more flexible and
            potentially safer.

            It appears that a good replacement for this would be

            std::vector<std ::string> items(array_len gth + 1);
            [color=blue]
            > for (count = 0; count <= array_length; count++) {[/color]

            Not a major issue, but prefer pre-increment to post-increment when the
            end result is the same. pre-inc may be faster, and is almost certainly
            not slower, than post-inc. This probably makes no difference with
            built-in types, but it's a good habit nonetheless.
            [color=blue]
            > if (count != 0) {
            > cout << "\nWhat shall item " << count << " be?\n\t";
            > }
            > cin.getline (items[count], 30);[/color]

            This is wrong. What happens when 'count' is 500? The first index of
            'items' only goes to 29, so you are potentially overflowing your array
            bounds and invoking undefined behavior. Also, 30 may be more characters
            than can fit in 'items[count]'. Basically, you reversed your indices in
            the declaration (which, as I mentioned, was illegal anyway because one
            of the sizes was not a compile-time constant).

            After fixing all this, you still need special handling for when the line
            is longer than your maximum string size.

            If you used std::strings instead of char arrays, you could do this:

            std::getline(it ems[count]);

            Easier, safer, almost impossible to get wrong, not limited to some
            arbitrary length.
            [color=blue]
            > }
            > for (count = 0; count < array_length; count++) {
            > randm = rand() % array_length;[/color]

            For various reasons, this is a poor way to limit rand()'s range. The
            comp.lang.c FAQ has information on this. But for simple cases, it rarely
            matters much.
            [color=blue]
            > strcpy (temp[30], items[30][count]);[/color]

            This is wrong. temp[30] doesn't exist, nor does
            items[30][any_value_here]; Array bounds in C++ go from 0 to array_size-1.

            Besides that, there is a type mismatch. strcpy() expects arguments of
            type char* (const char* in the case of the source string), and you are
            passing char. This is precisely what your error message says. You
            probably wanted something like:

            std::strcpy(tem p, items[count]);

            This assumes a few other changes in the code:

            1) The std:: qualification assumes #include <cstring> instead of
            #include <string.h>.

            2) items[count] is only valid if you reverse the indices in the
            declaration of items.

            Better still, get rid of the arrays and use vectors and strings like
            I've mentioned a few times:

            std::string temp;
            std::vector<std ::string> items;

            //...

            // No need for special functions when dealing with
            // std::string -- the normal operators work fine:
            temp = vector[count];
            [color=blue]
            > //ERROR
            > strcpy (items[30][count], items[30][randm]); //ERROR
            > strcpy (items[30][randm], temp[30]); //ERROR[/color]

            Same basic problems here.
            [color=blue]
            > }
            > return 0;
            > }
            >[/color]

            Finally, the standard library provide the function std::random_shu ffle()
            that does what you want.
            [color=blue]
            >
            > Can someone please tell me what is wrong? The errors I get are:
            > 21: error: invalid conversion from 'char' to 'char*'
            > 21: error: invalid conversion from 'char' to 'const char*'[/color]

            The errors could hardly be more clear. The function you are calling
            expects char* and const char* arguments, you passed char arguments.

            -Kevin
            --
            My email address is valid, but changes periodically.
            To contact me please use the address from a recent posting.

            Comment

            • Kevin Goodsell

              #7
              Re: Invalid conversion from 'char' tp 'char*'

              Tim Johansson wrote:
              [color=blue]
              > I'm new to C++, and tried to start making a script that will shuffle an
              > array. Can someone please tell me what's wrong?
              >
              > #include <iostream.h>[/color]

              This is not a standard header. Standard C++ uses <iostream>. This
              suggests that you are learning from a source that is either very
              outdated, inaccurate, or (most likely) both.
              [color=blue]
              > #include <string.h>[/color]

              This is standard, but deprecated. <cstring> is the replacement.
              [color=blue]
              >
              > int main () {
              > srand(time(0));[/color]

              Where did srand() and time() come from? You haven't #included the
              appropriate headers for these. Those headers are <cstdlib> and <ctime>,
              respectively. Alternatively, the old deprecated versions are <stdlib.h>
              and <time.h>.

              If you use the first, non-deprecated versions, the functions will be in
              namespace std::, and you need to account for that. So, for example, you
              could do this:

              std::srand(std: :time(0));

              Note also, that seeding the random number generator isn't always a good
              idea. You are debugging your program -- consistency is good during this
              phase. If you don't seed the random number generator (or seed it with
              some constant value, like 7), then you'll get the same sequence of
              numbers on each run, which makes debugging much easier.
              [color=blue]
              > int array_length;
              > int count;
              > int randm;
              > char temp[30];[/color]

              Consider using std::strings instead of char arrays. They are safer, more
              flexible, easier, and less error-prone.

              Don't declare everything up front. Declare things where you use them. It
              makes the code more clear, and someone reading the code does not need to
              search as much for the declaration of an object.
              [color=blue]
              > cout << "How many items in array? ";[/color]

              cout, cin, etc. are all in namespace std::, and you need to account for
              this somehow. So, for example, you could use this:

              std::cout << "some output" << std::endl;
              std::cin >> some_var;
              [color=blue]
              > cin >> array_length;[/color]

              What if this input operation fails? You should check for success. If it
              fails, array_length will have an undefined value. Using it for anything
              other than giving it a defined value would be bad.
              [color=blue]
              > char items [30][array_length + 1];[/color]

              This is illegal. Your compiler should reject it. If it doesn't, check
              the documentation about how to invoke it in standards-compliant mode.

              The problem is that array sizes must be compile-time constants.
              array_length + 1 is not known at compile-time.

              You should also prefer standard containers to arrays. std::vector is
              almost a drop-in replacement for arrays, and is more flexible and
              potentially safer.

              It appears that a good replacement for this would be

              std::vector<std ::string> items(array_len gth + 1);
              [color=blue]
              > for (count = 0; count <= array_length; count++) {[/color]

              Not a major issue, but prefer pre-increment to post-increment when the
              end result is the same. pre-inc may be faster, and is almost certainly
              not slower, than post-inc. This probably makes no difference with
              built-in types, but it's a good habit nonetheless.
              [color=blue]
              > if (count != 0) {
              > cout << "\nWhat shall item " << count << " be?\n\t";
              > }
              > cin.getline (items[count], 30);[/color]

              This is wrong. What happens when 'count' is 500? The first index of
              'items' only goes to 29, so you are potentially overflowing your array
              bounds and invoking undefined behavior. Also, 30 may be more characters
              than can fit in 'items[count]'. Basically, you reversed your indices in
              the declaration (which, as I mentioned, was illegal anyway because one
              of the sizes was not a compile-time constant).

              After fixing all this, you still need special handling for when the line
              is longer than your maximum string size.

              If you used std::strings instead of char arrays, you could do this:

              std::getline(it ems[count]);

              Easier, safer, almost impossible to get wrong, not limited to some
              arbitrary length.
              [color=blue]
              > }
              > for (count = 0; count < array_length; count++) {
              > randm = rand() % array_length;[/color]

              For various reasons, this is a poor way to limit rand()'s range. The
              comp.lang.c FAQ has information on this. But for simple cases, it rarely
              matters much.
              [color=blue]
              > strcpy (temp[30], items[30][count]);[/color]

              This is wrong. temp[30] doesn't exist, nor does
              items[30][any_value_here]; Array bounds in C++ go from 0 to array_size-1.

              Besides that, there is a type mismatch. strcpy() expects arguments of
              type char* (const char* in the case of the source string), and you are
              passing char. This is precisely what your error message says. You
              probably wanted something like:

              std::strcpy(tem p, items[count]);

              This assumes a few other changes in the code:

              1) The std:: qualification assumes #include <cstring> instead of
              #include <string.h>.

              2) items[count] is only valid if you reverse the indices in the
              declaration of items.

              Better still, get rid of the arrays and use vectors and strings like
              I've mentioned a few times:

              std::string temp;
              std::vector<std ::string> items;

              //...

              // No need for special functions when dealing with
              // std::string -- the normal operators work fine:
              temp = vector[count];
              [color=blue]
              > //ERROR
              > strcpy (items[30][count], items[30][randm]); //ERROR
              > strcpy (items[30][randm], temp[30]); //ERROR[/color]

              Same basic problems here.
              [color=blue]
              > }
              > return 0;
              > }
              >[/color]

              Finally, the standard library provide the function std::random_shu ffle()
              that does what you want.
              [color=blue]
              >
              > Can someone please tell me what is wrong? The errors I get are:
              > 21: error: invalid conversion from 'char' to 'char*'
              > 21: error: invalid conversion from 'char' to 'const char*'[/color]

              The errors could hardly be more clear. The function you are calling
              expects char* and const char* arguments, you passed char arguments.

              -Kevin
              --
              My email address is valid, but changes periodically.
              To contact me please use the address from a recent posting.

              Comment

              • Kevin Goodsell

                #8
                Re: Invalid conversion from 'char' tp 'char*'

                Ragemare wrote:
                [color=blue]
                >
                > Not that I'm a expert but you appear to be trying to pass a single segment
                > of the array instead of all of the array, get rid of the '[30]'.[/color]

                "Single element", not "single segment", but basically that's correct
                (though there are several other problems).
                [color=blue]
                > Also I
                > don't think you can pass a two dimentional array to strcpy.
                >[/color]

                You can't pass any array (directly) to a function ever, regardless of
                dimensions. Arrays aren't "first-class citizens" in C++. Any function
                that appears to take (or return) an array actually takes (or returns) a
                pointer.

                A function may, however, take (or return) a class that contains an
                array, or a reference to an array.

                -Kevin
                --
                My email address is valid, but changes periodically.
                To contact me please use the address from a recent posting.

                Comment

                • Kevin Goodsell

                  #9
                  Re: Invalid conversion from 'char' tp 'char*'

                  Ragemare wrote:
                  [color=blue]
                  >
                  > Not that I'm a expert but you appear to be trying to pass a single segment
                  > of the array instead of all of the array, get rid of the '[30]'.[/color]

                  "Single element", not "single segment", but basically that's correct
                  (though there are several other problems).
                  [color=blue]
                  > Also I
                  > don't think you can pass a two dimentional array to strcpy.
                  >[/color]

                  You can't pass any array (directly) to a function ever, regardless of
                  dimensions. Arrays aren't "first-class citizens" in C++. Any function
                  that appears to take (or return) an array actually takes (or returns) a
                  pointer.

                  A function may, however, take (or return) a class that contains an
                  array, or a reference to an array.

                  -Kevin
                  --
                  My email address is valid, but changes periodically.
                  To contact me please use the address from a recent posting.

                  Comment

                  • Christopher Benson-Manica

                    #10
                    Re: Invalid conversion from 'char' tp 'char*'

                    Tim Johansson <spam@gurka.s e> spoke thus:
                    [color=blue]
                    > #include <iostream.h>[/color]

                    It's called <iostream> - the .h headers are deprecated.
                    [color=blue]
                    > #include <string.h>[/color]
                    [color=blue]
                    > int main () {
                    > srand(time(0));
                    > int array_length;
                    > int count;
                    > int randm;
                    > char temp[30];
                    > cout << "How many items in array? ";
                    > cin >> array_length;
                    > char items [30][array_length + 1];[/color]
                    ^^^^^^^^^^^^
                    I assume this is legal in C++, but I'd love it if a real C++ person
                    stated something authoratative. As an aside, if you're making the
                    array array_length+1 items long, then array_length isn't really, is
                    it?
                    [color=blue]
                    > for (count = 0; count <= array_length; count++) {
                    > if (count != 0) {
                    > cout << "\nWhat shall item " << count << " be?\n\t";
                    > }
                    > cin.getline (items[count], 30);[/color]
                    ^^
                    Why not lose the magic number? Your code looks like C, so why not
                    keep the theme going and use

                    cin.getline( items[count], sizeof(items)/sizeof(*items) );

                    ? That way if you change the number of items, you don't have to
                    remember to change the call to getline.
                    [color=blue]
                    > }
                    > for (count = 0; count < array_length; count++) {
                    > randm = rand() % array_length;
                    > strcpy (temp[30], items[30][count]);[/color]

                    Here's the problem (well, besides that you're using C-style strings at
                    all): strcpy is prototyped as

                    char *strcpy( char *s, const char *t );

                    What is temp[30]? It's a char, of course. And what is
                    items[30][count]? It's a char, too. No wonder it doesn't compile,
                    eh?

                    And any particular reason you're ignoring the last string in items? If you
                    had made array_length the actual length of the array, you'd be fine (in this
                    snippet, at least), but you didn't.
                    [color=blue]
                    > strcpy (items[30][count], items[30][randm]); //ERROR
                    > strcpy (items[30][randm], temp[30]); //ERROR
                    > }
                    > return 0;
                    > }[/color]

                    Either read a C book or learn how to use std::strings and std::vectors
                    - I recommend the latter.

                    --
                    Christopher Benson-Manica | I *should* know what I'm talking about - if I
                    ataru(at)cybers pace.org | don't, I need to know. Flames welcome.

                    Comment

                    • Christopher Benson-Manica

                      #11
                      Re: Invalid conversion from 'char' tp 'char*'

                      Tim Johansson <spam@gurka.s e> spoke thus:
                      [color=blue]
                      > #include <iostream.h>[/color]

                      It's called <iostream> - the .h headers are deprecated.
                      [color=blue]
                      > #include <string.h>[/color]
                      [color=blue]
                      > int main () {
                      > srand(time(0));
                      > int array_length;
                      > int count;
                      > int randm;
                      > char temp[30];
                      > cout << "How many items in array? ";
                      > cin >> array_length;
                      > char items [30][array_length + 1];[/color]
                      ^^^^^^^^^^^^
                      I assume this is legal in C++, but I'd love it if a real C++ person
                      stated something authoratative. As an aside, if you're making the
                      array array_length+1 items long, then array_length isn't really, is
                      it?
                      [color=blue]
                      > for (count = 0; count <= array_length; count++) {
                      > if (count != 0) {
                      > cout << "\nWhat shall item " << count << " be?\n\t";
                      > }
                      > cin.getline (items[count], 30);[/color]
                      ^^
                      Why not lose the magic number? Your code looks like C, so why not
                      keep the theme going and use

                      cin.getline( items[count], sizeof(items)/sizeof(*items) );

                      ? That way if you change the number of items, you don't have to
                      remember to change the call to getline.
                      [color=blue]
                      > }
                      > for (count = 0; count < array_length; count++) {
                      > randm = rand() % array_length;
                      > strcpy (temp[30], items[30][count]);[/color]

                      Here's the problem (well, besides that you're using C-style strings at
                      all): strcpy is prototyped as

                      char *strcpy( char *s, const char *t );

                      What is temp[30]? It's a char, of course. And what is
                      items[30][count]? It's a char, too. No wonder it doesn't compile,
                      eh?

                      And any particular reason you're ignoring the last string in items? If you
                      had made array_length the actual length of the array, you'd be fine (in this
                      snippet, at least), but you didn't.
                      [color=blue]
                      > strcpy (items[30][count], items[30][randm]); //ERROR
                      > strcpy (items[30][randm], temp[30]); //ERROR
                      > }
                      > return 0;
                      > }[/color]

                      Either read a C book or learn how to use std::strings and std::vectors
                      - I recommend the latter.

                      --
                      Christopher Benson-Manica | I *should* know what I'm talking about - if I
                      ataru(at)cybers pace.org | don't, I need to know. Flames welcome.

                      Comment

                      • Petec

                        #12
                        Re: Invalid conversion from 'char' tp 'char*'

                        Christopher Benson-Manica wrote:[color=blue]
                        > Tim Johansson <spam@gurka.s e> spoke thus:
                        >[/color]
                        <snip>[color=blue]
                        >[color=green]
                        >> cin >> array_length;
                        >> char items [30][array_length + 1];[/color]
                        > ^^^^^^^^^^^^
                        > I assume this is legal in C++, but I'd love it if a real C++ person
                        > stated something authoratative. <snip>[/color]

                        Nope, it's not legal. You'd either have to use arrays dynamically allocced
                        with new, or std::vectors or another appropriate standard container.

                        - Pete


                        Comment

                        • Petec

                          #13
                          Re: Invalid conversion from 'char' tp 'char*'

                          Christopher Benson-Manica wrote:[color=blue]
                          > Tim Johansson <spam@gurka.s e> spoke thus:
                          >[/color]
                          <snip>[color=blue]
                          >[color=green]
                          >> cin >> array_length;
                          >> char items [30][array_length + 1];[/color]
                          > ^^^^^^^^^^^^
                          > I assume this is legal in C++, but I'd love it if a real C++ person
                          > stated something authoratative. <snip>[/color]

                          Nope, it's not legal. You'd either have to use arrays dynamically allocced
                          with new, or std::vectors or another appropriate standard container.

                          - Pete


                          Comment

                          • Christopher Benson-Manica

                            #14
                            Re: Invalid conversion from 'char' tp 'char*'

                            Petec <x@x.x> spoke thus:
                            [color=blue]
                            > Nope, it's not legal. You'd either have to use arrays dynamically allocced
                            > with new, or std::vectors or another appropriate standard container.[/color]

                            Well, that's what *I* would have done, but I didn't want to say "NO!"
                            and then have to wipe more egg off my face than usual :)

                            --
                            Christopher Benson-Manica | I *should* know what I'm talking about - if I
                            ataru(at)cybers pace.org | don't, I need to know. Flames welcome.

                            Comment

                            • Christopher Benson-Manica

                              #15
                              Re: Invalid conversion from 'char' tp 'char*'

                              Petec <x@x.x> spoke thus:
                              [color=blue]
                              > Nope, it's not legal. You'd either have to use arrays dynamically allocced
                              > with new, or std::vectors or another appropriate standard container.[/color]

                              Well, that's what *I* would have done, but I didn't want to say "NO!"
                              and then have to wipe more egg off my face than usual :)

                              --
                              Christopher Benson-Manica | I *should* know what I'm talking about - if I
                              ataru(at)cybers pace.org | don't, I need to know. Flames welcome.

                              Comment

                              Working...