Hi there, im trying to write some code to produce
A
BB
CCC
DDDD
EEEEE
FFFFFFF
exactly like the above, however im totally stuck, i dont know how to go about this, would i need to somehow use a for loop? or an array? pointers much appreciated!
Hi there, im trying to write some code to produce
A
BB
CCC
DDDD
EEEEE
FFFFFFF
exactly like the above, however im totally stuck, i dont know how to go about this, would i need to somehow use a for loop? or an array? pointers much appreciated!
A loop and an array (or null terminated C-style string) would work, though you probably would be able to rely on ASCII values to eliminate the array.would i need to somehow use a for loop? or an array?
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
No need to use an array. A loop (or two) should be fine.
First write code to write out this (one 'x' at a time):
xxxxxx
Then add code to that to write out this:
xxxxxx
xxxxxx
xxxxxx
xxxxxx
xxxxxx
xxxxxx
Then make a small change so that it writes out this:
x
xx
xxx
xxxx
xxxxx
xxxxxx
And finally change the code one last time to write out like you're suppose to:
A
BB
CCC
DDDD
EEEEE
FFFFFFF
At each step, compile and test and get it working before moving on.
ive tried this to start with:
but the problem is its not outputting the 'x' on the same line:Code:#include<iostream> #include<string> using namespace std; int main() { char cLetter = 'x'; int x; for(x = 0; x <= 5; x++) { cout << cLetter; cin.get(); } return 0; }
all the x are on seperate lines, how do i keep them on the same line?Code:x x x x x x
Last edited by dynamethod; 11-08-2007 at 12:00 PM.
char* is a pointer to an address where a character or string of characters are located. That's the only way you can declare a string of chars in c-style.
Remove the cin.get();all the x are on seperate lines, how do i keep them on the same line?
I might be wrong.
Quoted more than 1000 times (I hope).Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
You can put the cin.get() outside the loop just above the return 0; if you want to use it to keep the console window open so you can see your results.
Otherwise, that looks like a good start.
Ok, ive got it sorted a bit better now i think
however i think im going the wrong way about this because Daved said that a loop or two should do the trick but the way im going i will need a few 'for loops' to accomplish this :S
This outputs:Code:#include<iostream> #include<string> using namespace std; int main() { char cLetter = 'x'; int x; for(x = 0; x <= 5; x++) { cout << cLetter; } cout << "\n"; for(x = 0; x <= 5; x++) { cout << cLetter; } cin.get(); return 0; }
but thats just for two lines and ive already used two loops :SCode:xxxxxx xxxxxx
how do i go about using the loop so that i dont have keep using it over and over?
or am i on the right track here?
Use nested loops:
Now just figure out the ... parts. And you can also do all of this with a single loop.Code:for(...) { for(...) { ... } }
"Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
-Christopher Hitchens
You need two nested loops. Something like:
Try this out and let me knowCode:char Letter; int i,j; for(i=0; i<26; i++) { for(j=0; j<=i; j++) { Letter = 65+i; //Here convert to char the ASCII decimal code 65+i, which stands for 'A' when i=0, 'B' when i=1, etc. ;) cout << Letter; } cout << "\n"; }
Regards,
Tesctassa
Awesome~!
ok getting somewhere, heres my code now:
I think im on the right track now, thanks for everyones input! you guys are VERY helpful :-)Code:#include<iostream> #include<string> using namespace std; int main() { char cLetter = 'x'; int x; int y; for(y = 0; y <= 4; y++) { for(x = 0; x <= 4; x++) { cout << cLetter; } cout << "\n"; } cin.get(); return 0; }
if this doesnt look right please say so though lol, thanks again!
Awwwww!!! Tesctassa!!!! you just gave me the answer!! your code works perfectly, now i cant try and solve it :S
Thats very interesting about the ASCII, it will come in handy! but that was tooo much info :S
Wow, that's the first time I see a questioner complain about a violation of the "do not give full code" rule.
Tesctassa, in this forum we don't want people to do this. It is generally an impediment to learning, since the posters no longer need to solve their own problems. As you can see, some smart ones even realize that themselves.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
Uuuuuups! ^______^'
As you may see from my registration date I'm quite new on this forum, and I admit I didn't read the Forum Rules.... I beg pardon!!!
dynamethod pleeeeeeease excuse me!!! Anyway, now try printing on screen this:
:PCode:A BB CCC DDDD ...........
Regards,
Tesctassa
ps: again, please excuse me!
dynamethod pleeeeeeease excuse me!!! Anyway, now try printing on screen this:
:PCode:A BB CCC DDDD ...........
Regards,
Tesctassa
ps: again, please excuse me!
wow, this is quite tricky :S
im not sure how to go about this tbh, hints much appreciated!