问题描述
通常当我在C ++中写任何东西,并且我需要将 char
转换为 int
new int
等于char。
Usually when I write anything in C++ and I need to convert a char
into an int
I simply make a new int
equal to the char.
我使用的代码(snippet)
I used the code(snippet)
string word;
openfile >> word;
double lol=word;
我收到错误
Code1.cpp cannot convert `std::string' to `double' in initialization
错误是什么意思?第一个字是数字50.谢谢:)
What does the error mean exactly? The first word is the number 50. Thanks :)
推荐答案
您可以将char转换为int和反之亦然, int和char是相同的,8位,唯一的区别是当他们必须在屏幕上显示,如果数字是65,并保存为一个字符,那么它将显示'A',如果它保存为一个int它将显示65.
You can convert char to int and viceversa easily because for the machine an int and a char are the same, 8 bits, the only difference comes when they have to be shown in screen, if the number is 65 and is saved as a char, then it will show 'A', if it's saved as a int it will show 65.
对于其他类型的东西会改变,因为它们在内存中的存储方式不同。在C中有标准函数,它允许你从字符串转换为double容易,它的atof。 (您需要包含stdlib.h)
With other types things change, because they are stored differently in memory. There's standard function in C that allows you to convert from string to double easily, it's atof. (You need to include stdlib.h)
#include <stdlib.h>
int main()
{
string word;
openfile >> word;
double lol = atof(word.c_str()); /*c_str is needed to convert string to const char*
previously (the function requires it)*/
return 0;
}
这篇关于C ++字符串到双精度转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!