How Can I Convert a Nested For Loop to a Do-While Loop in C++?

  • Thread starter Thread starter magnifik
  • Start date Start date
  • Tags Tags
    Loops Product
Click For Summary

Discussion Overview

The discussion revolves around converting a nested for loop into a do-while loop in C++. Participants are focused on maintaining the same output structure while addressing issues related to the alignment of output characters.

Discussion Character

  • Technical explanation
  • Homework-related
  • Debate/contested

Main Points Raised

  • One participant shares their initial code using nested for loops and their attempt to replicate it with a do-while loop.
  • Another participant suggests adding an if condition inside the do-while loop to manage the output of the "#" character.
  • A further reply indicates confusion about the implementation of the if condition and notes that it resulted in two side-by-side diagonal lines instead of the desired output.
  • Another participant proposes removing the line that counts the "#" character from the original code to resolve the issue.
  • Participants request outputs from both the original and modified code to better understand the differences in behavior.

Areas of Agreement / Disagreement

Participants are exploring different approaches to achieve the desired output, but there is no consensus on the correct method or solution to the problem.

Contextual Notes

There are unresolved issues regarding the alignment of output characters and the specific implementation of conditions within the do-while loop. Participants have not reached a definitive solution.

Who May Find This Useful

Readers interested in C++ programming, particularly in loop structures and output formatting, may find this discussion relevant.

magnifik
Messages
350
Reaction score
0
I am trying to create a code that does the exact same thing as this does
Code:
#include <iostream>
	using namespace std;

	int main()
	{
	    int len;

	    cout << "Enter a number: ";
	    cin >> len;

	    for (int i = 0; i < len; i++)
	    {
		for (int j = i+1; j < len; j++)
		{
		    cout << ' ';
		}
		cout << "#\n";
	    }
	}

but using a do while loop instead. what i have so far is this

Code:
#include <iostream>
	using namespace std;

	int main()
	{
	    int len;

	    cout << "Enter a number: ";
	    cin >> len;
			
	    for (int i = 0; i < len; i++)
	    {
			int j = i+1;
			do
			{
				cout << ' ';
				j++;
			}
		while (j < len);
		cout << "#\n";
		}
	}

the only problem is on the last line the "#" signs line up instead of being diagonal. any help on how to fix this??
 
Physics news on Phys.org
try put a if condition insdie the do while loop instead just count outside while loop. could use conditions like if j = len -1, then count the #
 
kentigens said:
try put a if condition insdie the do while loop instead just cout outside while loop. could use conditions like if j = len -1, then cout the #

hmm, not exactly sure what you mean. the closest i got was adding

Code:
 if (j == len -1) {
           cout << "#";
           }
into the do part of the do while loop, but this just created two side by side diagonal lines :T to top line had len - 1, but the bottom line had the correct shape and number. any idea on how to get rid of the first one?
 
i mean to take out the line where u count the # from ur original code. and btw, don't have vs here, so can't really run ur code. can u please post up the outputs? both original code and the one u have atm?
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 8 ·
Replies
8
Views
2K
Replies
12
Views
3K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 5 ·
Replies
5
Views
7K