the console/cmd has no response

Hi,i am a beginner to C++ and have just written a program to get the lowest common multiple. It worked all right but the console/cmd has no response if you input data. If you tell me why it is i would appreciate it.
the code as follows:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
 #include <iostream>
#include <cmath>
 
using namespace std;
 
int main()
{
   int n,a[1000];
   while(cin>>n)
   {
       int k=0;
       for(int i=0;i<n;i++)
       {
           cin>>a[i];
           if(k<a[i])
            k=a[i];
        }
        int i;
        do{
            for(i=0;i<n;i++)
            {
                if(k%a[i]!=0)
                 {
                 i--;
                 break;
                 }
            }
            k++;
        }while(i!=n-1);
 
        cout<<k-1<<endl;
   }
 
    return 0;
}

 
Last edited on
Hello kingdom,

Try this to see what is happening. It is best to use break points in the IDE, so that the program does not run completely.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <cmath>

using namespace std;

int main()
{
    int n, a[1000];

    while (std::cout << "\n Enter number of tries: " && cin >> n)
    {
        int k = 0;

        for (int i = 0; i < n; i++)
        {
            std::cout << " Enter a[" << i << "]: ";
            cin >> a[i];

            if (k < a[i])
                k = a[i];
        }

        int i;

        do
        {
            for (i = 0; i < n; i++)
            {
                if (k % a[i] != 0)
                {
                    std::cout << "   In do/while if statement i = " << i << " k = " << k << '\n';

                    i--;

                    break;
                }
            }

            k++;
        } while (i != n - 1);

        cout << k - 1 << endl;
    }

    return 0;
}

Based on the "do" and "for" loop. The "do/while" condition will always be true or maybe just a very long time.

The "break" statement only breaks the for loop. Not sure if that is what you want or not.

Then if you do reach the closing } of the outer while loop you just start over again. Not that I have managed to reach that point yet.

Using something like line 31 you can do this in different places to see the value of different variables.

Andy
Thank you for your comments. I have just changed the code and it is successful this time. But I have no idea about the difference between those two. it puzzles me.
the new code as follows:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
 
using namespace std;
 
int main()
{
   int n,a[1000];
   while(cin>>n)
   {
       int k=0;
       for(int i=0;i<n;i++)
       {
           cin>>a[i];
           if(k<a[i])
            k=a[i];
        }
        int j;
        do{
                j=0;
                for(int i=0;i<n;i++)
               {
                    if(k%a[i]==0)
                    j++;
                    else
                    break;
                }
                k++;
           }while(j!=n);
       cout<<k-1<<endl;
    }
    return 0;
}
Hello kingdom,

One thing that makes this hard to work on is I have no idea what your expected output should be. You never mentioned that part.

I will give this new code a test and see what happens.


But I have no idea about the difference between those two. it puzzles me.

What part?

Andy

P.S. Given a known input what should the output be?
Last edited on
This is where the debugger becomes your new friend. If you use the debugger to trace through the program and monitor the contents of the variables etc, then you'll see what's happening and where the program is deviating from what's expected from the design.
first,input"n" which represent the number of the number you will input. then, input them.finally,output their Lowest Common Multiple

for example:
input:
2 4 6
3 2 5 7
output:
12
70
thank you, Handy Andy.

Your code is very helpful.First, I am sorry that I didn't carefully study it before. After having just run it and carefully examined the output, I find my problem .

thank you very much,
I get it.

╰(*´︶`*)╯
kingdom
Hello seeplus,

thank you,too. You and Andy let me learn a lot.

kingdom
Topic archived. No new replies allowed.