Jump to content
  • Advertisement
Sign in to follow this  
phil67rpg

while loops

This topic is 2561 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am working on a doubly nested while loops.I am trying to print out a grid of characters such as.
xxx
xxx
xxx

here is my code so far.

#include <iostream>

using namespace std;

int test=0;

int main()
{
int rows=0;
int columns=0;
char theChar;
int i=0;
int j=0;

cout << "How many rows? ";
cin >> rows;
cout << "How many columns? ";
cin >> columns;
cout << "What character? ";
cin >> theChar;

while (i<rows)
{
i++;
cout << theChar;
while(j<columns-1)
{
j++;
cout << theChar;
}
cout << endl;
}

cin >> test;
return 0;
}

I know it is an easy fix I just need a second opinion.I will continue to work on it.

Share this post


Link to post
Share on other sites
Advertisement

I know it is an easy fix I just need a second opinion.I will continue to work on it.

What is the actual problem? One issue I see is that on the second run through the "i" loop, the "j" variable isn't reset to 0 so will start out at the max value.

Also, is there a reason you're not using a for loop? This is exactly what for loops are designed for.

Share this post


Link to post
Share on other sites
well simply the question asks to take a doubly nested for loop which prints out a grid of characters and convert it to a doubly nested while loop.

Share this post


Link to post
Share on other sites
Without seeing the original for loops and just rearranging your code, you would need something like this:

while(i < rows)
{
cout << theChar;

while(j < columns)
{
cout << theChar;
++j;
}
j = 0;
cout << endl;
++i;
}


Like stated previously, j needs to be reset after the inner loops finishes its rounds.
Secondly, you need to increment your counters at the end of a loop iteration, not the beginning. The way a for loop works is like this: Start the counter at a certain number, while a condition is true, do the work. At the end of the work, manipulate the counter.
Lastly, if you are basing your iteration at 0 and want to do the work for each column, you need to change your inner condition to j < columns. The way you have it now will skip the last column.

[color="#1C2837"]



[color="#1C2837"]


Share this post


Link to post
Share on other sites
You need to reset the "j" before the nested while, and put the incrementation at the end of the loop block if you want it to work like a for loop does.

Share this post


Link to post
Share on other sites
jonbonazza's answer doesn't load properly, so here's what your program should look like:
[color="#1C2837"]#include <iostream>[color="#1C2837"]

using namespace std;

int test=0;

int main()

[color="#1C2837"]

{

int rows=0;

[color="#1C2837"]

int columns=0;

[color="#1C2837"]

char theChar;

[color="#1C2837"]

int i=0;

[color="#1C2837"]

int j=0;

[color="#1C2837"]

cout << "How many rows? ";

[color="#1C2837"]

cin >> rows;

[color="#1C2837"]

cout << "How many columns? ";

[color="#1C2837"]

cin >> columns;

[color="#1C2837"]

cout << "What character? ";

[color="#1C2837"]

cin >> theChar;

[color="#1C2837"]

while (i<rows)

[color="#1C2837"]

{

[color="#1C2837"]

i++; //get rid of the cout here, redo the while() expression. makes it simpler.

[color="#1C2837"]

while(j<columns)

[color="#1C2837"]

{

[color="#1C2837"]

j++;

[color="#1C2837"]

cout << theChar;

[color="#1C2837"]

}

[color="#1C2837"]

cout << endl;

j=0; //this is the new line that fixes your problem<br style="color: rgb(28, 40, 55); font-size: 13px; line-height: 16px; text-align: left; background-color: rgb(250, 251, 252); ">[color="#1C2837"]

}

[color="#1C2837"]

cin >> test;

[color="#1C2837"]

return 0;

[color="#1C2837"]

}

Share this post


Link to post
Share on other sites
If you move the declaration of j inside the first loop, then you don't have to "reset" it between iterations. In general, you should declare your variables in the tightest possible scope. Re-using variables like this often leads to bugs because the variable may not have a consistent initial state.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!