簡體   English   中英

在C ++中使用atof()將字符串轉換為float

[英]Converting string to float with atof() in C++

我是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());
}

您可以使用stringstreams將字符串轉換為所需的數據類型:

#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;
}

由於未提及華氏度到攝氏度(Celsius)的轉換,因此我們將忽略它。

如最初所寫,這個問題似乎是在詢問這行:

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

沒有顯示聲明。 現在,我假設有一個事先聲明:

double age, age2;

鑒於此(並假設在此行之前沒有對ageage2其他操作),將age2設置為age + 1會將未定義的值加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( 簡短,自包含,正確的示例 )所需的內容,並盡可能使問題中的代碼成為SSCCE。

我回避了有關atof() (或問題標題中最初提到的atoi() ,但在問題正文中沒有再次提及)是否是將C字符串轉換為doubleint的好方法的討論; 在C和C ++中都有更好的方法來進行這些轉換,並且C ++中的機制與C中的機制不同,尤其是當源字符串是std::string

  1. 正如我在上面的評論中所說,請針對年齡段使用浮點數

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

     QString age("15.5"); double temp = age.toDouble(); 
#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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM