Creating a string with a variable in - Programmers Heaven

Howdy, Stranger!

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

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Creating a string with a variable in

jambeardjambeard Posts: 70Member
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

  • bilderbikkelbilderbikkel Posts: 754Member
    Check
    http://codepedia.com/1/CppNewbieCodeSnippets
    for all conversion functions.

    Have fun,
    bilderbikkel

  • tsagldtsagld Posts: 621Member
    : 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

  • jambeardjambeard Posts: 70Member
    sprintf did the trick, cheers

    J
  • tokoGtokoG Posts: 209Member
    Hi

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

    cheers!
  • jambeardjambeard Posts: 70Member
    : 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
  • tokoGtokoG Posts: 209Member
    : : 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.