gpt4 book ai didi

c++ - 将单词转换为字母c++的字母值

转载 作者:行者123 更新时间:2023-11-28 02:21:31 25 4
gpt4 key购买 nike

我刚开始使用 C++ 并尝试编写一个程序,该程序接受一个单词并将字母转换为与其在字母表中的位置匹配的整数(以点分隔),例如你好 -> 8.5.12.12.15(希望我没记错 ;))

我写了一个小程序,但是好像不行。如果我输入一个字母,那么输出是正确的,但如果我输入多个字母,它就会崩溃。

这是代码:

#include "stdafx.h"
#include <iostream>
#include <string>

int convert(std::string* a, int i)
{
int b;
if (a[i] == "a") { b = 1; return b;}
else if (a[i] == "b") { b = 2; return b;}
else if (a[i] == "c") { b = 3; return b;}
else if (a[i] == "d") { b = 4; return b;}
else if (a[i] == "e") { b = 5; return b;}
else if (a[i] == "f") { b = 6; return b;}
else if (a[i] == "g") { b = 7; return b;}
else if (a[i] == "h") { b = 8; return b;}
else if (a[i] == "i") { b = 9; return b;}
else if (a[i] == "j") { b = 10; return b;}
else if (a[i] == "k") { b = 11; return b;}
else if (a[i] == "l") { b = 12; return b;}
else if (a[i] == "m") { b = 13; return b;}
else if (a[i] == "n") { b = 14; return b;}
else if (a[i] == "o") { b = 15; return b;}
else if (a[i] == "p") { b = 16; return b;}
else if (a[i] == "q") { b = 17; return b;}
else if (a[i] == "r") { b = 18; return b;}
else if (a[i] == "s") { b = 19; return b;}
else if (a[i] == "t") { b = 20; return b;}
else if (a[i] == "u") { b = 21; return b;}
else if (a[i] == "v") { b = 22; return b;}
else if (a[i] == "w") { b = 23; return b;}
else if (a[i] == "x") { b = 24; return b;}
else if (a[i] == "y") { b = 25; return b;}
else if (a[i] == "z") { b = 26; return b;}
}

int main()
{
std::string* a = new std::string;
std::string out;
std::cout << "Please enter a word: ";
std::cin >> *a;
int i = 0;
do
{
out += std::to_string(convert(a, i)) + ".";
i++;
} while (i < a->size());
std::cout << "The converted word is: " << out << std::endl;
return 0;
}

我在这里不知所措,希望你能帮助我......

提前致谢

卡尔图

[编辑] 固定代码

最佳答案

为什么会出错

您将字符串作为指针传递。当您使用索引运算符时,您并不是在访问每个单独的字符,您实际上是在访问可能的字符串数组中的一个字符串。可以将指针视为数组。

所以当你只有字母时,条件

a[0] == "a"   // is the same as *a == "a"

实际上没问题,因为您正在访问数组中的第一个字符串并将其与另一个字符串 "a" 进行比较。但是,当您访问下一个索引时,您会遇到未定义的行为,因为您正在访问无效的内存位置(即访问数组中的第二个字符串,但您从未打算创建一个字符串数组)。


你应该怎么做

无需创建 字符串。只是写:

std::string a; // This is enough

当您将字符串传递给函数时,将其作为 const 引用传递,因为您只是从中读取,

int convert( const std::string &s, int i );

您可以改进的地方

for 循环比 do while 更适合简单地迭代数组/字符串。

在你的convert函数中,

int b;
if (a[i] == 'a') { b = 1; return b;}
else if (a[i] == 'b') { b = 2; return b;}
...

对于非字母字符的情况,不返回。

你应该在返回独立值之间做出选择,

if (a[i] == 'a') { return 1;}
else if (a[i] == 'b') { return 2;}
...

或者最后返回b,

int b = -1; // Say -1 for a non alphabetical character
if (a[i] == 'a') { b = 1; }
else if (a[i] == 'b') { b = 2; }
...
return b; // Return ONLY at the end

不要两者都做。

请注意,您正在尝试将字符与字符串进行比较。

a[i] == "a" // In your code this is comparing a string with a string
// But if you pass your string correctly then it would be character-string comparison

但你实际上想比较一个字符与一个字符,

a[i] == 'a'

但正如其他人指出的那样,字母字符是线性索引的,因此比枚举所有可能的字符有更简单的解决方案。

关于c++ - 将单词转换为字母c++的字母值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32296216/

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