C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28695
Number of posts: 94715

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Creating a string with a variable in Posted by jambeard on 2 Apr 2006 at 2:27 PM
How would I create a string which has a variable int in it which I could then pass to a function?

myFunc(const string &inString)
{
    //do something with the string
}

main()
{
    int someInt;
    //Do something with the int

    string myString = "someInt = "; //How do I add someInt to this string?

    myFunc(myString);
}


J

Report
Re: Creating a string with a variable in Posted by bilderbikkel on 2 Apr 2006 at 10:19 PM
Check
http://codepedia.com/1/CppNewbieCodeSnippets
for all conversion functions.

Have fun,
bilderbikkel

Report
Re: Creating a string with a variable in Posted by tsagld on 3 Apr 2006 at 3:08 AM
: How would I create a string which has a variable int in it which I could then pass to a function?
:
:
: myFunc(const string &inString)
: {
:     //do something with the string
: }
: 
: main()
: {
:     int someInt;
:     //Do something with the int
: 
:     string myString = "someInt = "; //How do I add someInt to this string?
: 
:     myFunc(myString);
: }
: 

:
: J
:
:

Use sprintf.


Greets,
Eric Goldstein
www.gvh-maatwerk.nl

Report
Re: Creating a string with a variable in Posted by jambeard on 3 Apr 2006 at 5:34 AM
sprintf did the trick, cheers

J
Report
Re: Creating a string with a variable in Posted by tokoG on 3 Apr 2006 at 7:23 PM
Hi

I'd like to see what you were trying to do... would you mind posting your code using sprintf?

cheers!
Report
Re: Creating a string with a variable in Posted by jambeard on 4 Apr 2006 at 1:03 PM
: Hi
:
: I'd like to see what you were trying to do... would you mind posting your code using sprintf?
:
: cheers!
:


I'm using C++ with CLIPS (expert system shell). One of the CLIPS functions is assert string (add a Fact) but it doesn't let me use a variable (I need to pass it a time). I could of tried to re-write the function (CLIPS is open source) but I'm not the best of programmers!

Here's some of my code:

if(newKitchenHeatSensor.mStatus)
{
	t1 = time(0);
	int timeOn = difftime(t1,t2);

	char assert[200];
	sprintf(assert,"(updateHob (time-on %d))",timeOn);

	AssertString(assert);
	run = true;
}


J
Report
Re: Creating a string with a variable in Posted by tokoG on 4 Apr 2006 at 7:16 PM
: : Hi
: :
: : I'd like to see what you were trying to do... would you mind posting your code using sprintf?
: :
: : cheers!
: :
:
:
: I'm using C++ with CLIPS (expert system shell). One of the CLIPS functions is assert string (add a Fact) but it doesn't let me use a variable (I need to pass it a time). I could of tried to re-write the function (CLIPS is open source) but I'm not the best of programmers!
:
: Here's some of my code:
:
:
: if(newKitchenHeatSensor.mStatus)
: {
: 	t1 = time(0);
: 	int timeOn = difftime(t1,t2);
: 
: 	char assert[200];
: 	sprintf(assert,"(updateHob (time-on %d))",timeOn);
: 
: 	AssertString(assert);
: 	run = true;
: }
: 

:
: J
:


Hi

I just started the C language really and studying thru the string-functions now that I wondered about the use of sprintf. ;P

CLIPS, is that the programme if not the language, or a technique to create the expert systems, or is CLIPS expert system it self..?





 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.