| | |
Creating a triangle in C++
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Oct 2009
Posts: 3
Reputation:
Solved Threads: 0
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
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)
#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; }
0
#2 31 Days Ago
•
•
•
•
can only use nested loops NO arrays or strings
C++ Syntax (Toggle Plain Text)
#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.
Last edited by Clinton Portis; 31 Days Ago at 3:49 pm.
•
•
Join Date: Oct 2009
Posts: 3
Reputation:
Solved Threads: 0
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
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
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.
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.
•
•
Join Date: Oct 2009
Posts: 3
Reputation:
Solved Threads: 0
0
#6 25 Days Ago
i have managed to solve my own problem!!
let me share this
let me share this
C++ Syntax (Toggle Plain Text)
{ 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; }
![]() |
Similar Threads
- Drawing a right triangle (C++)
- Loops and creating a triangle with #'s (C++)
- custom cursor (Java)
Other Threads in the C++ Forum
- Previous Thread: Text - Based RPG V2
- Next Thread: what is a header block of the fuction
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count data database delete deploy developer dll download dynamiccharacterarray email encryption error file forms fstream function functions game getline givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings struct temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





