#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2001
    Location
    Up in the clouds
    Posts
    100
    Rep Power
    14

    Concantenating floats/ints onto strings


    Hey,
    New to C++. I've got the following:

    float min = 0.8;
    string Admin_Email = "email@address.com";

    string command = "echo \"The Server Load average is: " + min + "\" | mail -s \"Server Unstable\"" + Admin_Email;

    system(command.c_str());

    When I try to compile I get:

    invalid operands of types `const char[35]` and `float` to binary `operator +'

    I've thought about type casting, and have search around, but haven't found anything to really help. All I want to do is have the float, min, echoed to the command line, as well as the string, Admin_Email.

    TIA,
    Mike.
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,625
    Rep Power
    4247
    Try something like this instead:
    Code:
    #include <cstdio> // Put this with the other #includes
    
    
    float min = 0.8; 
    string Admin_Email = "email@address.com"; 
    
    char command[2000];
    sprintf(command, "echo \"The Server Load Average is %f\" | mail -s \"Server Unstable\" %s",
               min, Admin_Email.c_str();
    system(command);
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2001
    Location
    Up in the clouds
    Posts
    100
    Rep Power
    14
    Awesome, that did it! Now, just to understand this completely:

    sprintf(charvar, "text to enter %x1 %x2", var1, var2);

    In the instance above, the information will be put into charvar, %x is used to reference var1 and var2 consequtively. And what follows after the % indicates what kind of variable varx is, right? So %f means float, %s means string?

    Thanks a lot!
    Mike.

    PS.

    Woohoo, my first useful C++ program :D
  6. #4
  7. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,625
    Rep Power
    4247
    Yep, you pretty much got it correctly. BTW %s means char * rather than string, but you have the right idea there. Congratulations on writing your first useful C++ program! :)
  8. #5
  9. Left due to despotic ad-min
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2003
    Posts
    1,044
    Rep Power
    14
    To actually do it in idiomatic C++ (eg actually using the string class) rather than sprintf() which is formally a C function, try something like the following (illustrative, untested) code

    #include <iostream>
    #include <string>
    #include <strstream>
    #include <cstdio>

    int main()
    {
    std::ostrstream a_stream;
    a_stream << "echo \"The Server Load average is: "
    << min
    << "\" | mail -s \"Server Unstable\" "
    << Admin_Email;
    std::string command(a_stream.str());
    system(command.c_str());
    return 0;
    }
  10. #6
  11. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,625
    Rep Power
    4247
    The reason I didn't use strstream much is because early on, a C++ compiler that I used had a bug that caused it to work in one version, but gave a different result when I used it in another version of the compiler. These days, I've started to use it again :). The one thing to remember though is, when you want to overwrite the string in the strstream, you have to move the read and write pointers back to the beginning of the strstream(they're separate variables that can be moved independent of each other).
    Last edited by Scorpions4ever; July 10th, 2003 at 10:31 AM.

IMN logo majestic logo threadwatch logo seochat tools logo