Solved

error in string handling

Posted on 2004-10-05
9
242 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
  • 4
  • 3
  • 2
9 Comments
 
LVL 45

Expert Comment

by:Kdo
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 45

Expert Comment

by:Kdo
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
 

Author Comment

by:anshuma
ID: 12226161
couldn't get it to work, could you show me.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 45

Expert Comment

by:Kdo
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 75 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 45

Assisted Solution

by:Kdo
Kdo earned 50 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

705 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now