Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 按字符循环通过std字符串的分段错误_C++_String_For Loop_Char - Fatal编程技术网

C++ 按字符循环通过std字符串的分段错误

C++ 按字符循环通过std字符串的分段错误,c++,string,for-loop,char,C++,String,For Loop,Char,我正在尝试制作一个程序,要求我一个字符一个字符地循环一个字符串,并对它执行所说的操作。以下是我现在拥有的: #include <iostream> #include <stack> #include <string> int loopThroughString(const string& hello); int main(void){ while (true) { cout << "String? " ;

我正在尝试制作一个程序,要求我一个字符一个字符地循环一个字符串,并对它执行所说的操作。以下是我现在拥有的:

#include <iostream>
#include <stack>
#include <string>
int loopThroughString(const string& hello);

int main(void){
    while (true) {
        cout << "String? " ;
        string s;            
        cin.ignore();
        getline(cin, s);
        if (s.length() == 0){
            break;
        } 
        int answer = loopThroughString(s);
        cout << answer << endl;
    } 
    cout << endl;
}

int loopThroughString(const string& hello){
    int answer;
    string str = hello;
    char ch;
    stack<int> s1;

    for(unsigned int i = 0; i <str.size(); i++){
        ch = str[i];
        cout << "character ch of hello is: " << ch << "\n";
        for(int j=0; j < 10; j++){
            if(ch == j)
                s1.push(ch);
        }
    }
    result = s1.top();
    return result;
}
#包括
#包括
#包括
int loopThroughString(常量字符串和hello);
内部主(空){
while(true){

问题是在空堆栈上调用
s1.top()
是未定义的行为。在调用
s1.top()
之前,应该检查
!s1.empty()

由于以下代码,堆栈通常为空:

for(int j=0; j < 10; j++){
     if(ch == j)
            s1.push(ch);
}
for(int j=0;j<10;j++){
if(ch==j)
s1.推力(ch);
}

字符
ch
包含字符代码;字符
'0'
与整数
0
等有不同的代码。一个简单的解决方法是
for(char j='0';j问题是在空堆栈上调用
s1.top()
是未定义的行为。您应该检查
!s1.empty()
在调用
s1.top()之前。

由于以下代码,堆栈通常为空:

for(int j=0; j < 10; j++){
     if(ch == j)
            s1.push(ch);
}
for(int j=0;j<10;j++){
if(ch==j)
s1.推力(ch);
}

字符
ch
包含字符代码;字符
'0'
与整数
0
等有不同的代码。一个简单的解决方法是
for(char j='0';j问题是在空堆栈上调用
s1.top()
是未定义的行为。您应该检查
!s1.empty()
在调用
s1.top()之前。

由于以下代码,堆栈通常为空:

for(int j=0; j < 10; j++){
     if(ch == j)
            s1.push(ch);
}
for(int j=0;j<10;j++){
if(ch==j)
s1.推力(ch);
}

字符
ch
包含字符代码;字符
'0'
与整数
0
等有不同的代码。一个简单的解决方法是
for(char j='0';j问题是在空堆栈上调用
s1.top()
是未定义的行为。您应该检查
!s1.empty()
在调用
s1.top()之前。

由于以下代码,堆栈通常为空:

for(int j=0; j < 10; j++){
     if(ch == j)
            s1.push(ch);
}
for(int j=0;j<10;j++){
if(ch==j)
s1.推力(ch);
}

字符
ch
保存一个字符代码;字符
'0'
与整数
0
有不同的代码,等等。一个简单的解决方法是
for(char j='0';j尝试获取空堆栈的顶部

改变

result = s1.top();
return result;


您正在尝试获取空堆栈的顶部

改变

result = s1.top();
return result;


您正在尝试获取空堆栈的顶部

改变

result = s1.top();
return result;


您正在尝试获取空堆栈的顶部

改变

result = s1.top();
return result;


我觉得没问题。你确定这段代码是问题的原因吗?省略的代码是否修改了
str
?使用
std::string
std::cout
效果很好。检查你的include和命名空间。为你定义的
string
在哪里?你的
string
std::string
的类型定义吗?如果不是t、 您的错误可能在那里。
String
的构造函数中发生了什么?很抱歉,这不是String,它只是str::String。我在for循环中添加了一些cout进行测试,例如字符串:“1+1”输入时,for循环将运行2次,但在尝试设置char ch=str[i]时,在第三次运行时给出错误。我觉得没问题。你确定这段代码是问题的原因吗?省略的代码是否修改了
str
?使用
std::string
std::cout
效果很好。检查你的include和命名空间。为你定义的
string
在哪里?你的
string
std::string
的类型定义吗?如果not,您的错误可能在那里。
String
的构造函数中发生了什么?很抱歉,这不是String,它只是str::String。我在for循环中添加了一些cout进行测试,例如字符串:“1+1”输入时,for循环将运行2次,但在尝试设置char ch=str[i]时,在第三次运行时给出错误。我觉得没问题。你确定这段代码是问题的原因吗?省略的代码是否修改了
str
?使用
std::string
std::cout
效果很好。检查你的include和命名空间。为你定义的
string
在哪里?你的
string
std::string
的类型定义吗?如果not,您的错误可能在那里。
String
的构造函数中发生了什么?很抱歉,这不是String,它只是str::String。我在for循环中添加了一些cout进行测试,例如字符串:“1+1”输入时,for循环将运行2次,但在尝试设置char ch=str[i]时,在第三次运行时给出错误。我觉得没问题。你确定这段代码是问题的原因吗?省略的代码是否修改了
str
?使用
std::string
std::cout
效果很好。检查你的include和命名空间。为你定义的
string
在哪里?你的
string
std::string
的类型定义吗?如果not,您的错误可能在那里。
String
的构造函数中发生了什么?很抱歉,这不是String,它只是str::String。我在for循环中添加了一些cout进行测试,例如字符串:“1+1”输入时,for循环将运行2次,但在尝试设置char ch=str[i]时,在第三次运行时出现错误。