C++ Why the loop going in infinite loop?

  • Context: Comp Sci 
  • Thread starter Thread starter Raghav Gupta
  • Start date Start date
  • Tags Tags
    C++ Infinite Loop
Click For Summary

Discussion Overview

The discussion revolves around a C++ programming problem related to finding the greatest common divisor (GCD) or highest common factor (HCF) of two numbers. Participants explore issues with an infinite loop in the initial implementation and suggest modifications to the code.

Discussion Character

  • Homework-related
  • Technical explanation

Main Points Raised

  • One participant describes their initial code and expresses concern about an infinite loop occurring during the GCD calculation.
  • Another participant suggests using debugging statements to track the values of variables and understand the flow of the program.
  • Several participants point out that the placement of the initialization of variable 'i' is incorrect, leading to repeated execution during each loop iteration.
  • A participant later shares a revised version of their code, indicating that the changes made resolved the infinite loop issue and produced the correct output.

Areas of Agreement / Disagreement

Participants generally agree on the need to correct the placement of variable initialization to avoid the infinite loop, and the discussion appears to have resolved the initial problem with the code.

Contextual Notes

There may be limitations related to the assumptions about the input values and the handling of edge cases, which are not fully explored in the discussion.

Who May Find This Useful

Readers interested in C++ programming, particularly those learning about loops, debugging techniques, and GCD calculations, may find this discussion beneficial.

Raghav Gupta
Messages
1,010
Reaction score
76

Homework Statement


I have to find GCD or HCF of two numbers.

Homework Equations

The Attempt at a Solution


Code:
#include<iostream.h>
#include<conio.h>
void main()
{
    clrscr();
    int i,m,n,temp;
    count<<"Enter two numbers\n";
    cin>>m>>n;
    do
    {
    if(n>m)
    {
     temp=n;
     n=m;
     m=temp;   // this loop I think it is correct but still it is going to an infinite loop.
    }
     i=n;
     if(m%i==0&&n%i==0)
     break;
     i--;
    }while(i>=1);
    count<<"The HCF is\t"<<i;
    getch();
}
 
Physics news on Phys.org
Use some printf statements for debugging (or count)

See if your code is doing what you think it is doing.

I usually define a debug flag and have debugging statements such as

if(debug) printf("%d %d\n", num, num2);

etc
 
  • Like
Likes   Reactions: Raghav Gupta
Where you give i its starting value, you have incorrectly placed the statement so that it is executed during every pass through the loop. You should place it where it gets executed once only.
 
NascentOxygen said:
Where you give i its starting value, you have incorrectly placed the statement so that it is executed during every pass through the loop. You should place it where it gets executed once only.
Thanks,
Now I am able to execute and get correct output.
My code looks like this now,
Code:
#include<iostream.h>
#include<conio.h>
void main()
{
    clrscr();
    int i,m,n,temp;
    count<<"Enter two numbers\n";
    cin>>m>>n;
    i=n;

    do
    {

      if(m%i==0&&n%i==0)
      break;
      i--;
    }while(i>=1);
     count<<"The HCF is\t"<<i;
     getch();
}
 
  • Like
Likes   Reactions: NascentOxygen

Similar threads

  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 14 ·
Replies
14
Views
4K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
8
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 13 ·
Replies
13
Views
3K