7 Replies - 892 Views - Last Post: 29 August 2010 - 05:37 PM Rate Topic: -----

#1 Suryc  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 105
  • Joined: 15-August 10

For loop problems

Posted 29 August 2010 - 05:06 PM

Alright, so I'm trying to do #3 on Martyr2's mega protect list (I order it from the top, so #3=Prime Factorization)

Here is the code:

#include <iostream>
#include <conio.h>

using namespace std;

int main () {
    int primeNumber=0;
    bool numberChecker=true;
    int totalPrime=0;

    while (numberChecker==true) {
        cout << "Please enter the number that you want to see prime factors of (1-1000): ";
        cin >> primeNumber;
        if (cin.good() && primeNumber >=1 && primeNumber <=1000) {
            numberChecker=false;
            break;
        }
        else {
            cin.clear();
            cin.ignore(numeric_limits<streamsize>::max(), '\n');
            cout << "Error: Input unknown" << endl;
        }
    }

    for (int Counter=1;Counter==primeNumber;Counter++) {
        if (primeNumber % Counter == 0) {
            cout << Counter << endl;
            totalPrime++;
        }
    }

    if (totalPrime==1)
        cout << "This number is a prime number. " << endl;
    else
        cout << "This number has " << totalPrime << " factors. " << endl;

    /*Hold program open*/
    while (1) {
        if (_kbhit()) {
            break;
        }
    }
    return 0;
}


How I tried to set the for loop to work

  • The program asks for input and makes sure it's a valid number
  • The for loop checks if the primeNumber variable can be divided by 1 equally
  • If it can be, it prints 1; if it can't be the for loop just adds 1 to Counter and tries again
  • Then it checks if it can be divided by 2 equally
  • If it can be, it prints 2; if it can't be the for loop just adds 1 to the counter and tries again
  • When Counter == primeNumber, it exits and you have a list of all of the numbers that can be evenly divided into your number


What actually happens:

  • The program asks for input and makes sure it's a valid number
  • The program prints "This number is a prime number" if the previous input is 1
  • The program goes to the _kbhit part of the function if it's anything other than 1


How can I fix this?

This post has been edited by Suryc: 29 August 2010 - 05:07 PM


Is This A Good Question/Topic? 0
  • +

Replies To: For loop problems

#2 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1397
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: For loop problems

Posted 29 August 2010 - 05:22 PM

Are you telling us that your program doesn't tell you how many factors there are for input greater than 1?
Was This Post Helpful? 0
  • +
  • -

#3 Suryc  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 105
  • Joined: 15-August 10

Re: For loop problems

Posted 29 August 2010 - 05:24 PM

View PostOler1s, on 29 August 2010 - 04:22 PM, said:

Are you telling us that your program doesn't tell you how many factors there are for input greater than 1?

Yes.
Was This Post Helpful? 0
  • +
  • -

#4 Mohanddo  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 50
  • Joined: 07-July 09

Re: For loop problems

Posted 29 August 2010 - 05:30 PM

Try changing

for (int Counter=1;Counter==primeNumber;Counter++)


to

for (int Counter=1;Counter < primeNumber;Counter++)

Was This Post Helpful? 2
  • +
  • -

#5 simeesta  Icon User is offline

  • Deadly Ninja


Reputation: 221
  • View blog
  • Posts: 594
  • Joined: 04-August 09

Re: For loop problems

Posted 29 August 2010 - 05:30 PM

View PostSuryc, on 30 August 2010 - 01:06 AM, said:

Alright, so I'm trying to do #3 on Martyr2's mega protect list (I order it from the top, so #3=Prime Factorization)

Here is the code:

#include <iostream>
#include <conio.h>

using namespace std;

int main () {
    int primeNumber=0;
    bool numberChecker=true;
    int totalPrime=0;

    while (numberChecker==true) {
        cout << "Please enter the number that you want to see prime factors of (1-1000): ";
        cin >> primeNumber;
        if (cin.good() && primeNumber >=1 && primeNumber <=1000) {
            numberChecker=false;
            break;
        }
        else {
            cin.clear();
            cin.ignore(numeric_limits<streamsize>::max(), '\n');
            cout << "Error: Input unknown" << endl;
        }
    }

    for (int Counter=1;Counter==primeNumber;Counter++) {
        if (primeNumber % Counter == 0) {
            cout << Counter << endl;
            totalPrime++;
        }
    }

    if (totalPrime==1)
        cout << "This number is a prime number. " << endl;
    else
        cout << "This number has " << totalPrime << " factors. " << endl;

    /*Hold program open*/
    while (1) {
        if (_kbhit()) {
            break;
        }
    }
    return 0;
}


How I tried to set the for loop to work

  • The program asks for input and makes sure it's a valid number
  • The for loop checks if the primeNumber variable can be divided by 1 equally
  • If it can be, it prints 1; if it can't be the for loop just adds 1 to Counter and tries again
  • Then it checks if it can be divided by 2 equally
  • If it can be, it prints 2; if it can't be the for loop just adds 1 to the counter and tries again
  • When Counter == primeNumber, it exits and you have a list of all of the numbers that can be evenly divided into your number


What actually happens:

  • The program asks for input and makes sure it's a valid number
  • The program prints "This number is a prime number" if the previous input is 1
  • The program goes to the _kbhit part of the function if it's anything other than 1


How can I fix this?


the for loop has the following syntax

for (int Counter=1;Counter==primeNumber;Counter++)

which is saying counter is one;
while counter is equal to prime number, do stuff, and increment counter by one.

counter == primenumber will only happen once. when primeNumber is one

you want
for(int Counter=1;Counter<primeNumber;Counter++)

which will test counter values from 1 to primenumber whereas the 1st one tests while counter is equal to the prime number.
Was This Post Helpful? 1
  • +
  • -

#6 Suryc  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 105
  • Joined: 15-August 10

Re: For loop problems

Posted 29 August 2010 - 05:31 PM

View PostMohanddo, on 29 August 2010 - 04:30 PM, said:

Try changing

for (int Counter=1;Counter==primeNumber;Counter++)


to

for (int Counter=1;Counter < primeNumber;Counter++)

That's weird, why would it work for less than but not ==?

Thank you very much, by the way, (it worked) just curious. I thought for comparing you're supposed to use ==

View Postsimeesta, on 29 August 2010 - 04:30 PM, said:

the for loop has the following syntax

for (int Counter=1;Counter==primeNumber;Counter++)

which is saying counter is one;
while counter is equal to prime number, do stuff, and increment counter by one.

counter == primenumber will only happen once. when primeNumber is one

you want
for(int Counter=1;Counter<primeNumber;Counter++)

which will test counter values from 1 to primenumber whereas the 1st one tests while counter is equal to the prime number.


Oooh XD Simple simple, thanks :]

This post has been edited by Suryc: 29 August 2010 - 05:35 PM

Was This Post Helpful? 0
  • +
  • -

#7 Mohanddo  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 50
  • Joined: 07-July 09

Re: For loop problems

Posted 29 August 2010 - 05:37 PM

View PostSuryc, on 29 August 2010 - 04:31 PM, said:

View PostMohanddo, on 29 August 2010 - 04:30 PM, said:

Try changing

for (int Counter=1;Counter==primeNumber;Counter++)


to

for (int Counter=1;Counter < primeNumber;Counter++)

That's weird, why would it work for less than but not ==?

Thank you very much, by the way, (it worked) just curious. I thought for comparing you're supposed to use ==


Because only when that expression is false will it exit that for loop.

When it is ==primeNumber it will always be false until it is equal to primeNumber which will not happen.

This post has been edited by Mohanddo: 29 August 2010 - 05:38 PM

Was This Post Helpful? 0
  • +
  • -

#8 simeesta  Icon User is offline

  • Deadly Ninja


Reputation: 221
  • View blog
  • Posts: 594
  • Joined: 04-August 09

Re: For loop problems

Posted 29 August 2010 - 05:37 PM

View PostSuryc, on 30 August 2010 - 01:31 AM, said:

View PostMohanddo, on 29 August 2010 - 04:30 PM, said:

Try changing

for (int Counter=1;Counter==primeNumber;Counter++)


to

for (int Counter=1;Counter < primeNumber;Counter++)

That's weird, why would it work for less than but not ==?

Thank you very much, by the way, (it worked) just curious. I thought for comparing you're supposed to use ==

the for loop will used when the middle condition is true. so when will the expression
(counter==primenumber)
be true?

when primenumber is 1 is the only case.

(counter<primenumber)

will be true for counter is 1 to primenumber-1 and will stop when they are equal.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1