我试图使用递归函数将 String 转换为 ASCII 值。通过仅使用字符串作为参数
I was trying to convert String into ASCII value using Recursive function. By using the string only as parameter
这是我的代码(我只想使用字符串作为参数):
#include <iostream>
using namespace std;
int i = 0; //to take string character by character
void parseToInteger (string s1)
{
char convert;
string another;
if (convert == s1.length()) //at null character function terminates
{
cout<<endl; // prints nothing
}
else
{
convert = s1.at(i++);
static_cast <int> (convert);
cout<<convert;
parseToInteger (s1);
}
}
int main ()
{
string s0;
cout<<"Enter a string to convert it into its integer value: ";
getline (cin, s0);
parseToInteger (s0);
return 0;
}
这是我收到的错误:
Enter a string to convert it into its integer value: hello
terminate called after throwing an instance of 'std::out_of_range'
what(): basic_string::at: __n (which is 32540) >= this->size() (which is 5)
有人可以帮助我吗?我正在学习编程。
你的if (convert == s1.length())
非常奇怪。该表达式正在将 uninitialized char
变量与 s1
字符串(将是 size_t
类型)的长度进行比较。你应该做的是比较i
变量和长度(i
最好定义为size_t
类型)。
static_cast <int> (convert);
行也什么都不做。您不能那样更改变量的类型;相反,您应该在下一行中对 cout <<
的操作数使用强制转换。请注意,当将字符数字转换为其整数值时,您需要从中减去 '0'
的值。
这是解决上述问题的修复版本:
#include <iostream>
#include <string> // You 'forgot' this required header
size_t i = 0; // The "size_t" type is better suited for container sizes and lengths
void parseToInteger(std::string s1)
{
char convert;
// std::string another; // Never used!
if (i == s1.length()) { // Here, we've reached the end of the string
std::cout << std::endl; // prints nothing
}
else {
convert = s1.at(i++);
std::cout << cout << static_cast <int> (convert - '0'); // Apply the cast here
parseToInteger(s1);
}
}
int main()
{
std::string s0;
std::cout << "Enter a string to convert it into its integer value: ";
std::getline(std::cin, s0);
parseToInteger(s0);
return 0;
}
但请注意,您的代码中还有其他问题,如评论中所述。首先,最好通过引用传递 s1
参数;第二个:Are global variables bad?
这是解决后两个问题的函数版本:
void parseToInteger(std::string& s1, size_t i = 0)
{
if (i == s1.length()) { // Here, we've reached the end of the string
std::cout << std::endl; // 'flushes' output stream
}
else {
char convert = s1.at(i++);
std::cout << static_cast <int> (convert - '0'); // Convert to int
parseToInteger(s1, i);
}
}
新 i
参数的默认值(零)意味着您不必更改 main
函数中的调用。
这是我的代码(我只想使用字符串作为参数):
#include <iostream>
using namespace std;
int i = 0; //to take string character by character
void parseToInteger (string s1)
{
char convert;
string another;
if (convert == s1.length()) //at null character function terminates
{
cout<<endl; // prints nothing
}
else
{
convert = s1.at(i++);
static_cast <int> (convert);
cout<<convert;
parseToInteger (s1);
}
}
int main ()
{
string s0;
cout<<"Enter a string to convert it into its integer value: ";
getline (cin, s0);
parseToInteger (s0);
return 0;
}
这是我收到的错误:
Enter a string to convert it into its integer value: hello
terminate called after throwing an instance of 'std::out_of_range'
what(): basic_string::at: __n (which is 32540) >= this->size() (which is 5)
有人可以帮助我吗?我正在学习编程。
你的if (convert == s1.length())
非常奇怪。该表达式正在将 uninitialized char
变量与 s1
字符串(将是 size_t
类型)的长度进行比较。你应该做的是比较i
变量和长度(i
最好定义为size_t
类型)。
static_cast <int> (convert);
行也什么都不做。您不能那样更改变量的类型;相反,您应该在下一行中对 cout <<
的操作数使用强制转换。请注意,当将字符数字转换为其整数值时,您需要从中减去 '0'
的值。
这是解决上述问题的修复版本:
#include <iostream>
#include <string> // You 'forgot' this required header
size_t i = 0; // The "size_t" type is better suited for container sizes and lengths
void parseToInteger(std::string s1)
{
char convert;
// std::string another; // Never used!
if (i == s1.length()) { // Here, we've reached the end of the string
std::cout << std::endl; // prints nothing
}
else {
convert = s1.at(i++);
std::cout << cout << static_cast <int> (convert - '0'); // Apply the cast here
parseToInteger(s1);
}
}
int main()
{
std::string s0;
std::cout << "Enter a string to convert it into its integer value: ";
std::getline(std::cin, s0);
parseToInteger(s0);
return 0;
}
但请注意,您的代码中还有其他问题,如评论中所述。首先,最好通过引用传递 s1
参数;第二个:Are global variables bad?
这是解决后两个问题的函数版本:
void parseToInteger(std::string& s1, size_t i = 0)
{
if (i == s1.length()) { // Here, we've reached the end of the string
std::cout << std::endl; // 'flushes' output stream
}
else {
char convert = s1.at(i++);
std::cout << static_cast <int> (convert - '0'); // Convert to int
parseToInteger(s1, i);
}
}
新 i
参数的默认值(零)意味着您不必更改 main
函数中的调用。