Thread: problem with while loops

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    75

    Question problem with while loops

    I'm learning while loops and I'm having trouble with this program I'm writing. The user inputs 2 numbers and I have to be able to display different things, like the odd numbers between the numbers entered. This is what I have so far. If someone could steer me in the right direction, I'd really appreciate it.

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    	int firstNum;
    	int secondNum;
    
    	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;
    
    	while (firstNum <= secondNum)
    	{
    		cout << "Odd integers between " << firstNum << " and " 
    			 << secondNum << "are: ";

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    For starters, do you know how to test if an integer is odd/even?
    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

  3. #3
    Registered User mrafcho001's Avatar
    Join Date
    Jan 2005
    Posts
    483
    the easiest way to find if a number is odd is to use the module %
    what it does is it devides and returns the remainder for example:
    10%3
    would return 1
    10%2
    would return 0

    so if a number is divisable by 2 and no remainder is left than its even.. and if has a remainder than its odd

  4. #4
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    nope the easiest way is odd=number_to_test & 1;
    Miles quicker than modulo
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  5. #5
    Registered User
    Join Date
    Dec 2004
    Location
    UK
    Posts
    109
    Quote Originally Posted by Stoned_Coder
    nope the easiest way is odd=number_to_test & 1;
    Miles quicker than modulo
    I'd say that's the fastest not the easiest since it relies on bitwise operators and binary number representation which might not be that obvious to a beginner

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    75
    Ok, so I've tried to incorporate what you all have said. Let me know if this looks alright to you all!

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    	int firstNum;
    	int secondNum;
    	int sum;
    	int squareSum;
    
    	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;
    
    	while (firstNum <= secondNum)
    	{
    		cout << "Odd integers between " << firstNum << " and " 
    			 << secondNum << "are: \n";
    		if (firstNum % 2 == 0)
    		{
    			firstNum = (2 * firstNum) - 1;
    			cout << firstNum << " " << endl;
    		}
    		else if (firstNum % 2 == 1)
    		{
    			firstNum = firstNum + 2;
    			cout << firstNum << " " << endl;
    		}

  7. #7
    *this
    Join Date
    Mar 2005
    Posts
    498
    like stoned coder said you should do this:
    Code:
    int number;
    cin >> number;
    if (number & 1) cout << "ODD";
    else cout << "EVEN";
    What it does is bit operates on the smallest bit of number.

    1 & 1 = 1
    0 & 1 = 0

    etc...

    Since the smallest bit has a value of 1 which is 2 to power of 0 so its the only bit that could make a number odd.

  8. #8
    *this
    Join Date
    Mar 2005
    Posts
    498
    Although it may be a little advanced, you should check out bit operations. But if its because your trying to get a hang of modulus operator then go ahead.

    also what is this for:
    Code:
    firstNum = (2 * firstNum) - 1;
    why not just do this
    Code:
    firstNum++;
    It will just up firstNum to make it odd

  9. #9
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    Quote Originally Posted by Stoned_Coder
    nope the easiest way is odd=number_to_test & 1;
    Miles quicker than modulo
    You are heavily underestimiting the intelligence of compiler technology.

    "Every" compiler will optimize (a % 2) into (a & 1).

  10. #10
    Registered User
    Join Date
    Jun 2005
    Posts
    75
    Quote Originally Posted by JoshR
    Although it may be a little advanced, you should check out bit operations. But if its because your trying to get a hang of modulus operator then go ahead.

    also what is this for:
    Code:
    firstNum = (2 * firstNum) - 1;
    why not just do this
    Code:
    firstNum++;
    It will just up firstNum to make it odd

    I'm not trying to increment "firstNum" by one every time... I'm trying to display the odd numbers between whatever two numbers the user inputs. But does everything look ok as far as my set up? If I try to do something more with "firstNum" after what I've done already, how do I change it back to what the user input for "firstNum"? Cuz through the "while" and "if" stuff, didn't I change it? I need to be able to use the original "firstNum" again in the program. Suggestions?

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> I'm not trying to increment "firstNum" by one every time
    You should be. You should go through each number between firstNum and secondNum, one number at a time in the while loop. To do this, increment firstNum by one every time through the loop.

    >> I need to be able to use the original "firstNum" again in the program.
    Then create a new variable with a different name, like counter or something. Before your loop, assign it to the value of firstNum. The increment it each time and leave firstNum alone.

  12. #12
    Registered User
    Join Date
    Jun 2005
    Posts
    75
    So would I sort through all the numbers between the first and second number into odd and even in an "if...else" or what?

  13. #13
    *this
    Join Date
    Mar 2005
    Posts
    498
    Just change firstNum line to firstNum++; and it will work, see if you can figure out the logic...

  14. #14
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Miles quicker than modulo
    Modulus is obvious though. The same can't be said for AND. Then, of course, there's the argument that such micro-optimizations are silly until you know that they'll make a difference. And in the end, a wise choice of algorithm will usually make the point moot.
    My best code is written with the delete key.

  15. #15
    Registered User
    Join Date
    Jun 2005
    Posts
    75
    Ok, this is my "finished" code for what I've been trying to do. But I don't know why my loops are infinite when I execute the program so I can't check to see if the rest of my program runs the way I want it to. Hopefully you all can check my code out and let me know what I'm doing wrong or missing. Thanks.

    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;
    	
    	sum = 0;
    	counter = firstNum;
    	while (counter <= secondNum)
    	{
    		cout << "Odd integers between " << firstNum << " and " 
    			 << secondNum << " are: \n";
    			counter = (2 * firstNum) - 1;
    		    counter++;
    			cout << counter << " " << endl;
    		
    		cout << "Sum of even integers between " << firstNum << " and "
    			 << secondNum << " = ";
    		
    		counter = firstNum;
    		sum = sum + counter;
    			counter = (2 * firstNum);
    			counter++;
    			cout << sum << " " << endl;
    	}
    
    	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;
    }

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