Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Last minute question about string() argument

  1. Mar 4, 2005 #1

    Math Is Hard

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    What does the "1" do in string() in this line?

    answer = string(1,'0'+i%10) + answer;

    Thanks!
     
  2. jcsd
  3. Mar 4, 2005 #2

    dduardo

    User Avatar
    Staff Emeritus

    What library is this function coming from?
     
  4. Mar 4, 2005 #3

    Math Is Hard

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    sorry - this is using <string> in C++ ..
    I think.. I am really confused. Teacher posted it as part of a solution to an exercise we did. I did something different so I am trying to figure out what this means.
     
  5. Mar 4, 2005 #4

    dduardo

    User Avatar
    Staff Emeritus

    Ok, now I know what your doing.

    The string function is an overloaded constructor. In this case there are two parameters. The first parameter is the number of times the second parameter is repeated. Therefore,

    string(1,'H') => "H"
    string(2,'H') => "HH"

    The string constructor also has a one parameter function like so:

    string('H') => "H"
     
  6. Mar 4, 2005 #5

    Math Is Hard

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    ah! ok, thanks so much!!!
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: Last minute question about string() argument
Loading...