title: Converting string to float with atof() in C++
body: <p>I'm new to C++ and I need help converting my string variable into a number. I need to print the person's age and her next year age on the screen but I can't figure out how. I can't use the string to do math. So can you please shoot me a couple examples please?</p>
  
  <p>I tried to do it this way:</p>
  
  <pre><code>  //converts F to C
    ctemp=(ftemp-32)*5.0/9.0;
  
    //calculates age
    age2 = age + 1 ; age2 = atof (age2.c_str()); age = atof (age.c_str());
  
  
    cout&lt;&lt;name&lt;&lt;" is "&lt;&lt;age&lt;&lt;" now, and will be " &lt;&lt;age2&lt;&lt;" a year from now.\n"&lt;&lt;"Its "&lt;&lt;ftemp&lt;&lt;" in "&lt;&lt;city&lt;&lt;"--- That's "&lt;&lt;setprecision(2)&lt;&lt;ctemp&lt;&lt;" degrees C"&lt;&lt;endl;
  </code></pre>
  
  <p><strong>Edit:</strong></p>
  
  <pre><code>int main() { 
      int city; 
      double ftemp, ctemp,age,age2;
      string buf,name; //Ask user to enter age, name, temperature and city 
      cout&lt;&lt;"Enter your age\n"; 
      cin&gt;&gt;buf; 
      age = atof(buf.c_str()); 
      cout&lt;&lt;"Enter your full name please\n"; 
      getline(cin,name); 
      cout&lt;&lt;"Enter the outside temperature please\n"; 
      cin&gt;&gt;buf; 
      ftemp=atof(buf.c_str()); 
      cout&lt;&lt;"Enter the current city you are in\n"; 
      cin&gt;&gt;buf; 
      city=atoi(buf.c_str());
  }
  </code></pre>