Thread: error C2440: '=' : cannot convert from 'char [x]' to 'char [x]'

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    48

    error C2440: '=' : cannot convert from 'char [x]' to 'char [x]'

    I'm making a hangman game in the console right now but I've run into this problem and can't seem to figure out what to do (hey, cut me some slack I just started this stuff like a week or so ago and it's not like I study all day... I've got a life). I'm still not quite done with this program but I can probably finish the rest of it once I get this problem fixed

    What I'm trying to do is to have it get the length of the word it chooses and then output that many blank spaces so that the user know's how many letters it is

    At first I tried using "string word" instead of "char word[]" but it says something like "cannot return length of basic_string::", so now I'm using char. Now I get 4 errors (each one specific to one of the random words) that says:

    error C2440: '=' : cannot convert from 'char [x]' to 'char [x]'
    There is no context in which this conversion is possible


    Can anyone fix this code for me so that it will work or tell me what to do to make it work? Here's the code

    Code:
    #include "stdafx.h"
    
    int main(int argc, char* argv[])
    {
    	cout<<"DOS Hangman"<<endl;
    
    	//declare variables
    	srand(GetTickCount());
    	int n = rand()%4;
    	char word[] = {0};
    
    	//choose word
    	switch (n)
    	{
    	case 1:
    		word = "This";
    		break;
    	case 2:
    		word = "Is";
    		break;
    	case 3:
    		word = "Another";
    		break;
    	default:
    		word = "Test";
    	}
    
    	//outputs blank spaces
    	int len;
    	len = strlen(word);
    	for(int x=0; x==len; x++)
    	{
    		cout<<"_ ";
    	}
    
    	return 0;
    }
    Oh yeah, and I'm using Visual C++ 6.0 Standard.
    Thanks for your help
    Hey, you gotta start somewhere

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Use strcpy() instead of =
    Code:
    strcpy(word, "This");
    and you have to make a larger string ie:

    char word[64]={0};

    (depending on how large words you want to store)
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    48

    thx, but

    thx, but even though there are no more errors, the only thing that the program outputs is:

    DOS Hangman

    ... Anything else I can do?
    Hey, you gotta start somewhere

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    Re: error C2440: '=' : cannot convert from 'char [x]' to 'char [x]'

    Originally posted by c++_n00b
    for(int x=0; x==len; x++)
    {
    cout<<"_ ";
    }
    [/B]
    This is wrong, it should be: x<len

    Plus, you never print 'word' anywhere, you only set it to 'this', 'is', 'another' or 'test'...
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    48

    thx magus

    thx magus it works fine now... but if you don't mind me asking why not ever use "=="? Is it because x starts at 0? but if that's not what == is used for what is it used for?
    Hey, you gotta start somewhere

  6. #6
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802
    You can use '==' but not in that context. You'll use '==' a lot more in for statements or while loops... they're not very common in for loops.

    The structure of a for loop is:

    for (initilize; test; action)

    You had

    for (int x=0; x==len; x++)
    What this will do is initilize x to zero, compare it to len, if it's not equal to len, control will resume after your loop. The only time this will ever loop through is if len = 0, even then itt'l only execute once, as x is incremented, thus making it != to len.

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    27

    how to do it whith char

    heres how to do it with a char variable... shouldnt be too hard to convert to a full fledged hang man

    Code:
    char word[20];
    int rand;
    
    for(int i=0; i<20; i++)
    	word[i]=' ';//fills array with spaces
    
    ran=rand()%4;
    
    switch(ran)
    {
    	case 0: word="blah"; break;
    	case 1: word="asdf"; break;
    	case 2: word="zxcv"; break;
    	case 3: word="qwer"; break;
    }
    
    i=0;
    while(word[i]!=' ')
    {
    	cout<<"_ ";
            i++;
    }
    Gr3g
    Chance favors the prepared mind.

    Vis. C++ 6.0

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  3. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  4. Program Crashing
    By Pressure in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 10:28 PM
  5. convert long to pointer to char array
    By gazmack in forum C++ Programming
    Replies: 5
    Last Post: 09-26-2003, 11:33 AM