Thread: Help with simple while program!!!!

  1. #31
    Registered User
    Join Date
    Feb 2009
    Posts
    83
    Well no but it is not doing what I want it to do. I am trying to only show 6 numbers per line and it is not working.

    Quote Originally Posted by vart View Post
    your code is equivalent to

    Code:
    for (counter = 100; counter <= 150; counter = counter + 2)
    {
        cout << counter << endl;
    }
    if (counter % 6 == 0)
    {
       ;
    }
    cout << counter << endl;
    I just added some braces in proper (matched to your code - not your intentions) places. And correct indentation. Do you see any problems now?
    Last edited by walkonwater; 03-28-2009 at 01:06 PM.

  2. #32
    Registered User
    Join Date
    Feb 2009
    Posts
    83
    the if conditional is not working.

  3. #33
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by walkonwater View Post
    the if conditional is not working.
    don't you see that it is outside the loop?

    and last cout statement is outside the if?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #34
    Registered User
    Join Date
    Feb 2009
    Posts
    83
    well I don't seem to know where the braces go?

  5. #35
    Registered User
    Join Date
    Feb 2009
    Posts
    83
    well I put it like this but the braces are still not taking effect:
    Code:
       for (counter = 100; counter <= 148; counter = counter + 2)
    {
        cout << counter << endl;
    }
    
    if (counter % 6 == 0)
    {
    cout << counter << endl;
    }

  6. #36
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Like this:
    Code:
    for (int counter = 100; counter <= 148; counter = counter + 2)
    {
        cout << counter << ' ';
        if (counter % 6 == 0)
        {
            cout << endl;
        }
    }
    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

  7. #37
    Registered User
    Join Date
    Feb 2009
    Posts
    83
    is my if right because it showed up like this:

    100 102
    104 106 108
    110 112 114
    116 118 120
    122 124 126
    128 130 132
    134 136 138
    140 142 144
    146 148

    Quote Originally Posted by laserlight View Post
    Like this:
    Code:
    for (int counter = 100; counter <= 148; counter = counter + 2)
    {
        cout << counter << ' ';
        if (counter % 6 == 0)
        {
            cout << endl;
        }
    }

  8. #38
    Registered User
    Join Date
    Feb 2009
    Posts
    83
    So I changed it to this but it chops off a number at the top:

    Code:
    for (int counter = 100; counter <= 150; counter = counter + 2)
    {
        cout << counter << ' ';
        if (counter % 12 == 0)
        {
            cout << endl;
        }
    }

  9. #39
    Registered User
    Join Date
    Feb 2009
    Posts
    83
    for my above code (#38) it shows. See how it takes away the top number. :
    100 102 104 106 108
    110 112 114 116 118 120
    122 124 126 128 130 132
    134 136 138 140 142 144
    146 148 150

  10. #40
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by walkonwater
    See how it takes away the top number
    It does nothing of the sort. All is does is print a new line.

    Look, there is no problem. The only "problem" is that you are starting from a point where there are 5 numbers in the sequence before you reach a number divisible by 12.
    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

  11. #41
    Registered User
    Join Date
    Feb 2009
    Posts
    83
    Quote Originally Posted by laserlight View Post
    It does nothing of the sort. All is does is print a new line.

    Look, there is no problem. The only "problem" is that you are starting from a point where there are 5 numbers in the sequence before you reach a number divisible by 12.
    So what do I have to do to get it right?

  12. #42
    Registered User
    Join Date
    Feb 2009
    Posts
    83
    This is what my teacher told me but I don't understand how to do it.

    "Perhaps you need a separate counter for the printing as you cannot guarantee that the numbers won’t be divisible by 12 prematurely! 108 is divisible by 12 which is why your first line has a problem. Might have worked if you were starting at 1 but you are starting at 100. You can have 2 counters in a for loop."

  13. #43
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by walkonwater
    This is what my teacher told me but I don't understand how to do it.
    That is why I asked you to start with an example that counts from 1 to 18.
    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

  14. #44
    Registered User
    Join Date
    Feb 2009
    Posts
    83
    Quote Originally Posted by laserlight View Post
    That is why I asked you to start with an example that counts from 1 to 18.
    Well can you just tell me how I would get that extra top number on the top so I then could see my mistake.

  15. #45
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by walkonwater
    do I need to make a second counter. For example counter2.
    Yes
    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. 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, 06:06 PM
  5. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM