I need to write a program that inputs an integer and a character, and outputs a symetrical triangle composed of the character that has the interger of new lines. I can only use 'while' loops at this stage of the course, and I know it has to be nested 'while' loops. I can not use 'for/next' loops. I have done this before in VB6, so I am somewhat exasperated that I can not even get past firstbase with this same problem in C++. And I like C++, strangely enough. Ive spent close to 20 hours trying to figure it out and just cant ;0

the display needs to look something like(minus the -'s that i had to use here as a placeholder):

-----$
----$$$
---$$$$$
--$$$$$$$
-$$$$$$$$$

<< moderator edit: spaces are preserved using [co[u][/u]de][/co[u][/u]de] tags >>

$
    $$$
   $$$$$
  $$$$$$$
 $$$$$$$$$

I set a variable to the same as the inputed integer and i did
k-- to decrease the space by one every time, but i dont know how i should display more than 1 character per line..
---------------------------------------------------------
Any suggestions would be greatly appreciated.

Dani AI

Generated

Following 's question and 's for-loop example, here is a compact while-only approach that prints a centered triangle of an input character for N lines. The inner while is what repeats the character on each line (that was the missing piece mentioned in the original post).

#include <iostream>

int main() {
    int n;
    char ch;
    std::cout << "Lines: ";
    if (!(std::cin >> n)) return 0;
    std::cout << "Character: ";
    std::cin >> ch;

    int line = 0;
    while (line < n) {
        int spaces = n - line - 1;
        while (spaces > 0) {
            std::cout << ' ';
            --spaces;
        }
        int count = 2 * line + 1;
        while (count > 0) {
            std::cout << ch;
            --count;
        }
        std::cout << '\n';
        ++line;
    }
    return 0;
}

Notes and common pitfalls:

  • The outer loop walks lines (0..n-1). Leading spaces are n - line - 1. Character count is 2*line + 1. Those formulas avoid off-by-one mistakes.
  • To print more than one character per line, use a second (inner) loop that writes the character repeatedly (see count loop).
  • std::cin >> ch skips whitespace; to allow a space as the chosen character use std::cin.get() after consuming the leftover newline.
  • Validate n (non‑positive values produce no output) and prefer simple, well-named counters (line, spaces, count) to keep nested whiles readable.

As pointed out, an alternative is to use <iomanip> (std::setw / std::setfill) to handle spacing/filling, which can make the code shorter but learning the nested-while structure is exactly what the course exercise wants.

Recommended Answers

All 3 Replies

Member Avatar for Member #46692

U know how 2 convert a for loop into a while loop now don't u?

#include <iostream.h>
#include <string.h>
using namespace std;


int main(void)
{
    cout<<"enter a integer";
    int b;
    cin>>b;

    cout<<"enter a symbal";
    char s;
    cin>>s;

    int space=b;

    int k=1;
    for(int i=0; i<b; i++)
    {

        for(int j=0; j<space; j++)
        {
            cout<<" ";

        }
        space--;


        for(int m=0; m<k; m++)
        {

        cout<<s;
       }  
       k=k+2;   
        cout<<"\n";
    }

   system("pause");             
}

Don't worry yourself about creating the spaces and aligning into a triangle.... Thanks to C++ you can set the width and fill character of the output ;)

Thanks guys for the help. while loops can get confuseing as hell with all the brackets and everything.

Thanks again guys :)
Kisvirag

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.