Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/140.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++ 无法以c+形式输入+;程序退出_C++ - Fatal编程技术网

C++ 无法以c+形式输入+;程序退出

C++ 无法以c+形式输入+;程序退出,c++,C++,这是一个程序,用来检查一个字符串有多少次使用chef这个词。该程序正在正确编译,但当我运行它时,它显示进程已退出。 我无法输入任何内容。我如何解决这个问题?这是我的密码 #include<iostream> #include<vector> using namespace std; int chef_count(string s){ int count=0,cur=0,c,h,e,f; while(cur!=(s.size()-2)){ c=

这是一个程序,用来检查一个字符串有多少次使用chef这个词。该程序正在正确编译,但当我运行它时,它显示进程已退出。 我无法输入任何内容。我如何解决这个问题?这是我的密码

#include<iostream>
#include<vector>

using namespace std;


int chef_count(string s){
  int count=0,cur=0,c,h,e,f;
    while(cur!=(s.size()-2)){
      c=0,h=0,e=0,f=0;
      if(s[cur]=='c'||s[cur+1]=='c'||s[cur+2]=='c'||s[cur+3]=='c'){
        c++;
      }
      else if(s[cur]=='h'||s[cur+1]=='h'||s[cur+2]=='h'||s[cur+3]=='h'){
        h++;
      } 
      else if(s[cur]=='e'||s[cur+1]=='e'||s[cur+2]=='e'||s[cur+3]=='e'){
        e++;
      }
      else if(s[cur]=='f'||s[cur+1]=='f'||s[cur+2]=='f'||s[cur+3]=='f'){
        f++;
      }
      if(c==1 && e==1 && h==1 && f==1){
        count++;
      }
      cur++;
    }
  return count;

}


int main(){
  int n;
  int val;
  vector<string> store;
  string s;
  for(int i=0;i<n;i++){
    getline(cin,s);
    store.push_back(s);

  }
  for(int i=0;i<n;i++){
    val=chef_count(store[i]);
    if (val>0){
      cout << val <<endl;
    }
    else{
      cout << "normal" <<endl;
    }
  }

  return 0;

}
#包括
#包括
使用名称空间std;
int chef_计数(字符串s){
整数计数=0,cur=0,c,h,e,f;
而(cur!=(s.size()-2)){
c=0,h=0,e=0,f=0;
如果(s[cur]='c'| s[cur+1]='c'| s[cur+2]='c'| s[cur+3]='c'){
C++;
}
如果(s[cur]='h'| s[cur+1]='h'| s[cur+2]='h'| s[cur+3]='h'){
h++;
} 
如果(s[cur]='e'| s[cur+1]='e'| s[cur+2]='e'| s[cur+3]='e'){
e++;
}
否则如果(s[cur]='f'| s[cur+1]='f'| s[cur+2]='f'| s[cur+3]='f'){
f++;
}
如果(c==1&&e==1&&h==1&&f==1){
计数++;
}
cur++;
}
返回计数;
}
int main(){
int n;
int-val;
向量存储;
字符串s;

for(int i=0;i
for(int i=0;i使用if而不是else if)。为n设置一个值,并执行cur=cur+4,因为您已经在检查字符串的前4个字母中的单词“chef”。
下面是您的代码的工作版本:(您的代码检查字符串中单词“chef”的所有排列):

#包括
#包括
#包括
使用名称空间std;
int chef_计数(字符串s){
整数计数=0,cur=0,c,h,e,f;

虽然(Curt这是一种非常糟糕的检查方法。为什么不使用
find
:未初始化的局部变量实际上是未初始化的。它们的值是不确定的,看起来是随机的。在没有初始化的情况下以任何方式使用它们都会导致未定义的行为。在使用索引之前,您应该始终检查
字符串的长度。如果tring是长度2,然后是s[2]将访问越界并且是未定义的行为。您的代码看起来像是在检查字符串中单词chef的所有组合。如果您正在查找确切的单词,那么您应该这样编码。是的,字母是混合的。输入大小写如下:ifehcfisgoodforchess-单词chef在这种情况下出现两次计算c、h、e和f的数量。如果计数相等,则打印它,否则打印c、h、e或f中最小的计数。
#include<iostream>
#include<vector>
#include<string>
using namespace std;


int chef_count(string s){
  int count=0,cur=0,c,h,e,f;
    while(cur<(s.size())-1){
      c=0,h=0,e=0,f=0;
      if(s[cur]=='c'||s[cur+1]=='c'||s[cur+2]=='c'||s[cur+3]=='c'){
        c++;
      }
       if(s[cur]=='h'||s[cur+1]=='h'||s[cur+2]=='h'||s[cur+3]=='h'){
        h++;
      } 
       if(s[cur]=='e'||s[cur+1]=='e'||s[cur+2]=='e'||s[cur+3]=='e'){
        e++;
      }
       if(s[cur]=='f'||s[cur+1]=='f'||s[cur+2]=='f'||s[cur+3]=='f'){
        f++;
      }
      if(c==1 && e==1 && h==1 && f==1){
        count++;
      }
      cur=cur+4;
    }
  return count;

}


int main(){
  int n=2;
  int val;
  vector<string> store;
  string s;
  for(int i=0;i<n;i++){
      getline(cin,s);
    store.push_back(s);

  }
  for(int i=0;i<n;i++){
    val=chef_count(store[i]);
    if (val>0){
      cout << val <<endl;
    }
    else{
      cout << "normal" <<endl;
    }
  }

  return 0;

}