Symbol Iterations

Hello, all! I just submitted an assignment for school, and I have a feeling that I took a rather bush-league approach to coding it. After messing with <iomanip> a bit more, I took what may or may not be considered a shortcut on the second for-loop. Here's what I did:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36

#include <iostream>
#include <iomanip>
#include <string>
#include <stdlib.h>
using namespace std;

int main() {

	int n;
	char ch = '@';

	cout << "Choose an odd number between 5 and 21: ";
	cin >> n;
	cout << endl;

	for (int i = n; i < 5 || i > 21 || i % 2 == 0; ) {
		exit(0);
	}

	 for (int i = n; i >= 1 ; i = i - 2) {
		cout << string(i, ch);
		cout << endl;
	}

	 cout << endl;
	 for (int i = n; i >= 1 ; i = i - 1) {
		cout << setw(n) << right << string(i, ch);
		cout << endl;
	 }
	cout << endl;

	system("PAUSE");
	return 0;
}


As you can see, the user will enter a number between 5 and 21 that's odd, and the rest goes from there. I looked around and found the string(n, ch) code as a suggestion for printing symbols like that, but I have a feeling that there might be a more efficient/straightforward way of doing the same thing. I spent quite a while trying to think of a way to do it, but multiplying '@' by "n" in my cout statement caused the program to show the symbol's binary value. I tried assigning "ch" to "i" in the loop, but it obviously didn't work because of the type conflict.

The second for-loop was supposed to replace the given symbol with spaces to get the effect you see here, and the right-justification probably wasn't what was wanted.

The other kicker was that we could only use return once. I originally put return 0; in the first loop, but that would (obviously) have kept me from using it at the end. I doubt that borrowing exit() is really a good idea, but I couldn't get break; to do what I needed.

If you run the program, the results appear exactly as requested, but one of the reasons I want to learn this language is because I want to get ideas of how to do the same thing many different ways. While this program is extremely simple, I would really like to see what ideas you all have regarding the loops. I'd also like your exit-strategies that don't resort to plain C. Any feedback is much appreciated!
Using std::string( i, ch ) is a perfectly valid and straightforward way of doing what you're doing. Efficient? No, but I wouldn't concern myself with efficiency at this point, because it will make things a little more complicated.

But I do have two other comments. Your first for() loop isn't a loop. It's an if statement disguised as a for loop. Not only should you change it to be an if-statement, but if your instructor only wants one return, it means s/he only wants one exit point from the program. Using exit() on line 18 introduces a second exit point, and you will get docked for it.
To remove exit():

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <iomanip>
#include <string>
#include <stdlib.h>
using namespace std;

int main() {

	int n;
	char ch = '@';

	cout << "Choose an odd number between 5 and 21: ";
	cin >> n;
	cout << endl;

	if(n < 5 || n > 21 || n % 2 == 0 ); //not sure why you used a for loop to do an if statements job, if conditions are met, do nothing, else, do everything else
        else{

	 for (int i = n; i >= 1 ; i = i - 2) {
		cout << string(i, ch);
		cout << endl;
	}

	 cout << endl;
	 for (int i = n; i >= 1 ; i = i - 1) {
		cout << setw(n) << right << string(i, ch);
		cout << endl;
	 }
	cout << endl;

	system("PAUSE");
	return 0;
}
}


using string(int, char) is straightforward. If you wanted to do it a different way, you would have to have a nested for loop, and have it cout 1 char at a time.
Thank you both for your answers. For my own sake, I've gone in and adjusted the "for" to make it an "if", which is a total oversight on my part. I guess I just assumed that every if-statement *had* to perform some sort of action after it was done. I'm going to try and work out a nested loop statement to get this thing going in another fashion. Thanks again, guys!
Ok, I've figured out how to make this pattern with a nested for-loop, but I'm now having trouble with the second part of the problem. Here's what I have, so far:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
using namespace std;

int main() {

	int n;
	cout << "Enter an odd number between 5 and 21: ";
	cin >> n;

	if (n < 5 || n > 21 || n % 2 == 0 );

	else {

	for (int i = n; i > 0; i = i - 2) {
		for (int j = 1; j <= i; j++) {
			cout << "@";
		}
		cout << endl;
	}


	}

	

	return 0;

}


I'm now supposed to make a pattern that looks like this (pretend that the dashes are spaces):

@@@@@@@
---@@@@@@(one space)
------@@@@@(two spaces)
---------@@@@(three spaces)
------------@@@(four spaces)
---------------@@(five spaces)
------------------@(six spaces)

Within the same program, the instructions now say to take "n" number of symbols and make each successive line replace the right-most symbol(s) with a space(s). Basically, I have to shift the symbols to the right and lose a right-most symbol, each time. We're not allowed to use setfill. As was the case in the first part, I'm just looking for a push in the right direction.
Last edited on
Topic archived. No new replies allowed.