I need to create a triangle in C++ that will take the user inputted letter (capital) and then create a triangle that will look like. I can only use nested loops NO arrays or strings

Enter a letter: F
F
FE
FED
FEDC
FEDCB
FEDCBA


here is the code i have so far i am creating a triangle but not exactly the one i require

#include <iostream>
using namespace std;
int main () 
{
	char letter, last;
	char row, col;
	
	cout << "Entre a letter: ";
	cin >> letter;
	letter = toupper(letter);
		
	for (row = 0; row < letter; row++)
		{
			for (col = 0; col <= row; col++)
				cout << letter ;
				last = letter;
				 cout << last << letter<< letter--;
				 cout << endl;
		}
    return 0;
}

Recommended Answers

All 5 Replies

can only use nested loops NO arrays or strings

I was kinda stumped at first until I came up with a linked-list of chars:

#include<iostream>
#include<cctype>
using namespace std;

struct node
{
     char letter;
     node *next;
};

int main()
{
     node *head_ptr = new node;
     node *list = head_ptr;
     int counter = 0;
     char input, ans;

     do{

               cout << "\nEnter a letter:  ";
               cin >> input;

               list->letter = toupper(input);      
               list->next = new list;
               list = list->next;

               counter++;

               cout << "\nWould ye' like to enter another letter? (Y/N) ";
               cin >> ans;                     

          }while(toupper(ans) == 'Y');

          list->next = NULL;
          list = head_ptr;

          int j=1;
          while(list)
          { 
               for(int i=0;  i<j && list; i++)
               {
                    cout << list->letter; 
                    list = list->next;
               }

               cout << endl;
               j++;
          }

         list = head_ptr;
         node temp;

          while(list)
          {
               temp = list;
               list = list->next;
               delete temp;               
          }         

return 0;
}

This code creates a 'linked-list' of nodes that contain space for 1 char per node. After the user is prompted to enter several letters, the list is traversed (starting from the head pointer) and displayed to the screen in a tree type manner.

This is untested code, so it might contain simple/easy to fix errors.

line #24 should be: list->next = new node;

Thanks fro your help
my only problem is that i am so new at this and have yet to learn about linked lists of char. there as to be a stupid way for newbies like me, i do have a class tomorrow and can ask the teacher, and i will keep on trying. if you try my code it also does not stop at letter it keeps going on through the ASCII chart

Again thanks

The requirements for this assignement approach an intermediate level of programming since we cannot use arrays or <string> class objects. Being that we are unable to use arrays, we cannot use 'c-strings' (char arrays). So what's left?

Our options include: STL containers and linked lists or just create a ton of singular char variables and hope we have enough for user input. Another option would be to write all the char variables to a file.

I'm open to suggestions, I think the method I used was probably the most primitive of options that I could think of.

i have managed to solve my own problem!!
let me share this

{
	char letter, maxrow;
	char row, col;
	
	cout << "Enter a letter: ";
	cin >> letter;
	letter = toupper(letter);
	maxrow = letter;
	
	for (row = 'A'; row <= maxrow; row++)
		
	{
		for (col = 'A'; col <= row; col++, letter--)
			cout << letter;
		cout<< endl;
		letter=maxrow;	
		
	}
    return 0;
}
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.