在C ++中使用atof()将字符串转换为float

时间:2013-08-24 18:48:14

标签: c++ math atoi

我是C ++的新手,我需要帮助将我的字符串变量转换为数字。我需要在屏幕上打印这个人的年龄和她的下一个年龄,但我无法弄清楚如何。我无法使用字符串进行数学运算。那么请你给我一些例子吗?

我试着这样做:

  //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<<name<<" is "<<age<<" now, and will be " <<age2<<" a year from now.\n"<<"Its "<<ftemp<<" in "<<city<<"--- That's "<<setprecision(2)<<ctemp<<" degrees C"<<endl;

修改

int main() { 
    int city; 
    double ftemp, ctemp,age,age2;
    string buf,name; //Ask user to enter age, name, temperature and city 
    cout<<"Enter your age\n"; 
    cin>>buf; 
    age = atof(buf.c_str()); 
    cout<<"Enter your full name please\n"; 
    getline(cin,name); 
    cout<<"Enter the outside temperature please\n"; 
    cin>>buf; 
    ftemp=atof(buf.c_str()); 
    cout<<"Enter the current city you are in\n"; 
    cin>>buf; 
    city=atoi(buf.c_str());
}

4 个答案:

答案 0 :(得分:1)

您可以使用字符串流将字符串转换为所需的数据类型:

#include <sstream>
#include <string>
#include <iostream>

int main() {
  std::string num_str = "10.5";

  std::istringstream iss(num_str);

  float f_val = 0;

  iss >> f_val;

  //this will output 10.5 (as expected)
  std::cout << f_val << std::endl;

  return 0;
}

f_val分配给字符串的值。

这种方法是首选,因为它具有内置的转换检查功能,并且可以在任何基本的C ++类型上运行。因此,如果您可以创建模板函数来执行检查并返回所需类型的特定值:

#include <sstream>
#include <iostream>
#include <string>
#include <stdexcept>

template<typename T>
T ConvertStringToNumber(const std::string& str)  
{  
  std::istringstream ss(str);  
  T number = 0;  

  ss >> number;  

  if (ss.fail( )) 
  {
    throw std::invalid_argument("ConvertStringToNumber:" + str);
  }

   return number;  
}

int main(int argc, char argv[]){
  std::string num_str = "10.5";
  float f = ConvertStringToNumber<float>(num_str);

  std::cout << f << std::endl;

  return 0;
}

答案 1 :(得分:1)

由于未提及华氏度至摄氏度(摄氏度)转换,我们将忽略它。

正如最初的写作,问题似乎是询问这一行:

age2 = age + 1 ; age2 = atof (age2.c_str()); age = atof (age.c_str());

我们没有看到声明。暂时,我会假设事先声明:

double age, age2;

鉴于此(假设在此行之前没有对ageage2进行其他操作),将age2设置为age + 1会将一个值添加到未定义的值并将其存储在age2。然后为存储的值分配转换结果...等待,你在开玩笑......将age2转换为C字符串并将atof()应用于结果的结果。那么,也许我假设age2的类型是错误的 - 它真的是std::string?但是如果age2是一个字符串,那么您在先前的分配方面遇到了问题,并且此age2 = atof(age2.c_str());分配也不起作用。类似问题适用于age = atof(age.c_str());作业。逻辑应该'分配给age; age2 = age + 1;”。

str:string s_age = "21";
double age = atof(s_age.c_str());
double age2 = age + 1;

std::cout << "Age: " << age << ", Next year: " << age2 << endl;

考虑排序。考虑类型。当您下一个问题时,请显示类型!查看创建SSCCE(Short, Self-Contained, Correct Example)所需的内容,并尽可能使您的问题中的代码成为SSCCE。

我正在讨论是否atof()(或atoi()最初在问题标题中提到但在问题正文中没有再提及)是转换C字符串的好方法进入doubleint;有更好的方法可以在C和C ++中进行这些转换,并且C ++中的机制与C中的机制不同,尤其是当源字符串是std::string时。

答案 2 :(得分:0)

  1. 正如我在上面的评论中所说,对年龄和年龄使用 float

    float age, age2;
    // do math
    cout<<name<<" is "<<age<<" now, and will be " <<age2<<" a year from now.\n";
    
  2. 使用Qt作为您的C ++ Framework并享受C ++

    QString age("15.5");
    double temp = age.toDouble();
    

答案 3 :(得分:-2)

#include "iostream.h"

main() {
    float a,b,rmvivek,arni;
    char ch="123.00"; 
    a=atof(ch);
    arni=a+2*5;
    cout  << "the a value is" << arni;
}
相关问题