Creating a string with a variable in

How would I create a string which has a variable int in it which I could then pass to a function?

[code]
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);
}
[/code]

J

Comments

  • Check
    http://codepedia.com/1/CppNewbieCodeSnippets
    for all conversion functions.

    Have fun,
    bilderbikkel

  • : How would I create a string which has a variable int in it which I could then pass to a function?
    :
    : [code]
    : 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);
    : }
    : [/code]
    :
    : J
    :
    :

    Use sprintf.


    Greets,
    Eric Goldstein
    www.gvh-maatwerk.nl

  • sprintf did the trick, cheers

    J
  • Hi

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

    cheers!
  • : Hi
    :
    : I'd like to see what you were trying to do... would you mind posting your code using [b]sprintf[/b]?
    :
    : 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:

    [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;
    }
    [/code]

    J
  • : : Hi
    : :
    : : I'd like to see what you were trying to do... would you mind posting your code using [b]sprintf[/b]?
    : :
    : : 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:
    :
    : [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;
    : }
    : [/code]
    :
    : J
    :


    Hi

    I just started the C language really and studying thru the string-functions now that I wondered about the use of [b]sprintf[/b]. ;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..?


Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories