Creating a triangle in C++

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Oct 2009
Posts: 3
Reputation: isabellegagnier is an unknown quantity at this point 
Solved Threads: 0
isabellegagnier isabellegagnier is offline Offline
Newbie Poster

Creating a triangle in C++

 
0
  #1
31 Days Ago
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

  1. #include <iostream>
  2. using namespace std;
  3. int main ()
  4. {
  5. char letter, last;
  6. char row, col;
  7.  
  8. cout << "Entre a letter: ";
  9. cin >> letter;
  10. letter = toupper(letter);
  11.  
  12. for (row = 0; row < letter; row++)
  13. {
  14. for (col = 0; col <= row; col++)
  15. cout << letter ;
  16. last = letter;
  17. cout << last << letter<< letter--;
  18. cout << endl;
  19. }
  20. return 0;
  21. }
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 318
Reputation: Clinton Portis is an unknown quantity at this point 
Solved Threads: 32
Clinton Portis's Avatar
Clinton Portis Clinton Portis is offline Offline
Posting Whiz
 
0
  #2
31 Days Ago
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:
  1. #include<iostream>
  2. #include<cctype>
  3. using namespace std;
  4.  
  5. struct node
  6. {
  7. char letter;
  8. node *next;
  9. };
  10.  
  11. int main()
  12. {
  13. node *head_ptr = new node;
  14. node *list = head_ptr;
  15. int counter = 0;
  16. char input, ans;
  17.  
  18. do{
  19.  
  20. cout << "\nEnter a letter: ";
  21. cin >> input;
  22.  
  23. list->letter = toupper(input);
  24. list->next = new list;
  25. list = list->next;
  26.  
  27. counter++;
  28.  
  29. cout << "\nWould ye' like to enter another letter? (Y/N) ";
  30. cin >> ans;
  31.  
  32. }while(toupper(ans) == 'Y');
  33.  
  34. list->next = NULL;
  35. list = head_ptr;
  36.  
  37. int j=1;
  38. while(list)
  39. {
  40. for(int i=0; i<j && list; i++)
  41. {
  42. cout << list->letter;
  43. list = list->next;
  44. }
  45.  
  46. cout << endl;
  47. j++;
  48. }
  49.  
  50. list = head_ptr;
  51. node temp;
  52.  
  53. while(list)
  54. {
  55. temp = list;
  56. list = list->next;
  57. delete temp;
  58. }
  59.  
  60. return 0;
  61. }

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.
Last edited by Clinton Portis; 31 Days Ago at 3:49 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 318
Reputation: Clinton Portis is an unknown quantity at this point 
Solved Threads: 32
Clinton Portis's Avatar
Clinton Portis Clinton Portis is offline Offline
Posting Whiz
 
0
  #3
31 Days Ago
line #24 should be: list->next = new node;
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 3
Reputation: isabellegagnier is an unknown quantity at this point 
Solved Threads: 0
isabellegagnier isabellegagnier is offline Offline
Newbie Poster
 
0
  #4
31 Days Ago
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
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 318
Reputation: Clinton Portis is an unknown quantity at this point 
Solved Threads: 32
Clinton Portis's Avatar
Clinton Portis Clinton Portis is offline Offline
Posting Whiz
 
0
  #5
31 Days Ago
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 3
Reputation: isabellegagnier is an unknown quantity at this point 
Solved Threads: 0
isabellegagnier isabellegagnier is offline Offline
Newbie Poster
 
0
  #6
25 Days Ago
i have managed to solve my own problem!!
let me share this
  1. {
  2. char letter, maxrow;
  3. char row, col;
  4.  
  5. cout << "Enter a letter: ";
  6. cin >> letter;
  7. letter = toupper(letter);
  8. maxrow = letter;
  9.  
  10. for (row = 'A'; row <= maxrow; row++)
  11.  
  12. {
  13. for (col = 'A'; col <= row; col++, letter--)
  14. cout << letter;
  15. cout<< endl;
  16. letter=maxrow;
  17.  
  18. }
  19. return 0;
  20. }
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC