Piyush Shandilya
Piyush Shandilya

Reputation: 75

Simple code in C++ compiling but not executing in Linux

I am trying to compile and execute a simple code in C++ on a linux machine. But the program gets stuck in the middle of the code. I can't find the reason.

Here is the code

#include <iostream>

using namespace std;
int n;
int product =1;
int counter =0;
int p;
int main()
{
    //return 1;
    cout << "How many numbers?" << endl;
    cin >> n ;
    cout << "Input the numbers " << endl;
    for(int i=0;i<n;i++)
        {
                cin >> p;
                product = product*p;
                int p = 1;
        }
        cout << "Now our number to be factorised is " << product << endl;
        cin >> p;
        for(int i=1;i=product;i++)
        {
         if(product%i==0)
         counter++;
        }
         cout << "the number of factors is " << counter << endl;
          return 0;
}

The code gets stuck at "Now our number to be factorised is " product. It calculates the product but doesn't progress any further

Upvotes: 1

Views: 247

Answers (2)

Grijesh Chauhan
Grijesh Chauhan

Reputation: 58251

Because infinite loop, in second for loop you have misspelled == :

for(int i=1;i=product;i++)
             ^
               should be ==

Side note: To minimize such kind of bugs in you code I would suggest you to keep space in expression e.g. an expression i=product should be written as i = product, so that its readable. similarly you should add space after ; and ,.

Upvotes: 10

Shafik Yaghmour
Shafik Yaghmour

Reputation: 158449

It looks likes you have a typo on this line:

for(int i=1;i=product;i++)
             ^

you are using assignment(=) instead of logical equals(==). So this is effectively an infinite loop since the result of this expression is true.

Upvotes: 0

Related Questions