Problem Copying char array

This is a discussion on Problem Copying char array within the C++ Programming forums, part of the General Programming Boards category; Hi guys. I keep receiving problems when copying characters from an array to another. I'm using Visual C++ 6.0 on ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,422
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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

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