Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,003 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,359 people online right now. Registration is fast and FREE... Join Now!




nested while loop

 
Reply to this topicStart new topic

nested while loop

sumlani
26 Jan, 2008 - 11:09 AM
Post #1

New D.I.C Head
*

Joined: 26 Jan, 2008
Posts: 26



Thanked: 3 times
My Contributions
CODE
#include <iostream>

using namespace std;

int main()
{
    const int SRC_SIZE = 22;
    int step_number = 1,
    x_count = 1;
    while (step_number <= SRC_SIZE)
    {
     ++step_number;
    cout << endl;
    while (x_count <= step_number)
         ++x_count;
         cout << "X";
     }
     cout << endl;
     return 0;
}


I know that the second loop is not working. I'm not understanding the use of EOF.

currently my output looks like this:
X
X
X

It should be:
X
XX
XXX
XXX etc... Help, I just need a better explanation then what my textbook is giving me.
User is offlineProfile CardPM
+Quote Post

GWatt
RE: Nested While Loop
26 Jan, 2008 - 11:31 AM
Post #2

human inside
Group Icon

Joined: 1 Dec, 2005
Posts: 2,176



Thanked: 18 times
Dream Kudos: 450
My Contributions
You're missing braces after your second while loop. It's perfectly legal to not put braces, but only the line immediately following the while loop will be executed.
User is offlineProfile CardPM
+Quote Post

Jingle
RE: Nested While Loop
26 Jan, 2008 - 11:45 AM
Post #3

D.I.C Head
**

Joined: 20 Oct, 2007
Posts: 249


My Contributions
QUOTE(GWatt @ 26 Jan, 2008 - 12:31 PM) *

You're missing braces after your second while loop. It's perfectly legal to not put braces, but only the line immediately following the while loop will be executed.

and
CODE

#include <iostream>

using namespace std;

int main()
{
    const int SRC_SIZE = 22;
    int step_number = 1,
    x_count = 1;
    while (step_number <= SRC_SIZE)
    {
        //++step_number;                     // I moved this
        cout << endl;
            while (x_count <= step_number)
            {
                ++x_count;
                cout << "X";
            }
            x_count = 1;   // you were forgeting to set x_count back to 1 so it was always one les than step_number
                ++step_number;            //to here
    }
    cout << endl;
    return 0;
}



This post has been edited by Jingle: 26 Jan, 2008 - 12:18 PM
User is offlineProfile CardPM
+Quote Post

sumlani
RE: Nested While Loop
26 Jan, 2008 - 02:19 PM
Post #4

New D.I.C Head
*

Joined: 26 Jan, 2008
Posts: 26



Thanked: 3 times
My Contributions
QUOTE(Jingle @ 26 Jan, 2008 - 12:45 PM) *

QUOTE(GWatt @ 26 Jan, 2008 - 12:31 PM) *

You're missing braces after your second while loop. It's perfectly legal to not put braces, but only the line immediately following the while loop will be executed.

and
CODE

#include <iostream>

using namespace std;

int main()
{
    const int SRC_SIZE = 22;
    int step_number = 1,
    x_count = 1;
    while (step_number <= SRC_SIZE)
    {
        //++step_number;                     // I moved this
        cout << endl;
            while (x_count <= step_number)
            {
                ++x_count;
                cout << "X";
            }
            x_count = 1;   // you were forgeting to set x_count back to 1 so it was always one les than step_number
                ++step_number;            //to here
    }
    cout << endl;
    return 0;
}





Thank you so much, I have been trying to get this all morning. I can't believe it was something this simple (I really can :-))! Your the greatest!!
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 12:25PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month