C ++字符串到双精度转换

编程入门 行业动态 更新时间:2024-10-08 00:24:45
本文介绍了C ++字符串到双精度转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

通常当我在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 ++字符串到双精度转换

本文发布于:2023-11-30 13:30:39,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1650156.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   精度

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!