• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 249
  • Last Modified:

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;
0
anshuma
Asked:
anshuma
  • 4
  • 3
  • 2
2 Solutions
 
Kent OlsenData Warehouse Architect / DBACommented:

What happens when you compile / run it?

0
 
anshumaEngineeringAuthor Commented:
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
0
 
Kent OlsenData Warehouse Architect / DBACommented:

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
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
anshumaEngineeringAuthor Commented:
couldn't get it to work, could you show me.
0
 
Kent OlsenData Warehouse Architect / DBACommented:


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;

0
 
anshumaEngineeringAuthor Commented:
Its not working, I use VC++
0
 
mnashadkaCommented:
What I think you want here is:
string x1;
char t[]="C:\\";
char ext[]=".txt";

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

x1 = t + x1 + ext; // Concatenate the strings.  Note: this only works with strings.  Don't use it with char *'s or char []'s.

cout << endl<< "Filename= " <<filename;
0
 
mnashadkaCommented:
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.
0
 
Kent OlsenData Warehouse Architect / DBACommented:

Good Catch, mnashadka.  I've got C code on the brain and obviously wrapped them around each other......

Kent
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 4
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now