Help with simple while program!!!!

This is a discussion on Help with simple while program!!!! within the C++ Programming forums, part of the General Programming Boards category; The first part is I am trying to make this program look like this: 10 12 14 16 18 20 ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    83

    Help with simple while program!!!!

    The first part is I am trying to make this program look like this:

    10 12 14 16 18 20
    22 24 26 28 30 32
    34 36 38 40

    I am trying to do this with a while loop. I have to make each line only show 6 number per line. THe only thing I can't get it make the my interger, counter, add 2 to it each time and make 6 only on each line.

  2. #2
    Registered User
    Join Date
    Feb 2009
    Posts
    83
    I take that back. I can get it to count by 2 but don't know how to make only 6 numbers show up per line.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,936
    Well, at least you have something. Show us what you have so far so we can suggest how to make it do what you want it to do.
    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

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    83
    Code:
    counter = 100;
      do {
        counter = counter + 2;
        cout << " " << counter;
      } while (counter <= 148);
      
        return 0;
        }

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    83
    This is the hint out teacher gave us But I don't understand how to: To help with printing only six numbers per line, you can setup a separate variable that gets incremented inside the loop once for each time through the loop. This second counter can keep track of the number of values displayed on the line. When it reaches 6, you can output a newline and set that variable back to 0.

  6. #6
    Registered User
    Join Date
    Feb 2009
    Posts
    83
    please someone help???

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,936
    Well, your teacher's hint is what I would have suggested.

    You can test it out this way: write a program to print:
    Code:
    1 2 3 4 5 6
    7 8 9 10 11 12
    13 14 15 16 17 18
    by using a loop to loop over the natural numbers (instead of hard coding the output).
    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

  8. #8
    Registered User
    Join Date
    Feb 2009
    Posts
    83
    well the problem is is that I don't know how to make only certain amout of numbers go one row.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,936
    Quote Originally Posted by walkonwater
    well the problem is is that I don't know how to make only certain amout of numbers go one row.
    Notice that 6 is perfectly divisible by 6, as is 12 and 18. Use that to your advantage with operator % by printing a new line appropriately.
    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

  10. #10
    Registered User
    Join Date
    Feb 2009
    Posts
    83
    That is funny. I emailed my teacher and he told me to use the operator as well but I don't get how to use it. I knew 6 would pay a factor of course but I didn't undertand how to make the cout part of this to perform the 6-per-line.

  11. #11
    Registered User
    Join Date
    Feb 2009
    Posts
    83
    But with the operator, I do not understand how to write that to make it only produce 6 per line.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,936
    Well, I do not want to give the whole game away, so here is the pseudocode for the algorithm:
    Code:
    for i = 1 to i = 18
        print i
        if i is perfectly divisible by 6
            print new line
    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

  13. #13
    Registered User
    Join Date
    Feb 2009
    Posts
    83
    the integer "i" would be my counter right?

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,936
    Yes, but of course you have to adapt this to your actual problem.
    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

  15. #15
    Registered User
    Join Date
    Feb 2009
    Posts
    83
    so would it look like this:

    Code:
    for i = 1 to i = 18
        cout << counter;
        if counter % 6;
        cout << endl;
    but I don't understand how to do the first for statement if the rest is correct.

Page 1 of 4 1234 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with a very simple program
    By htdefiant in forum C++ Programming
    Replies: 13
    Last Post: 08-14-2007, 01:27 PM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  3. [Help] Simple Array/Pointer Program
    By sandwater in forum C Programming
    Replies: 3
    Last Post: 03-30-2007, 02:42 PM
  4. simple silly program
    By verbity in forum C Programming
    Replies: 5
    Last Post: 12-19-2006, 05:06 PM
  5. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 11:39 AM

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