Simple while loop

This is a discussion on Simple while loop within the C++ Programming forums, part of the General Programming Boards category; Sorry to be wasting your time with this simple problem, but I'm in my first class and am stumped on ...

  1. #1
    Registered User
    Join Date
    Mar 2010

    Angry Simple while loop

    Sorry to be wasting your time with this simple problem, but I'm in my first class and am stumped on a while loop.

    Here is what I have:

    #include <iostream> 
    int main() {
       int i = 100;
        while (i <= 150){
          cout << i << " ";
           i = i + 2;}
    return 0;}

    The program runs just fine, but the numbers all appear on one line. I need to have only 6 per line, for example 100 102 104 106 108 110
    112 114 116 etc.... to 150

    Keep in mind I have to use a while loop. I know I need to add another variable somewhere in that loop but I cant figure out the syntax I need to use. Can someone give me a push in the right direction? Thank you very much.

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    London, United Kingdom
    First of all this is C++ not C, so please post under the appropriate forum. Admins will probably move the thread there.

    Secondly, all you need is another variable, let's call it x that you initialize to 0 before the loop. Then, inside the loop you increment and test if it's new value % 6 == 0. That means that the variable value has reached a value which is a multiple of 6 and it's time to print an endline:

    i.e. cout << endl;

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Oh yes, so I have moved it.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. loop needed also how to make input use letters
    By LoRdHSV1991 in forum C Programming
    Replies: 3
    Last Post: 01-13-2006, 05:39 AM
  2. Trying to figure out a simple loop....
    By chadsxe in forum C++ Programming
    Replies: 9
    Last Post: 01-05-2006, 01:31 PM
  3. simple collision detection problems
    By Mr_Jack in forum Game Programming
    Replies: 0
    Last Post: 03-31-2004, 04:59 PM
  4. Request for comments
    By Prelude in forum A Brief History of
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. for loop or while loop
    By slamit93 in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2002, 05:13 AM

Tags for this Thread

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21