strcpy not working at all?!

Hi, I am trying to build a string based on the data the program parses so at the end, i can output a standard cout << everything;  rather than awkard cout's varying in the programme or cout << x << y << " " << z;

this is for a h/w assignment, but the thing i am doing isnt assigned.  i am just spicing up the programme with some cosmetic issues to make it look better and run better, etc...so i can learn string handling, etc.

below is the issues.

thanks

jw
====================================================

#include <iostream>
#include <cstring>
#include "timestamp.h"

string tag;
string alles;

strcpy(alles,tag);
cout << "after strcat: " << alles;

~/275 $ g++ -o time2 timestamp.cc timedriver.cc
timestamp.cc: In member function `void TimeStamp::display()':
timestamp.cc:143: cannot convert `std::string' to `char*' for argument `1' to
   `char* strcpy(char*, const char*)'
~/275 $
weinrjAsked:
Who is Participating?
 
MindphaserConnect With a Mentor Commented:
Moved to PAQ and refunded points.

** Mindphaser - Community Support Moderator **
0
 
lexxwernCommented:
okay
is that your code. !hello! void main()??? is that there!!!

"i am just spicing up the programme
with some cosmetic issues to make it look better and run be..."

you are spicing up a program with strcpy()!!!
please explain a bit more

lexxwern
0
 
weinrjAuthor Commented:
well spcing up may not been a good choice of words...void main is not shown.  i am just diplaying stuff related to the problem.  i have over 400 lines of code working fine except for those issues.

what this is doing, making a timestamp (a small part of the the full thing)...

anyways, output would be like Saturday, March 23, 2002 at 12:00 AM.  where everything is a string, saturday, march, 23, 20002, 12,:,00,am.

so i want to cacatante or copy everything into a big string so i can output everything at the bottom of the function, and in the middle of the func it can build it up....like mon=dec....day=31....and later on every=mon+day; (like javascript would do...).

i hope this cleared it up a bit.  thanks!!!

jw
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
lexxwernCommented:
Copies string src to dest

 Declaration:
  ? char *strcpy(char *dest, const char *src);
  ? char far * _fstrcpy(char far *dest, const char far *src);

 Remarks:
Copies string src to dest, stopping after the terminating nul
been moved.

here...
argument two gets merged into arg 1.
0
 
lexxwernCommented:
please give the code for the class, seems to be a problem there.

//: In member function `void TimeStamp::display()':
//timestamp.cc:143: cannot convert `std::string' to `char*' //for argument `1' to
//  `char* strcpy(char*, const ch

lexxwern
0
 
snoeglerCommented:
Hey lexxwern, take a look at the declarations of the strings weinrj wants to copy:

 string tag;
 string alles;

 strcpy(alles,tag);


This are STL strings, which cannot be copied by 'strcpy' because they are class objects.

 char tag[80];
 char alles[80];

 strcpy(alles,tag);

This would do something, but it would probably crash, because the strings are not necessarily (esp. in release build) zero terminated. Also it does not make sense to copy a string to another string before assigning something to it.

 char tag[80]="this is my 'tag' string";
 char alles[80];
 
 strcpy(alles,tag); // <- now 'alles' contains the same as 'tag'

If you want to copy the strings you originally used, simply assign them:

 string tag="this is my 'tag' string";
 string alles;

 alles = tag;
0
 
weinrjAuthor Commented:
but i have multiple strings to copy.

as i said i am building an entire date into the string.

tag is just the day.  (in german bc i already used day and days for something else so i just used the german word to mean the same thing).

so pretty much alles = tag & monat & date & year & hh & ":" & minute & amPmFlag;

is what i want to do.

thanks

jw
0
 
TriskelionCommented:
STL provides a way for you to concatenate or copy using
plus (+) and/or equal (=) without all of the old-school string manipulating functions.

#include <iostream>
using namespace std;

int main()
{
     string tag     =     "day";
     string alles = "all";
     
     alles += tag;
     cout << "after strcpy: " << alles.c_str();
     //
     return 0;
}
0
 
weinrjAuthor Commented:
well pretty much that didnt work out too well, so this works...

   cout << tag << ", " << monat << day << ender << ", " << year << " at " << tmpHr << ":" << mm[0] << mm[1] << ampm << ".";


see its big, i rather had done cout << alles;

but this works none the less...

output:

Enter current date and time (yyyy, mm, dd, hh, mm): 2002 3 23 15 19
Saturday, March 23rd, 2002 at 3:19 PM.

thats what that function pretty much does....i got about 4 or 5 more members to do b4 this baby is done! and due by wednesday 12:00AM....
so i am going to have some fun tonight!!
0
 
gandalf79Commented:
assuming all of the variables you listed above are of the std::string type and have already been assigned values. the following code should do about the same thing as your long cout statment.

#include <string>
// do some stuff here
std::string alles;

alles.append(tag);
alles.append(", ");
alles.append(monat);
alles.append(day);
alles.append(ender);
alles.append(", ");
alles.append(year);
alles.append(" at ");
alles.append(tmpHr);
alles.append(":");
alles.append(mm[1]);
alles.append(mm[2]);
alles.append(ampm);
alles.append(".");

cout << alles << endl;

of corse this will not work if any of the varibles placed into the append functions are not of the std::string type or the const char* type. the most be converted from numbers to std::string first!!!
0
 
TriskelionCommented:
The += thing will still work depending on how you add the strings.
You can still put all of the variables in alles and then cout << alles.

Do you have to use the STL string?
It sounds (as someone said earlier) like you need a char * and good-old sprintf();
0
 
weinrjAuthor Commented:
i used <cstring>
class


some things are arrays and some things are ints and some things are just chars.

jw
0
 
mblatCommented:
declaration for strcpy is
int strcpy(char*,const char*);

so you can't use string(s) here.

to use strings you need to do:

string a;
string b = "test";

strcpy(const_cast<char*>(a.c_str()),b.c_str());

or

a = b;

or use

char* a;
char* b;

then you can use strcpy
0
 
snoeglerCommented:
This is wrong and will result in memory corruption.
BTW: It will also fail, because strcpy() doesn't take a const(!) char* as its 'destination pointer'.
0
 
mblatCommented:
you are right - strcpy doesn't take "const char*" as "destinaition pointer".  That is why you need to const_cast to take off "constess".

And by the way - before posing it I compile and run it....

so...
0
 
snoeglerCommented:
'c_str()' returns a 'const char*' - now guess why? Because it may not be overwritten. It is intended to pass the contents of the STL string to functions which need C strings as an input only argument.

MSDN quote:

"Objects of type string belonging to the C++ template class basic_string<char> are not necessarily null terminated. The null character ' \0 ' is used as a special character in a C-string to mark the end of the string but has not special meaning in an object of type string and may be a part of the string just like any other character. There is an automatic conversion from const char* into strings, but the string class does not provide for automatic conversions from C-style strings to objects of type basic_string<char>.

The returned C-style string should not be modified, as this could invalidate the pointer to the string, or deleted, as the string has a limited lifetime and is owned by the class string."

Please don't post comments (not to speak of posting an answer) unless you know what you're talking about.

BTW: Why SHOULD weinrj use 'strcpy', if the (correct and working and not memory corrupting) way of simply assigning the strings does what is desired?
0
 
weinrjAuthor Commented:
I wsa out of the area for a bit...So what should i do at this point?  The assignment was due last week, so I had already handed it in.  I think the way I did it, works fine.

Regardless of the big string I desired.

So your saying that code in the proposed answer is wrong?
I have another project at the time, so I have little time at this moment to see if this works.

I will be back in a bit though.

JW
0
 
DanRollinsCommented:
mblat,
The experts in this section have agreed to post comments rather than answers.  So, in the future, please post comments, like everybody else.  Thanks!
=--==-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

wienri,
Yes.  The code in the proposed answer is wrong.  You should reject that answer.  

Oddly, nobody here has made a suggestion that would really answer your question.  There are several techniques that will do the trick.  If the variables

  monat, day,  year, etc,

are std::string objects, then you can concatenate one to another to build a longer string.  For instance,

std::string alles;

alles= tag + ",";
alles += monat + day + ender;
(...etc...)

However, it is more likely that some of these are int data (binary numeric values).  In that case, you need to first convert them into character data (std::string objects).  That can be laborious for a large number of variables.  Fortunately the standard C libraries provide a function that simplies this very common need.  It is called sprintf.  For instance,

#include <string>
#include <iostream>

using namespace std;

void main()
{
     string sDayName= "Saturday";
     string sMonat=   "March";
     string sEnder= "rd";
     int nDay= 23;
     int nYear= 2002;
     int nHour= 3;
     int nMinute= 9;
     string sAmOrPm= "PM";
     //------------- all vars are ready, now build the string

     char szTmp[100]; // temporary destination (make sure big enough)

     sprintf( szTmp, "%s, %s %d%s, %d at %d:%02d %s",
          sDayName.c_str(),
          sMonat.c_str(),
          nDay,
          sEnder.c_str(),
          nYear,
          nHour,
          nMinute,
          sAmOrPm.c_str()
     );

     string sAlles= szTmp; // copy to string object for ease of use

     cout << "You entered: " << sAlles << endl;
}
=-=-=-=-=-=-=-=-=-=-=-=--==-=-=-=-=-=-
I hope you do well with your next assignment.  Good luck!

-- Dan
0
 
griesshCommented:
Dear weinrj

I think you forgot this question. I will ask Community Support to close it unless you finalize it within 7 days. You can always request to keep this question open. But remember, experts can only help you if you provide feedback to their questions.
Unless there is objection or further activity,  I will suggest to

     "refund the points and PAQ at zero points"

since you provided your own solution.

=========================================================
You have 13 open questions out of 90 that need your attention! Please take
some time and accept an answer if an expert was able to help you or
provide feedback if needed.
==========================================================

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
======
Werner
0
 
MoondancerCommented:
ADMINISTRATION WILL BE CONTACTING YOU SHORTLY.  Moderators Computer101 or Netminder will return to finalize these if they are still open in 14 days.  Experts, please post closing recommendations before that time.

Below are your open questions as of today.  Questions which have been inactive for 21 days or longer are considered to be abandoned and for those, your options are:
1. Accept a Comment As Answer (use the button next to the Expert's name).
2. Close the question if the information was not useful to you, but may help others. You must tell the participants why you wish to do this, and allow for Expert response.  This choice will include a refund to you, and will move this question to our PAQ (Previously Asked Question) database.  If you found information outside this question thread, please add it.
3. Ask Community Support to help split points between participating experts, or just comment here with details and we'll respond with the process.
4. Delete the question (if it has no potential value for others).
   --> Post comments for expert of your intention to delete and why
   --> YOU CANNOT DELETE A QUESTION with comments; special handling by a Moderator is required.

For special handling needs, please post a zero point question in the link below and include the URL (question QID/link) that it regards with details.
http://www.experts-exchange.com/jsp/qList.jsp?ta=commspt
 
Please click this link for Help Desk, Guidelines/Member Agreement and the Question/Answer process.  http://www.experts-exchange.com/jsp/cmtyHelpDesk.jsp

Click you Member Profile to view your question history and please keep them updated. If you are a KnowledgePro user, use the Power Search option to find them.  

Questions which are LOCKED with a Proposed Answer but do not help you, should be rejected with comments added.  When you grade the question less than an A, please comment as to why.  This helps all involved, as well as others who may access this item in the future.  PLEASE DO NOT AWARD POINTS TO ME.

To view your open questions, please click the following link(s) and keep them all current with updates.
http://www.experts-exchange.com/questions/Q.20237866.html
http://www.experts-exchange.com/questions/Q.20244796.html
http://www.experts-exchange.com/questions/Q.20245337.html
http://www.experts-exchange.com/questions/Q.20246990.html
http://www.experts-exchange.com/questions/Q.20246992.html
http://www.experts-exchange.com/questions/Q.20246034.html
http://www.experts-exchange.com/questions/Q.20251070.html
http://www.experts-exchange.com/questions/Q.20268654.html
http://www.experts-exchange.com/questions/Q.20269088.html
http://www.experts-exchange.com/questions/Q.20274990.html
http://www.experts-exchange.com/questions/Q.20280590.html

To view your locked questions, please click the following link(s) and evaluate the proposed answer.
http://www.experts-exchange.com/questions/Q.20268101.html

*****  E X P E R T S    P L E A S E  ******  Leave your closing recommendations.
If you are interested in the cleanup effort, please click this link
http://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=commspt&qid=20274643 
POINTS FOR EXPERTS awaiting comments are listed in the link below
http://www.experts-exchange.com/commspt/Q.20277028.html
 
Moderators will finalize this question if in @14 days Asker has not responded.  This will be moved to the PAQ (Previously Asked Questions) at zero points, deleted or awarded.
 
Thanks everyone.
Moondancer
Moderator @ Experts Exchange
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.