So this a program to check how many times a string has the word chef.The program is compiling properly but when i run it shows that the process exited. I am not able to input anything . How do i solve this? Here's my code
#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;
}
for(int i=0;i<n;i++)
- you'll need a value for n
there.
Your compiler should have warned you for that. Look for warnings about "uninitialized variable"
Use if instead of else if. Set a value for n and do cur=cur+4 because you are already checking the first 4 letters of the string for the word 'chef'. Below is a working version of your code: (your code checks for all permutations of the word 'chef' in the string):
#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;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.