Home > OS >  how to make this loop functional after the first run?
how to make this loop functional after the first run?

Time:01-08

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

CodePudding user response:

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;
}

CodePudding user response:

As pointed in the comments, the way you declared your variables at the start of the function body prevents s for restarting to 1 after the first run.

You should declare your variables "as late as possible". Something like that:

#include <iostream>

using namespace std;

int main() {
  int x = 1;

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

    int n;
    cin >> n;

    int s = 1; // fresh new s value for each loop iteration

    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') {
      continue;
    } else {
      x = 0;
    }*/
    if (z == 'n') { // simpler version
      x = 0;
    }
  }

  return 0;
}
  • Related