#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 !
Eineki
October 7, 2008, October 07, 2008 01:09, permalink
do you mean this way?
#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;
}
#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;
}
goodespeler.myopenid.com
October 7, 2008, October 07, 2008 01:20, permalink
That works. Why won't this way work?
for (char a = 'A', int c = 0; a <= 'Z'; a++, c++) {
cout << a << (c % num == 0 ? "\t\n" : "\t");
}
Eineki
October 7, 2008, October 07, 2008 01:46, permalink
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 (char a = 'A', int c = 0; a <= 'Z'; a++, c++) {
cout << a << (c % num == 0 ? "\t\n" : "\t");
}
{
// 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
}
#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;
}
Adam
October 7, 2008, October 07, 2008 03:15, permalink
#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;
}
clonecd688
May 9, 2011, May 09, 2011 00:30, permalink
Не is not laughed at that laughs at himself first.
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.