# Thread: problem with while loops

1. This is your problem:
Code:
`counter = (2 * firstNum) - 1;`
I still don't see a purpose for it at all. It also resets your counter on each iteration. So, it makes your while statement never become false.

2. I would approach it like this:
Code:
```   /* Store counter and sum for use in for loops */
int counter, sum;

cout << "Odd integers between " << firstNum << " and " << secondNum
<< " are: " << endl;

for (counter = firstNum; counter <= secondNum; counter++)
if (counter % 2 != 0) cout << counter << " ";

for (counter = firstNum, sum = 0; counter <= secondNum; counter++)
if (counter % 2 == 0) sum += counter;

cout << "Sum of even integers between " << firstNum << " and "
<< secondNum << " = " << sum << endl;```
I like the colors

3. Originally Posted by JoshR
I would approach it like this:
Code:
```   /* Store counter and sum for use in for loops */
int counter, sum;

cout << "Odd integers between " << firstNum << " and " << secondNum
<< " are: " << endl;

for (counter = firstNum; counter <= secondNum; counter++)
if (counter % 2 != 0) cout << counter << " ";

for (counter = firstNum, sum = 0; counter <= secondNum; counter++)
if (counter % 2 == 0) sum += counter;

cout << "Sum of even integers between " << firstNum << " and "
<< secondNum << " = " << sum << endl;```
I like the colors

How could I take what you've given me here and make it into a while loop? What you put up there helped me, but I'm supposed to write the program first with while loops, then again with for loops. LOL.

4. How could I take what you've given me here and make it into a while loop?
The trick is to recognise that:
Code:
```for (init-statement condition; expression) {
statement
}```
is equivalent to:
Code:
```{
init-statement
while (condition) {
statement
expression;
}
}```
e.g.
Code:
```for (int i = 0; i < 10; ++i) {
std::cout << i << std::endl;
}```
is equivalent to:
Code:
```{
int i = 0;
while (i < 10) {
std::cout << i << std::endl;
++i;
}
}```
Okay, they arent exactly the same, e.g. if you use continue, but the differences should be rather minor.

5. Use your own code MyntiFresh. You changed too much between your last two versions. Get one thing to work first before moving on to the next. If you add the counter to the code above that uses the % to find even or odd, and then remove all the (2 * firstNum) - 1 stuff which is totally unnecessary, then you will be closer to your solution for even/odd. Once that is done you can worry about the sum and other stuff.

6. OK, so I tried to make it look like you said.... but it's still an infinite loop. Here's the part I'm having trouble with...

Code:
```	counter = firstNum;
while (counter <= secondNum)
{
cout << "Odd integers between " << firstNum << " and "
<< secondNum << " are: \n";
if (counter % 2 != 0)
cout << counter << " " << endl;
counter++;
}

sum = 0;
while (counter <= secondNum)
{
if (counter % 2 == 0)
sum += counter;
cout << "Sum of even integers between " << firstNum << " and "
<< secondNum << " = " << sum << endl;
counter++;
}```

7. The odd part works fine for me. Good job.

The first part to fixing the sum loop is to reset counter to the value of firstNum before that loop. Once you do that, the sum part works as well, you just have to figure out where your output statements should be to make it look nicer.

8. Originally Posted by Daved
The odd part works fine for me. Good job.

The first part to fixing the sum loop is to reset counter to the value of firstNum before that loop. Once you do that, the sum part works as well, you just have to figure out where your output statements should be to make it look nicer.
Alright, I reset counter back to firstNum. But I'm still getting the same infinite loop when I execute. Could it be somewhere else in my program that's making it do that? I know they frown upon people putting up their entire code, but I'm just getting so frustrated with this.

Code:
```#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
int firstNum;
int secondNum;
int sum;
int squareSum;
int counter;
int i;
int square;

cout << "Enter two numbers." << endl;
cout << "First number must be less than or equal "
<< "to the second number you enter" << endl;
cout << "Enter numbers: ";
cin >> firstNum >> secondNum;
cout << endl;

counter = firstNum;
while (counter <= secondNum)
{
cout << "Odd integers between " << firstNum << " and "
<< secondNum << " are: \n";
if (counter % 2 != 0)
cout << counter << " " << endl;
counter++;
}

sum = 0;
counter = firstNum;
while (counter <= secondNum)
{
if (counter % 2 == 0)
sum += counter;
cout << "Sum of even integers between " << firstNum << " and "
<< secondNum << " = " << sum << endl;
counter++;
}

cout << left << setw(30) << "Number"
<< setfill(' ') << right << "Square of Number" << endl;
while (i <= 10)
{
square = i * i;
cout << setw(30) << i << setfill(' ') << right << square << endl;
i++;
}

squareSum = 0;
counter = firstNum;
cout << "Sum of the squares of odd integers between "
<< firstNum << " and " << secondNum << " = ";
while (counter <= secondNum)
{
counter = (2 * firstNum) - 1;
squareSum = squareSum + (counter * counter);
counter++;
cout << squareSum << endl;
}

while ('A' <= 'Z')
{
cout << "Upper case letters are: "
<< 'A' << " " << endl;
'A++';
}

return 0;
}```

9. I highly suggest commenting out everything after the sum (up to the return 0) and making sure that part works. It's better to get each part to work one at a time. If I do that, then now the odd part and the sum part work pretty well. Concentrate on those to get them the way you want them, and then move on to the next part.

When you are ready to move on to the next part, then you will quickly realize that your while loop goes while i is less than or equal to 10. But you never initialized i, so it starts out with a garbage number (like -858993460) and keeps adding one each time. That will take a long time to get to 10. First get the first two parts working, and then work on the third part by setting i to an intial value.

10. Thank you so much for your help. I've finally gotten almost everything to work now. But now I'm stuck on trying to display the sum of the squares of the odd integers.... here's the code for it.... suggestions?

Code:
```counter = firstNum + 1;
squareSum = 0;
while (counter < secondNum)
{
if (counter % 2 != 0);
squareSum = squareSum + (counter * counter);
counter++;
}
cout << "Sum of the squares of odd integers between "
<< firstNum << " and " << secondNum << " = ";
cout << squareSum << endl;```

11. When I execute what I have above, it gives me the sum of all the squares between the two numbers, not just the sum of the squares of the odds between the two numbers. I just can't figure out how to fix that.

12. #include <iomanip>
What's that for? I've never seen it before.

Code:
`if (counter % 2 != 0);`
Was that supposed to be
Code:
`if (counter % 2 != 0) {`
Code:
`counter++;`
This increments the counter. Maybe you want
Code:
`counter += 2;`
(Which is, by the way, the same as
Code:
`counter = counter + 2;`
)

13. Code:
`#include <iomanip>`
You have to use that in order to use the "setfill" and "setw" stuff. And the reason why that part of my program wasn't working right was just cuz of one stupid little semicolon! lol

Now I have one last question. I'm obviously writing the code wrong for what I'm trying to do here. All I want is to print all the upper case letters of the alphabet. Here's what I have.... comments???

Code:
```while ('A' <= 'Z')
{
cout << "Upper case letters are: "
<< 'A' << " " << endl;
'A++';
}```

Of course.

Code:
`while ('A' <= 'Z')`
Always true.

Try this:
Code:
```char c = 'A';
while(c <= 'Z') {
cout << c;
c ++;
}```

Or
Code:
`for(char c = 'A'; c <= 'Z'; c ++) cout << c;`
[/edit]

Popular pages Recent additions