# For loop problems

Page 1 of 1

## 7 Replies - 942 Views - Last Post: 29 August 2010 - 05:37 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=188240&amp;s=3b1231dd0805272c470a52bdff3a7b76&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Suryc

Reputation: 9
• 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 () {
bool numberChecker=true;
int totalPrime=0;

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

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

• D.I.C Lover

Reputation: 1397
• 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?

### #3 Suryc

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

## Re: For loop problems

Posted 29 August 2010 - 05:24 PM

Oler1s, 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.

### #4 Mohanddo

Reputation: 3
• 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++)
```

### #5 simeesta

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

## Re: For loop problems

Posted 29 August 2010 - 05:30 PM

Suryc, 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 () {
bool numberChecker=true;
int totalPrime=0;

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

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.

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.

### #6 Suryc

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

## Re: For loop problems

Posted 29 August 2010 - 05:31 PM

Mohanddo, 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 ==

simeesta, 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.

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

### #7 Mohanddo

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

## Re: For loop problems

Posted 29 August 2010 - 05:37 PM

Suryc, on 29 August 2010 - 04:31 PM, said:

Mohanddo, 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

### #8 simeesta

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

## Re: For loop problems

Posted 29 August 2010 - 05:37 PM

Suryc, on 30 August 2010 - 01:31 AM, said:

Mohanddo, 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.