?
Solved

error in string handling

Posted on 2004-10-05
9
Medium Priority
?
247 Views
Last Modified: 2012-06-27

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
Comment
Question by:anshuma
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
9 Comments
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 12225980

What happens when you compile / run it?

0
 

Author Comment

by:anshuma
ID: 12225999
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
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 12226101

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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:anshuma
ID: 12226161
couldn't get it to work, could you show me.
0
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 12226273


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
 

Author Comment

by:anshuma
ID: 12226359
Its not working, I use VC++
0
 
LVL 8

Accepted Solution

by:
mnashadka earned 300 total points
ID: 12226523
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
 
LVL 8

Expert Comment

by:mnashadka
ID: 12226545
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
 
LVL 46

Assisted Solution

by:Kent Olsen
Kent Olsen earned 200 total points
ID: 12226651

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

Kent
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question