0

I tried running a program on Sublime Text 3 with a custom build. I think I have entered into an infinite loop.

I have read a few other answers about Sublime Text 3 entering into infinite loop. But none of them answer my question.

The program is in c++. I am inputting text from a file called input.in and outputting to output.out. There isn't something in the code that can turn into a potential infinite loop.

I could solve the problem by deleting output.out and then making another file by the same name. But I don't think that this is the only way and neither the best way. Does anyone have a better method.

I tried to delete all the input from input.in. That just made the problem worse. The program started writing lots of 0000 0000 blocks to output.out. After thousands of such lines it starts printing alphanumeric strings of length 4 (which might also be hexadecimal numbers not quite sure) I have already tried killing sublime text from terminal. But when I reopen it the file output.out is still filling. I also tried Cancel Build. Even that doesn't help.

My code is

#include <stdio.h>
#include <cmath>
#include <bits/stdc++.h>
#define ll long long 
#define rep(a,b) for(int i = a; i<b; i++)
#define debug(s) cout<<s
using namespace std;

float err = 1e-7;
int main(){
    #ifndef ONLINE_JUDGE
        // for getting input from input.txt
        freopen("input.in", "r", stdin);
        // for writing output to output.txt
        freopen("output.out", "w", stdout);
    #endif
    int a;
    while(scanf("%d",&a) == 1){
        int ans = 0;
        vector<int> v1;
        std::vector<int> v2;
        for(int i = a+1;i<=2*a;i++){
            for(int j = i; j<100000000000; j++){
                //printf("checking for i = %d and j = %d\n", i,j);
                if(abs(1.0/a-1.0/j - 1.0/i)<err){
                    //arr.push_back("1/%d = 1/%d + 1/%d\n"+a+j+i) ;
                    v1.push_back(j);
                    v2.push_back(i);
                    ans++;
                    break;
                }
                if(1.0/j + 1.0/i < 1.0/a){
                    break;
                }
            }
        }



        printf("%d\n", ans);
        for(int i = 0; i<ans; i++){
            printf("1/%d = 1/%d + 1/%d\n",a,v1.at(i),v2.at(i));
        }
    }

}

Any help would be appreciated.

EDIT: I am not taking input from the terminal or the small box that appears in Sublime text. I am taking input from a file and that is what makes the problem different from the suggested duplicate. The other methods like cancelling build don't help which would help the question asker in the sugested duplicate

  • 2
    Regarding your problem, this is a very good time to [learn how to debug your programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – Some programmer dude Jan 06 '19 at 09:20
  • 1
    Unrelated to your problem, please don't use competition/online judge sites to learn programming or languages. They will only teach you bad habits (like [including ``](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) or using macros that makes your code less readable and maintainable). If you want to learn C++ "properly", I suggest a few books from [this curated list](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282), or take classes. – Some programmer dude Jan 06 '19 at 09:23
  • @Someprogrammerdude - Thanks for your advice. I'll read both the articles. – Arhaan Ahmad Jan 06 '19 at 09:28
  • 2
    Also please don't just downvote. If anyone is downvoting atleast point out in the comments what is wrong with this question. I have tried researching on my own and will continue to do so until I get an answer. – Arhaan Ahmad Jan 06 '19 at 09:44
  • Possible duplicate of [Stop Sublime Text from executing infinite loop](https://stackoverflow.com/questions/28397473/stop-sublime-text-from-executing-infinite-loop) – hyde Jan 06 '19 at 11:35
  • To improve the question, write a minimal program (maybe cin and cout in while(true) or something) which exhibits the problem, then explain how the suggested duplicate (and other similar questions if you found some) doesn't work with that. – hyde Jan 06 '19 at 11:38
  • So what happens if you use the Cancel Build solution? Or, are you actually asking about your code, and the problem actually has nothing to do with your IDE/programming editor choice? If the latter, I would remove Sublime Text from tags and title (you can of course explain your environment in the question text). – hyde Jan 06 '19 at 15:01
  • 1
    @hyde This is about Sublime text 3. Cancelling build has no effect. `output.txt` continues getting filled. Even if I restart sublime text, it still keeps on filling. – Arhaan Ahmad Jan 06 '19 at 15:26

0 Answers0