English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

Hey. I'm trying to do a program for school, and I'm haveing for loop problems. I'm trying to make a triangle that looks like this...

AC
A C
A C
A C
ABBB
something like that. Anyway, I basically need to add another space between the A and C in each new row. I was thinking if I did something like

(for int j=0, j {
cout << var1 NUMSPACES var3 << endl;
}
Is there a way I can make a variable represent spaces? Like, NUMSPACES = 0, and have the code know that 0 is the number of spaces? so when I change it to NUMSPACE = 2, it knows that I want two spaces?

2006-12-09 02:37:18 · 2 answers · asked by Finn 1 in Computers & Internet Programming & Design

2 answers

Not sure if anybody should be answering this, but I'll give some useful pointers.

First, you could do something like the following to write out one space:
cout << var1;
cout << ' ';
cout << var2;
cout << endl;

Now just find a why to execute the line with the spaces the number of times you need it. (hint: this exercise is meant to teach you about nested loops)

For extra points, 'NUMSPACES' could be a variable, but maybe not of the number of spaces. Rather, think of what other types a variable has.

Oh and in python one could indeed do something like " "*3 and get 3 spaces. But that's just a shorthand.

2006-12-09 03:27:23 · answer #1 · answered by wds 1 · 0 0

Perhaps this is overcomplicating things? Just change the value for baseSize to make it bigger/smaller.

void outLine(char, char, char, int);
int baseSize=4;
int main(int argc, char* argv[]) {
for (int n=0; n outLine('A',' ','B',n);
}
outLine('A','B','B',baseSize);
return 0;
}

void outLine(char bChar, char fChar, char eChar, int baseWide) {
cout << bChar;
for (int j=0; j<=baseWide; j++) {
cout << fChar;
}
cout << eChar << endl;
}

2006-12-09 03:50:53 · answer #2 · answered by BigRez 6 · 0 0

fedest.com, questions and answers