输入没有因为注释而终止

Input not terminating because of a comment

本文关键字:终止 注释 因为 输入      更新时间:2023-10-16

此cpp代码未终止。我已经尝试了各种输入的代码,但这不是终止。我认为第52行有错误,当我注释第52行时,代码工作正常。

#include<cstdio>
#include<iostream>
#include<vector>
#include<algorithm>
// binary search for larger elements
using namespace std;
vector <int > q;

// bsearch value uses hte 

#define bvector q // just define these values to use them in your functin
#define VALUE(x) bvector[x]
int b_search(int value){
int low=0,high=bvector.size(),mid;
    mid=(low+high)/2;
    cout << "In the bsearch";
    while(low<high)
    {
            if(VALUE(mid)==value)
                    return mid;
            else if(VALUE(mid)>value)//                      
                    high=mid;
            else if(VALUE(mid)<value)        
                   low=mid+1; 
    }
    if(VALUE(low)>value)
            return low;
    return -1;                
}
int main(){
int i;
    for(scanf("%d",&i);i;scanf("%d",&i))
            q.push_back(i); // this is for taking input in the vectot
sort(q.begin(),q.end());                
    for(i=0;i<q.size();i++)
            printf("%d ",q[i]);// for printing the sorted
            int j;
            printf("Enter the elements you want to search");
            int x;
            scanf("%d",&x);
 // BUG is present in this lines
            cout <<"This is the end of scanf";// if this line is commented then the 54th line is not reached
            j=b_search(x);
          printf("%d ",j);
return 0;
 }

语句:mid=(low+high)/2;有点放错地方了。它应该在while循环中。这可能就是导致无限循环的原因。