Link to home
Start Free TrialLog in
Avatar of Amit
AmitFlag for United States of America

asked on

error in string handling


can somebody tell me what's wrong with this ?
_____________________________________________________
string x1,filename;

      char t[]="C:\\";
      char ext[]=".txt";

      cout << "Enter x1";
      cin >>x1;

      filename=strcat(t,x1.c_str());
      filename=strcat(filename.c_str(),ext);

      cout << endl<< "Filename= " <<filename;
Avatar of Kent Olsen
Kent Olsen
Flag of United States of America image


What happens when you compile / run it?

Avatar of Amit

ASKER

filename=strcat(filename.c_str(),ext);

it say this line has error in it

 error C2664: 'strcat' : cannot convert parameter 1 from 'const char *' to 'char *'
        Conversion loses qualifiers

Your usage of the variable 'filename' invokes a conflict.  On one hand, you're passing it to strcat () as a 'const char *', but you also try to use it as the receiving field which is a 'char *'.

Use another variable for the first parameter or the receiving field.

Kent
Avatar of Amit

ASKER

couldn't get it to work, could you show me.


string x1,filename, temp;

     char t[]="C:\\";
     char ext[]=".txt";

     cout << "Enter x1";
     cin >>x1;

     temp=strcat(t,x1.c_str());
     filename=strcat(temp.c_str(),ext);

     cout << endl<< "Filename= " <<filename;

Avatar of Amit

ASKER

Its not working, I use VC++
ASKER CERTIFIED SOLUTION
Avatar of mnashadka
mnashadka

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mnashadka
mnashadka

Sorry, if you have another string named filename, you should change:
x1 = t + x1 + ext;
To:
filename = t + x1 + ext;

Same concept, just different variable.  Hope this helps.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial