Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to make this loop functional after the first run?

This is my code, but after the first try, no matter the given number, it only says "number is not perfect", and bypass the "for" loop.

#include <iostream>

using namespace std;

int main() {
  int n, i, x = 1, s = 1;
  char z;

  while (x) {
    cout << "please enter a number for incpection : ";
    cin >> n;

    for (i = 2; i < n; i++) {
      if ((n % i) == 0)
        s += (n / i);
    }

    if (s == n) {
      cout << "the entered number is complete\n";
    } else {
      cout << "number is not perfect\n";
    }

    cout << "do you wish to continue ?(y/n) : ";
    cin >> z;

    if (z == 'y')
      continue;
    else
      x = 0;
  }

  return 0;
}

I tried checking the Syntax and "{" repositioning but didn’t work

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The problem really lies in the fact that for some reason, you introduce and set all the variables only at the beginning of the function; that can’t work, because you need to "reset" s after every number. A version of your code that introduces the variables where they are actually "living" solves that:

#include <iostream>
using namespace std;
int main() {
  bool x = true;
  while (x) {
    int n;
    cout << "please enter a number for incpection : ";
    cin >> n;

    int s = 1;
    for (int i = 2; i < n; i++) {
      if ((n % i) == 0)
        s += (n / i);
    }
    if (s == n) {
      cout << "the entered number is complete\n";
    } else {
      cout << "number is not perfect\n";
    }
    cout << "do you wish to continue ?(y/n) : ";
    
    char z;
    cin >> z;
    if (z != 'y')
      x = false;
  }
  return 0;
}

Because we do want you to learn to code successfully, I’ll also point out that your variable names are very bad, and that makes it hard, already at your small program size, to reason (as a human) about what they do. Variable names don’t "cost" anything, so use them to describe what you’re doing. 100% same code as above, but much clearer to me, if I need to read this same code tomorrow morning:

#include <iostream>
using namespace std;
int main() {
  bool done = false;
  while (!done) {
    int test_number;
    cout << "please enter a number for incpection : ";
    cin >> test_number;

    int sum_of_divisors = 1;
    for (int i = 2; i < test_number; i++) {
      if ((test_number % i) == 0)
        sum_of_divisors += (test_number / i);
    }
    if (sum_of_divisors == test_number) {
      cout << "the entered number is complete\n";
    } else {
      cout << "number is not perfect\n";
    }

    char answer;
    cout << "do you wish to continue ?(y/n) : ";
    cin >> answer;
    done = (answer != 'y');
  }
  return 0;
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading