Thread: problem with while loops

  1. #16
    *this
    Join Date
    Mar 2005
    Posts
    498
    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. #17
    *this
    Join Date
    Mar 2005
    Posts
    498
    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
    Last edited by JoshR; 07-08-2005 at 11:39 PM.

  3. #18
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    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.*

  4. #19
    Registered User
    Join Date
    Jun 2005
    Posts
    75
    Quote 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.

  5. #20
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Last edited by laserlight; 07-09-2005 at 09:09 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #21
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    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.

  7. #22
    Registered User
    Join Date
    Jun 2005
    Posts
    75
    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++;
    	}

  8. #23
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    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.

  9. #24
    Registered User
    Join Date
    Jun 2005
    Posts
    75
    Quote 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;
    }

  10. #25
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    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.

  11. #26
    Registered User
    Join Date
    Jun 2005
    Posts
    75
    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;

  12. #27
    Registered User
    Join Date
    Jun 2005
    Posts
    75
    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.

  13. #28
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    #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;
    )
    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.

  14. #29
    Registered User
    Join Date
    Jun 2005
    Posts
    75
    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++';
    	}

  15. #30
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    >>comments?
    Of course.

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

    Try this:
    Code:
    char c = 'A';
    while(c <= 'Z') {
        cout << c;
        c ++;
    }
    [edit]
    Or
    Code:
    for(char c = 'A'; c <= 'Z'; c ++) cout << c;
    [/edit]
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP with windows message loops problem
    By cloudy in forum C++ Programming
    Replies: 3
    Last Post: 01-21-2006, 01:09 PM
  2. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  3. searching problem
    By DaMenge in forum C Programming
    Replies: 9
    Last Post: 09-12-2005, 01:04 AM
  4. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM
  5. binary tree problem - help needed
    By sanju in forum C Programming
    Replies: 4
    Last Post: 10-16-2002, 05:18 AM