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
    Posts
    1

    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:


    Code:
    #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
    Location
    London, United Kingdom
    Posts
    2,094
    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
    Location
    Singapore
    Posts
    21,401
    Oh yes, so I have moved it.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 04:39 AM
  2. Trying to figure out a simple loop....
    By chadsxe in forum C++ Programming
    Replies: 9
    Last Post: 01-05-2006, 12:31 PM
  3. simple collision detection problems
    By Mr_Jack in forum Game Programming
    Replies: 0
    Last Post: 03-31-2004, 03:59 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  5. for loop or while loop
    By slamit93 in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2002, 04: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