Results 1 to 5 of 5

Thread: strcpy is annoying! help!

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 1999
    Location
    East Ballina,NSW,Australia
    Posts
    121

    Exclamation strcpy is annoying! help!

    Hi All,

    I'm having a an annoying problem with some source, any help will be greatly appreciated!

    char point;
    //load all other variables etc

    //Load BattleStatus.dat into oceanp
    thefile.open("battlestatus.dat");
    for(rowd = 0; rowd <=9; rowd++)
    {
    for (cold = 0; cold <=9; cold++)
    {
    thefile.get(point);
    if(point == '~')
    //The problem is below I keep getting the following compiler error about the strcpy: error C2664: 'strcpy' : cannot convert parameter 1 from 'char' to 'char *'
    strcpy(point, " ");

    oceanp[rowd][cold] = point;
    }
    }
    thefile.close();



    Plz help!

    Regards,

    Smithy.

  2. #2
    Frenzied Member Vlatko's Avatar
    Join Date
    Aug 2000
    Location
    Skopje, Macedonia
    Posts
    1,409
    strcpy is expecting a pointer to a char.

    Have a look at thisthread . It is similar to your problem.
    Last edited by Vlatko; May 11th, 2001 at 04:13 AM.
    I am become death, the destroyer of worlds.
    mail:[email protected]

    • Visual Basic 6.0 & .NET
    • Visual C++ 6.0 & .NET
    • ASP
    • LISP
    • PROLOG
    • C
    • Pascal

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Oct 1999
    Location
    East Ballina,NSW,Australia
    Posts
    121

    Smile ok, but

    Hi,

    I read over it but it didn't help much. From an overall perspective, what iam trying to achieive is get it to read from a file called battlestatus.dat.
    battlestatus.dat contains data in a format like this...
    CCCCC S T F SS XX
    battlestatus.dat has 100 characters, with things such as Characters and spaces.
    My program is supposed to read battlestatus.dat and write it into oceanp [10] [10] when the first program loads.
    This is basically what i'm trying to achieve, so far, all it writes is blank spaces to oceanp [10] [10].
    I don't know why but battlestatus.dat may contain some spaces after the first 100 characters or it could be that it's not moving onto the next poisition in the file or it could be because the variable point is not updating.
    My Code is below. please help!

    Code:
    #include <iostream.h>
    #include <cstring>
    #include <fstream.h>
    
    char oceanp [10] [10] ={ {' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' '},
    {' ' , ' ' , 'C' , 'C' , 'C' , 'C' , 'C' , ' ' , ' ' , ' '},
    {' ' , ' ' , ' ' , ' ' , ' ' , 'S' , ' ' , ' ' , ' ' , ' '},
    {' ' , ' ' , ' ' , 'T' , ' ' , 'S' , ' ' , ' ' , ' ' , 'F'},
    {' ' , 'M' , ' ' , ' ' , 'T' , 'S' , ' ' , ' ' , 'F' , ' '},
    {' ' , 'M' , ' ' , ' ' , ' ' , 'S' , ' ' , 'F' , ' ' , ' '},
    {'F' , 'M' , 'B' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' '},
    {'F' , ' ' , ' ' , 'B' , ' ' , ' ' , ' ' , ' ' , 'T' , ' '},
    {'F' , ' ' , ' ' , ' ' , 'B' , ' ' , ' ' , ' ' , 'T' , ' '},
    {' ' , ' ' , ' ' , ' ' , ' ' , 'B' , ' ' , ' ' , ' ' , ' '} };
    
    int row;
    int col;
    int rowd;
    int cold;
    char point = ' ';
    ifstream thefile;
    
    int main()
    {
    
    
    //Load BattleStatus.dat into oceanp
    thefile.open("battlestatus.dat");
    for(rowd = 0; rowd <=9; rowd++)
    {
    	for (cold = 0; cold <=9; cold++)
    	{
    		thefile.get(point);
    		oceanp[rowd][cold] = point;
    	}
    }
    thefile.close();
    
    		
    	
    	cout << "**********   Smithy's Simple BattleShips Game!!   **********" << endl;
    	cout << "Program By:  Ben Smith. (C) 2001 Ben Smith." << endl;
    	cout << endl;
    Any Help would be greatly appreciated!!
    Thanks.

    Regards,

    Smithy.

  4. #4
    Hyperactive Member
    Join Date
    Jan 2000
    Posts
    355
    strcpy(point, " ");

    the reason for the error is you're passing a char, point, to strcpy when it wants a char*

    so do this:

    strcpy(&point, " ");

    to pass point's address

    think that should work, from my shaky C knowledge
    buzzwords are the language of fools

  5. #5
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    A pointer to a character isn't the same as a string. It needs to be null-terminated to work. Anyway for assigning a character you don't need to use strcpy(), you can just assign with the assignment operator ( = ) :

    Code:
    point = ' ';
    I can't really suggest anything for the file access problem because I've never done file access with streams.
    Harry.

    "From one thing, know ten thousand things."

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width