Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

error in string handling

Posted on 2004-10-05
9
Medium Priority
?
248 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone 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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.
Suggested Courses

604 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