Thread: Need help with while loop

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    3

    Question Need help with while loop

    I'm just starting out so bare with me

    I'm in the middle of trying to create a program using while loop that has the user input two integers then displays the odd numbers between those numbers and again with the even numbers. I know I probably have to use a counter but how do I go about starting it. BTW there is more to the program but this is the part I'm struggling with. Thanks for any help you can give.

  2. #2
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    Welcome to the boards!

    I'd suggest maybe posting some code you've already made so far, but from what it sounds like you're trying to do, you will want to look in to the modulus operator. This operator returns the remainder of a division operation.

    So, for example, if you were to do:

    10%2

    the result would be 0, as 10/2 equals 5 without any remainder.

    Now, however, if you did:

    11%2

    you'd get 1, because 11/2 equals 5.5, and 0.5*2=1 which is the result.


    So, using this logic, you can then figure out if you're on an odd or even number, and using that knowledge, use a counter and increment by two (+=2) until you're at the end variable.

    -Hope that helps

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    3
    OK I'm not even close and his is not how I wanted to spend my bday lol The actual project is write a program that uses while loops to prefoem the following steps:
    *prompt user to imput two integers (firstnum smaller than second)
    *output all odd number between the two numbers
    *output the sum of the even numbers between the two numbers
    *output the numbers and their square between 1 and 10
    output all uppercase letter (IE A B C D ...)
    Here's what I have so far. Like I said I said I'm just starting out, actually just been about two weeks so far.

    (I was just trying to work out the odd number part so far)

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	int num1, num2;
    	int odds;
    	int evens;
    
    	cout << "Enter two integers: " << "First number must be smaller than the second number";
    	cin >> num1, num2;
    	cout << endl;
    
    	if (num1 % 2 == 0)
    		num1 = odds;
    	
    	else if (num1 % 2 != 0)
    		num1 = evens;
    
    	while(odds > num1 && odds < num2)
    	{
    
    		cout << odds << endl;
    		odds += 2;
    	}
    	
    	cout << odds << endl;
    	cout << "did you get it right?" << endl;
    
    	return 0;
    }
    OK I know I'm really off but just how far off am I?

    I think I will not choose an online class for programming next time, gotta hear the teacher explain this..
    Last edited by eden; 01-21-2005 at 06:00 PM. Reason: added tags

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    46
    try doing this:

    Code:
    //for your user input, this makes it more reliable
    
    cout << "Please enter an integer: ";
    cin >> num1;
    cout << endl << "Please enter another integer: ";
    cin >> num2;
    cout << endl;
    
    //your if statement is fine, but you also need to find the higher number, otherwise your while loop won't work right
    
    int upper;
    if (num1 > num2)
      upper = num1;
    else
      upper = num2;
    
    while (odds <= upper)
    {
      cout << upper << endl;
      odds += 2;
    }

  5. #5
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    First, let me point-out one thing, and a hint...

    num1 = odds;

    The equal sign assigns the value on the right to the variable on the left. It looks like equation, but it's not. It's an assignment statement. This won't work for you, 'cause odds doesn't have a value.

    And here's the big hint:

    Develop your code a few lines at a time. Put in some extra (temporary) cout statements to check your progress.

    First, write the code to input the two numbers. Maybe add a cout statement to display the variable names and their values.

    Compile and Test-run

    Add an if-statement that displays "odd" if the first number is odd.

    Compile and Test-run

    When that works, add the "else" logic to display "even" if the first number is even.

    Compile and Test-run

    Now, make a loop that displays all of the numbers.

    Compile and Test-run

    Put your (working) odd-even logic into the loop... Display all of the numbers, and display the word "odd" or "even" after each one.

    Compile and Test-run

    Revise the if-logic so instead or displaying "odd" or "even" after each number, it displays just the odd numbers.

    Compile and Test-run

    Copy the loop and revise it so it displays the even numbers.

    Compile and Test-run,

    Add the code to sum the even numbers, etc., etc., etc...

    Of course, you don't have to follow these exact steps, but you get the idea. As you gain experience and confidance, you can compile and test less frequently, but even expert programmers test their code as they develop it. Nobody writes a whole program before compiling & running.

    I'm not suggesting that you design your programs a few lines at a time... you should have at-least an overall plan before you start... just write the actual code a few lines at a time.

    You probably haven't studied functions yet, but using this same approach when writing functions will save you lots of greif.
    Last edited by DougDbug; 01-21-2005 at 07:49 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 10:21 PM
  2. While loop misbehaving (or misunderstanding)
    By mattAU in forum C Programming
    Replies: 2
    Last Post: 08-28-2006, 02:14 AM
  3. loop in a linked linked lists
    By kris.c in forum C Programming
    Replies: 6
    Last Post: 08-26-2006, 12:38 PM
  4. while loop help
    By bliznags in forum C Programming
    Replies: 5
    Last Post: 03-20-2005, 12:30 AM
  5. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 09:14 AM