0

I use the following code to get the input

int main() {
    int x=0,value=0;
    cin>>x;
    string str;
    for(int i=0;i<x;++i){
      cin>>str;
      value = findpalindrome(str);
      cout<<value<<endl;}
  return 0;
}

And these are my test cases:

3
xabcbayabbaz
abcbaabc
abcba

The code accepts the first string and gives the output. However, when I enter the next two strings, it just waits for more input. I tried giving input separately without the loop but that didn't work either. The compiler waits for more input infinitely.

I am putting my entire code here for reference.

#include <iostream>
#include <string.h>
using namespace std;

struct palindrome{
  int start;
  int end;
  int strlength;
}s[20];

bool checkifpalindrome(string str)
{
    string str1=str;
    for(int i=0;i<str.length()/2;++i){
        char temp=str1[i];
        str1[i]=str1[str.length()-1-i];
        str1[str.length()-1-i]=temp;
        
    }
    if(str1.compare(str)==0)
        return true;
    return false;
}
bool checkoverlap(int cur_pos,int prev_pos){
  if((s[cur_pos].start>=s[prev_pos].start && s[cur_pos].start<=s[prev_pos].end)||
    (s[cur_pos].end>=s[prev_pos].start && s[cur_pos].end<=s[prev_pos].end))
    return true;
  else 
    return false;
}

int findmax(int count){
    int max = 0;
    int pos = 0;
    for(int i=0;i<count;++i){
        if (s[i].strlength>max){
            max = s[i].strlength;
            pos = i;
        }
    }
    return pos;
}
int findpalindrome(string str)
{
    int sum=0;
    int count=-1;
    string str1;
    for(int i=0;i<str.length();++i){
    for(int j=1;j<str.length()-i+1;++j){
        str1=str.substr(i,j+1);
        if(checkifpalindrome(str1)){
            count++;
            s[count].start=i;
            s[count].end=i+j;
            s[count].strlength=str1.length();
        }  
      }
    }
    int pos[2];
    pos[0]=findmax(count);
    sum+=s[pos[0]].strlength;
    s[pos[0]].strlength=0;
    pos[1]=findmax(count);
    int i=0,n=10;
    while(i<n){
        if(s[pos[0]].strlength==str.length())
          break;
        else if(checkoverlap(pos[1],pos[0])){
          s[pos[1]].strlength=0;
          pos[1]=findmax(count);
          continue;
        }
        else if(s[pos[1]].strlength==0)
            break;
        else{
          sum+=s[pos[1]].strlength;
          break;
        }
        i++;
    }
    return sum;
}
int main() {
    int x=0,value=0;
    cin>>x;
    string str;
    for(int i=0;i<x;++i){
      cin>>str;
      value = findpalindrome(str);
      cout<<value<<endl;}
  return 0;
}
1201ProgramAlarm
  • 32,384
  • 7
  • 42
  • 56
Jerrymon
  • 109
  • 8
  • 1
    I'm not sure the issue is anything to do with `cin` for the *second* input. Just call `findPalindrome` with the input that doesn't work. My guess is you're going into an infinite loop somewhere. – cigien Oct 24 '20 at 19:19
  • 1
    Run in a debugger (or add some print statements) to see where you code is blocking. – Martin York Oct 24 '20 at 19:46
  • Recommended reading: [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/10147399) – Aykhan Hagverdili Oct 24 '20 at 20:22

2 Answers2

1

Note that the i++ line in the while loop of your findpalindrome function will never be executed - either the loop will exit (on break) or start again from the beginning (on continue).

Removing the continue; line from the first else if block in that loop prevents the program from going into an infinite loop - but I can't verify that it fixes all your problems (there are numerous cases of comparing signed to unsigned types, which may be problematical).

Enabling (full) compiler warnings (and heeding them) is helpful in such circumstances! For your original code, clang-cl gives (among other things):

warning : code will never be executed [-Wunreachable-code]

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
0

In addition to @Adrian Mole's answer, it is good to check such things by removing intermediate functions like your findpalindrome(), just to make sure, that input values are read correctly and the problem is not in it.

elo
  • 489
  • 3
  • 13