Help with returning length of char variable.

This is a discussion on Help with returning length of char variable. within the C++ Programming forums, part of the General Programming Boards category; I'm having some trouble returning the length of a char variable in a XOR encryption code. Code: #include <iostream> #include ...

  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,807
    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,319
    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

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