gpt4 book ai didi

c++ - 将字符串转换为 int 问题

转载 作者:行者123 更新时间:2023-11-28 04:19:06 25 4
gpt4 key购买 nike

所以我设法将字符串转换为 int。但是,在代码 #1 中,当我尝试将它分配给数组中的第一个插槽并将其打印出来时,它会打印“<”。为什么要这样做?我知道它与 ascii 字符有关。代码 #2 打印出我想要的 int 60。

atoi(menuAttributes[c].c_str()) = 20;
quantity[d] = 3;
string price[14];


#1
price[0] = atoi(menuAttributes[c].c_str()) * quantity[d];
cout << price[0] << endl;


#2
cout << atoi(menuAttributes[c].c_str()) * quantity[d] << endl;

差不多,我希望 price[0] 等于 int 60,而不是 char '<'。谢谢!

编辑:已解决,感谢大家的帮助。菜鸟,抱歉!

最佳答案

这是因为你的price定义:

string price[14];

- 您已将其定义为包含 14 个字符串的数组,并尝试为数组中的第一个字符串 (price[0]) 分配一个数值(从字符串的角度来看是伪造的)查看)。

一旦您将价格定义为 int price[14],您就会得到您所期望的

关于c++ - 将字符串转换为 int 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55869678/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com