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.
while loops
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.
What question; your post doesn't have a question in it
well I got this question out of a book.
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.
Without seeing the original for loops and just rearranging your code, you would need something like this:
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"]
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"]
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.
jonbonazza's answer doesn't load properly, so here's what your program should look like:
[color="#1C2837"]#include <iostream>[color="#1C2837"]
[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"]}
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement