Thread: Help with returning length of char variable.

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    4

    Help with returning length of char variable.

    I'm having some trouble returning the length of a char variable in a XOR encryption code.

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
      int len;  
      string str2="A nice cat";
      len = str2.size();
      char str[len]=str2;
      char key[len]="ABCDEFGHIJ";
        for(int x=0; x<(len-1); x++)
        {
          str[x]=str[x]^key[x];
          cout  <<  str[x];
        }
      cout << "\n";
      system("PAUSE");
    }
    I am getting the errors:

    variable-sized object 'str' may not be initialized.
    variable-sized object 'key' may not be initialized.


    Any ideas?

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    You can't have variable length array in C++. You would have to use dynamic allocation.
    Woop?

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Why do you need to use character arrays at all? Just use the string, since operator[] returns a character just like it does for a character array.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 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. Obtaining source & destination IP,details of ICMP Header & each of field of it ???
    By cromologic in forum Networking/Device Communication
    Replies: 1
    Last Post: 04-29-2006, 02:49 PM
  4. code condensing
    By bcianfrocca in forum C++ Programming
    Replies: 4
    Last Post: 09-07-2005, 09:22 AM
  5. Strings are V important...
    By NANO in forum C++ Programming
    Replies: 15
    Last Post: 04-14-2002, 11:57 AM