Sunday, August 2, 2009

C++ variable question?

Hi, I have a question about C++. Is there any way to set a character variable equal to a string, and then another variable after that? For example:





char x[100];


char y[100];





cout %26lt;%26lt; "Please enter your name: ";


cin %26gt;%26gt; y;





x = "name is: " , y ,"\r\n";





I know that is incorrect how I have it, but that is what I need to do.. is there any way of doing that? I know you could simply have a cout statement, but that will not work, as I need to send x to another function, and it needs all of that info without sending another variable to the function with the extra info (dealing with socket programming, and sending commands to a server, if you are really interested).





Any help on this would be greatly appreciated.

C++ variable question?
use sprintf()..


sprintf() acts the same way as printf() do BUT, instead of placing the output directly to the console screen, the output is rather stored in a string.. So using your variable x and y, it can be done like this...





sprintf(x, "Name is: %s \r\n", y);





(^^,) Then the new string will be placed in x... And don't forget to include stdio.h.. Anyway, just try this without the "stdio.h" header.. I think this is already available in the "iostream" header.. Well, if you have more questions relating C++, just visit my blog and just leave a comment at http://ronaldborla.blogsome.com/ or http://ronaldborla.wordpress.com/


Also http://ronaldborla.blogsome.com/2008/02/... for my biggest announcement in my blog... (^^,) Thanks!
Reply:You can set the values in the char array equal to the characters in the string. I'm not sure if that's what you're after, but you could write a simple loop along the lines of


for (i=o; i%26lt; len(myString); i++)


y[i]=mySting[i];





I'd have to look up how to select a char from a string, I'm not sure if that notation works, but you get the idea.


No comments:

Post a Comment