944,193 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 680
  • C++ RSS
Oct 28th, 2009
0

Creating a triangle in C++

Expand Post »
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

C++ Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
isabellegagnier is offline Offline
3 posts
since Oct 2009
Oct 28th, 2009
0
Re: Creating a triangle in C++
Quote ...
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:
C++ Syntax (Toggle Plain Text)
  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; Oct 28th, 2009 at 3:49 pm.
Reputation Points: 237
Solved Threads: 117
Practically a Posting Shark
Clinton Portis is offline Offline
822 posts
since Oct 2005
Oct 28th, 2009
0
Re: Creating a triangle in C++
line #24 should be: list->next = new node;
Reputation Points: 237
Solved Threads: 117
Practically a Posting Shark
Clinton Portis is offline Offline
822 posts
since Oct 2005
Oct 28th, 2009
0
Re: Creating a triangle in C++
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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
isabellegagnier is offline Offline
3 posts
since Oct 2009
Oct 28th, 2009
0
Re: Creating a triangle in C++
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.
Reputation Points: 237
Solved Threads: 117
Practically a Posting Shark
Clinton Portis is offline Offline
822 posts
since Oct 2005
Nov 3rd, 2009
0
Re: Creating a triangle in C++
i have managed to solve my own problem!!
let me share this
C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
isabellegagnier is offline Offline
3 posts
since Oct 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Need help with FOR repitition with Switch
Next Thread in C++ Forum Timeline: what is a header block of the fuction





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC