This is your problem:
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.Code:counter = (2 * firstNum) - 1;
This is your problem:
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.Code:counter = (2 * firstNum) - 1;
I would approach it like this:
I like the colorsCode:/* 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;
Last edited by JoshR; 07-08-2005 at 11:39 PM.
RE: modulus
FWIW: http://groups-beta.google.com/group/...9a558db?hl=en&
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
Originally Posted by JoshR
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.
The trick is to recognise that:How could I take what you've given me here and make it into a while loop?
is equivalent to:Code:for (init-statement condition; expression) { statement }
e.g.Code:{ init-statement while (condition) { statement expression; } }
is equivalent to:Code:for (int i = 0; i < 10; ++i) { std::cout << i << std::endl; }
Okay, they arent exactly the same, e.g. if you use continue, but the differences should be rather minor.Code:{ int i = 0; while (i < 10) { std::cout << i << std::endl; ++i; } }
Last edited by laserlight; 07-09-2005 at 09:09 AM.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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.
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++; }
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.Originally Posted by Daved
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; }
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.
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;
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.
What's that for? I've never seen it before.#include <iomanip>
Was that supposed to beCode:if (counter % 2 != 0);
Code:if (counter % 2 != 0) {This increments the counter. Maybe you wantCode:counter++;
(Which is, by the way, the same asCode:counter += 2;
)Code:counter = counter + 2;
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
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! lolCode:#include <iomanip>
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++'; }
>>comments?
Of course.
Always true.Code:while ('A' <= 'Z')
Try this:
[edit]Code:char c = 'A'; while(c <= 'Z') { cout << c; c ++; }
Or
[/edit]Code:for(char c = 'A'; c <= 'Z'; c ++) cout << c;
Last edited by dwks; 07-09-2005 at 12:53 PM.
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.