Why this code is executing 'cin' statement infinite times

the code is taking value of N infinite times . i don’t know why this program is behaving like this ?

#include <iostream>

using namespace std;

int main()
{
  int T,i,D[100];
  cout<<" enter number of test cases you want to perform ";
  cin>>T;
  
  for(i=0;i<T;++i)
  {
      int N[T],A[T],B[T],j,k;
      
         cout<<" enter no of fibonacci series number you want to print ";
         cin>>N[i];
         
         cout<<N[i];
         A[0]=0;
         A[1]=1;
         
         for(j=2;j<N[i];++j)
         {
             A[j]=A[j-1]+A[j-2];
             A[j-2]=A[j-1];
             A[j-1]=A[j];
         }

     
     for(j=0;j<N[i];++j)
     {
         B[j]=A[j]%10;
     }
     
     while(N[i]!=1)
       {     
         
           if(N[i]%2==0)
           {
                for(j=0;j<N[i];j+=2)
                  {
                  
                      
                         B[j/2]=B[j];
                     
                   
                    }
                    N[i]=N[i]/2;
           }
           
           else
           {
               for(j=0;j<N[i];j+=2)
                  {
                  
                      
                         B[j/2]=B[j];
                     
                   
                    }
                    
                    N[i]=(N[i]+1)/2;
           }
       }
     D[i]=B[1];
     
  }
for(i=0;i<T;++i)
{
   cout<<" the required solution is "<<D[i]<<"\n";
}
   return 0;
}```

Please either format your code or link to your submission - the forum software has mangled it and it won’t compile! :slight_smile:

Slightly better code :

#include

using namespace std;

int main()
{
int T,i,D[100];
cout<<" enter number of test cases you want to perform ";
cin>>T;

for(i=0;i<T;++i)
{
int N[T],A[T],B[T],j,k;


      cout<<" enter no of fibonacci series number you want to print ";
      cin>>N[i];
      
      cout<<N[i];
      A[0]=0;
      A[1]=1;
      
      for(j=2;j<N[i];++j)
      {
          A[j]=A[j-1]+A[j-2];
          A[j-2]=A[j-1];
          A[j-1]=A[j];
      }

  
  for(j=0;j<N[i];++j)
  {
      B[j]=A[j]%10;
  }
  
  while(N[i]!=1)
    {     
      
        if(N[i]%2==0)
        {
             for(j=0;j<N[i];j+=2)
               {
               
                   
                      B[j/2]=B[j];
                  
                
                 }
                 B[i]=B[i]/2;
        }
        
        else
        {
            for(j=0;j<N[i];j+=2)
               {
               
                   
                      B[j/2]=B[j];
                  
                
                 }
                 
                 N[i]=(N[i]+1)/2;
        }
    }
  D[i]=B[1];


}
for(i=0;i<T;++i)
{
cout<<" the required solution is “<<D[i]<<”\n";
}
return 0;
}
1 Like

check it now i have fixed it

1 Like

i just edited it bro .can you check for the error now ?

What input are you providing, and how are you providing it?

I just tried it on the command line and asked for 1 testcase with “no of fibonacci series number” == 2, and it didn’t execute the cin statement infinite times, but it did get stuck inside the while(N[i]!=1) loop.

2 Likes

did it printed the value of N ?

Oh, yes - but I had to make a change:

cout<<N[i];

to
cout<<N[i] << endl;

(the output wasn’t being flushed before, so you didn’t see N being printed).

2 Likes

ok and the program didn’t came out of while loop ?

No; I made the change:

 while(N[i]!=1)
        {
            cout << "Blah!" << endl;

and it just prints out Blah! forever.

3 Likes

i have changed a code a bit inside while loop change it from B[i]=B[i]/2 to N[i]= N[i]/2;
now this should not execute infinite times

1 Like

Yes, that was the mistake. It isn’t executing infinitely now.

Btw, you should try debugging yourself, before asking in the discuss. Just output any random statements in the main parts of the program and you will come to know where the bug is.

2 Likes