Thread: Problem Copying char array

  1. #1
    [root@matrix]# NullStyle's Avatar
    Join Date
    Nov 2003
    Posts
    7

    Problem Copying char array

    Hi guys. I keep receiving problems when copying characters from an array to another. I'm using Visual C++ 6.0 on Windows XP Home Edition. I've also tested my code on Dev C++ and it does not work.

    Code:
    #include <iostream.h>
    #include <string.h>
    
    int main()
    {
    	char word[20], perm[20];
    	int i, j, max;
    
    	cin >> word;
    	
    	max = strlen(word);
    	for(i = 0; i < max; ++i)
    		word[i] = perm[i];
    	for(j = 0; j < max; ++j)
    		cout << perm[j];
    	return 0;
    }
    Input:
    CALENDAR
    Output:
    ΜΜΜΜΜΜΜΜ

    Thanks in advance.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > word[i] = perm[i];
    I think you want
    perm[i] = word[i];
    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.

  3. #3
    [root@matrix]# NullStyle's Avatar
    Join Date
    Nov 2003
    Posts
    7
    Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Char array problem.
    By kbro3 in forum C++ Programming
    Replies: 16
    Last Post: 08-21-2008, 01:31 PM
  2. Problem with copying a string into array in a struct
    By JFonseka in forum C Programming
    Replies: 15
    Last Post: 05-04-2008, 05:07 AM
  3. problem working with char array
    By bradleym83 in forum C Programming
    Replies: 4
    Last Post: 07-27-2005, 10:57 AM
  4. Problem Putting INTs Into a CHAR Array
    By cram in forum C++ Programming
    Replies: 13
    Last Post: 10-13-2004, 07:53 AM
  5. comparing fields in a text file
    By darfader in forum C Programming
    Replies: 9
    Last Post: 08-22-2003, 08:21 AM