why is my code not letting me repeat? after the first run, and you type y to repeat it skips the run and say repeat again?

[

void line(){
char ans;
do{
string line;

int count=0;    
cout<<"Enter a line of text.\n";
getline(cin,line);

for(int x=0;x<line.length();x++){
      
if(islower(line[x])&&isalpha(line[x+1])&&isalpha(line[x+2])
&&isalpha(line[x+3])&&(!(isalpha(line[x-1])))&&(line[x+4]==' '||
line[x+4]=='.'||line[x+4]==','||line[x+4]=='\0')){
count+=5;                                        
} 
else{
count=0;   
}  
if(count==5){
line[x]='l';
line[x+1]='o'; 
line[x+2]='v';
line[x+3]='e';            
}    
 
if(isupper(line[x])&&isalpha(line[x+1])&&isalpha(line[x+2])
&&isalpha(line[x+3])&&(!(isalpha(line[x-1])))&&(line[x+4]==' '||
line[x+4]=='.'||line[x+4]==','||line[x+4]=='\0')){        
count+=4;                      
}
else{
count=0;
}

if(count==4){
line[x]='L';
line[x+1]='o'; 
line[x+2]='v';
line[x+3]='e';                         
}
}

for(int y=0;y<line.length();y++){       
cout<<line[y];                      
} 
cout<<endl;
cout<<"Repeat?(y/n)";
cin>>ans;
}while(ans=='y');  
}

]

>> cout<<"Repeat?(y/n)";
>> cin>>ans;

You need to clear out all the extra stuff in the keyboard buffer, such as the <Enter> key '\n' before continuing after that cin. Try cin.ignore(); But if you also type other characters that won't necessarily work either because it will only remove a single character. You need to study this thread to get a complete answer.

>> cout<<"Repeat?(y/n)";
>> cin>>ans;

You need to clear out all the extra stuff in the keyboard buffer, such as the <Enter> key '\n' before continuing after that cin. Try cin.ignore(); But if you also type other characters that won't necessarily work either because it will only remove a single character. You need to study this thread to get a complete answer.

thanks did the trick

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.