質問

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?

I tried to do it this way:

  //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;

Edit:

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());
}
役に立ちましたか?

解決

You can use stringstreams to convert the string to the data type you want:

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

Which will assign f_val to the value of the string.

This approach is preferred in that it has built-in checking of conversion, and it can work on any of the fundamental C++ types. Hence, if you can can create a template function to perform the checking for you and return that particular value of the type you desire:

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

他のヒント

Since the Fahrenheit to Centigrade (Celsius) conversion isn't mentioned, we'll ignore it.

As originally written, the question seems to be asking about the line:

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

We are not shown the declarations. For the time being, I'll assume there's a prior declaration:

double age, age2;

Given that (and assuming no other manipulation of age or age2 before this line), setting age2 to age + 1 adds one to an undefined value and stores it in age2. Then the stored value is assigned the result of converting ... hold on, you're joking ... the result of converting age2 to a C string and applying atof() to the result. So, maybe my assumed type for age2 is wrong — it is really std::string? But if age2 is a string, you've got problems with the prior assignment, and this age2 = atof(age2.c_str()); assignment doesn't work either. Similar issues apply to the age = atof(age.c_str()); assignment. The logic should be 'assign to 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;

Think about sequencing. Think about types. When you next ask a question, show the types! Look up what it takes to create an SSCCE (Short, Self-Contained, Correct Example) and make the code in your question an SSCCE if at all possible.

I'm ducking the discussion of whether atof() (or atoi() as originally mentioned in the question title but not mentioned again in the body of the question) is a good way to convert a C string into a double or int; there are better ways to do those conversions in both C and C++, and the mechanisms in C++ are different from those in C, especially when the source string is a std::string.

  1. as I said in a comment above, use float for age and ages:

    float age, age2;
    // do math
    cout<<name<<" is "<<age<<" now, and will be " <<age2<<" a year from now.\n";
    
  2. use Qt as your C++ Framework and enjoy 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帰属
所属していません StackOverflow
scroll top