Click to See Complete Forum and Search --> : C++ cout stuff


FooL
April 28th, 2001, 12:46 PM
I have this assignment for intro to prog and my instructor is *famous* for giving us assignments using things that we'd have no way of knowing about. Here's the question. (Remember, I'm not asking for the whole solution, just a "key command" or something to get me pointed in right direction.

Here's the Q: What kind of cout << do I use to get this to happen?

I need to make a shape based on user input. i.e. if the user typed in a x and a 5 the output would be like this:
x
xxx
xxxxx
xxx
x

I understand the counters, loops, if, and while statements I'll be using. Here's the problem. How do I get the prog to output varying instances of the variable "Letter". in other words, how do I get it to do 1 x then 3 x's and then 5 x's everytime I enter a new loop?

I've tried working a counter that adds 2 to itself every loop and then trying something like:
cout << Letter * counter << endl;

But I end up getting the ANSCI equivalent of the Letter times the value of counter. (So it outputs like 282 instead of XXXXX or something....)

Any help is very much appreciated!

LagMonster
April 28th, 2001, 09:42 PM
Divide the Amount for iterations in half...Loop up to that. Then Loop back down from it.

y=iterations/2

for(x,1,y){
x++
for(dis,1,y){
dis++;
cout << string;}
cout << endl;
}
y--
for(x,y,1){
x--
for(dis,1,y){
dis++;
cout << string;}
cout << endl;
}


Try to make sense of that...

FooL
April 28th, 2001, 10:15 PM
Thanks. I'm still new, so I had to read it like 20 times and think about it VERY slowly. But, I'm sure I know what you're getting at. Thanks. That's a lot more complicated than the route I was going. But then again, my way wasn't even getting close to working. While I'm at it, response is always slow here. Do you know of a site that is dedicated to C++ like Windrivers is to Windows?

Maybe I should post that on a new thread...?

mattjamison
April 29th, 2001, 01:46 AM
void main(){

int counter=0;
char string[10]="x";

cout << "enter counter";
cin >> counter;

cout << "\n\nenter character";
cin >> string;

if(counter%2 == 0)
counter--;

for(int i=0; i<counter; i++){
for(int j=0; j<=i; j++){
cout << string;
}
cout << endl;
i++;
}

for(i=counter-1; i>0; i--){
for(int j=i-1; j>0; j--){
cout << string;
}
cout << endl;
i--;
}
}