B04f7f475867f6b47a59b49dfabc0daf

Can someone check my for loop? There should be a way to include my counter (c) to be included and itterated in the loop, but I kept running into issues with having a char and an int in my initialization section.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream.h>

int main() {
	
        int num;
	cout << "\n\nHow many characters do you want me to print per line: ";
	cin >> num;

	int c = 0;
        char a;
	for (a = 'A'; a <= 'Z'; a++) {	
		c++;
		cout << a << '\t' << (c % num == 0 ? "\n" : "");
	}

	cout << "\n\nHit enter to terminate: ";
	cin.ignore(); cin.ignore();

	return 0;
}

Refactorings

No refactoring yet !

5a00a3a98dcf6f9cd717440fd2b606e5

Eineki

October 7, 2008, October 07, 2008 01:09, permalink

No rating. Login to rate!

do you mean this way?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream.h>

int main() {
	int num;
  	cout << "\n\nHow many characters do you want me to print per line: ";
	cin >> num;

        { // use of a nameless block to hide a and c
          char a;
          int c;
  	  for (c=1, a='A'; a<='Z'; a++, c++) {	
		cout << a << ((int)c % num == 0 ? "\n" : "\t");
	  }
        }

	cout << "\n\nHit enter to terminate: ";
	cin.ignore(); cin.ignore();

	return 0;
}

a dirtier solution is

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream.h>

int main() {
	int num;
  	cout << "\n\nHow many characters do you want me to print per line: ";
	cin >> num;

        for (char c=1, a='A'; a<='Z'; a++, c++) {	
		cout << a << ((int)c % num == 0 ? "\n" : "\t");
        }

	cout << "\n\nHit enter to terminate: ";
	cin.ignore(); cin.ignore();

	return 0;
}
B04f7f475867f6b47a59b49dfabc0daf

goodespeler.myopenid.com

October 7, 2008, October 07, 2008 01:20, permalink

No rating. Login to rate!

That works. Why won't this way work?

1
2
3
for (char a = 'A', int c = 0; a <= 'Z'; a++, c++) {	
    cout << a  << (c % num == 0 ? "\t\n" : "\t");
}
5a00a3a98dcf6f9cd717440fd2b606e5

Eineki

October 7, 2008, October 07, 2008 01:46, permalink

No rating. Login to rate!

Think at the for loop into the expanded form (it is syntax sugar)

In this way the problem is clear:
the declaration
char a, int c;
is wrong, it should be
char a; int c;
but ; is the for clause separator and cause you the troubles you encountered.

for in shorter form

1
2
3
4
for (char a = 'A', int c = 0; a <= 'Z'; a++, c++) {	
    cout << a  << (c % num == 0 ? "\t\n" : "\t");
}

for expanded in a while loop

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
{
// for initialization clause
  char a='A', // this comma should be a ; */
  int c=0;
// eo for initialization clause

  while (a<='Z') {
    cout << a  << (c % num == 0 ? "\t\n" : "\t");  // for loop

// for increment clause
    a++;
    c++;
// eo for increment clause
  }


dirty solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream.h>

int main() {
	int num;
  	cout << "\n\nHow many characters do you want me to print per line: ";
	cin >> num;

       for (char c=1, a='A'; a<='Z'; a++, c++) {	
		cout << a << ((int)c % num == 0 ? "\n" : "\t");
	  }
        }

	cout << "\n\nHit enter to terminate: ";
	cin.ignore(); cin.ignore();

	return 0;
}
A8d3f35baafdaea851914b17dae9e1fc

Adam

October 7, 2008, October 07, 2008 03:15, permalink

1 rating. Login to rate!
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
#include <iostream>

int main(int argc, char **argv)
{
    int characters_per_line;
    char *characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    
    std::cout << "How many characters do you want me to print per line? ";
    std::cin  >> characters_per_line;
    
    while (*characters) {
        for (int i = 0; i < characters_per_line && *characters; i++) {
            std::cout << *characters++;
            std::cout << "\t";
        }
        
        std::cout << std::endl;
    }
    
    std::cout << "Hit enter to terminate.";
    std::cin.ignore();
    std::cin.ignore();
    
    return 0;
}

Your refactoring





Format Copy from initial code

or Cancel