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
Solved

error in string handling

Posted on 2004-10-05
9
244 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: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 45

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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

 

Author Comment

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

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 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:Kent Olsen
Kent Olsen 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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

Suggested Solutions

Title # Comments Views Activity
c++ substatte a varabe for a string in a LPCTSTR statment 8 89
convert char array to number in c 5 86
Safe conversion? 4 72
designing in object programming 12 94
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
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.

840 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