I am currently going through a c++ book trying to do the problems at the end of the chapters. They dont provide solutions so im kind of stuck. My problem is:

Write a program that uses a loop to display the characters for each ASCII code 32 through 127. Display 16 characters on each line with one space between characters.

Currently it prints out the ASCII characters fine but it looks like a mess. i dont know how to get it to do 16 characters on each line.


Here is The code i have with Sample output.

#include <iostream>

using namespace std;

int main()
{




    for (int i = 0; i <= 127; ++i)
    {
        cout << char(i) << " ";

        for (int j = 1; j <= 16; j++)
        {
         //   cout << endl;
        }



    }

    //cout << endl;

    return 0;
}

sample output: (the white space is included in the output. i did a straight copy and paste.


1 2 3 4 5 6 7 8 9 : ; < = > ? @ A B C D E F G H I J K L M N O P Q R S T U V W X Y Z [ \ ] ^ _ ` a b c d e f g h i j k l m n o p q r s t u v w x y z { | } ~ Press [Enter] to close the terminal ...

Dani AI

Generated

Your output looks "messy" because you were printing control codes (below 32) and not controlling when to break the line. A nested loop like suggested will simply repeat each character many times; avoid it. is right that a modulus-based break is a good idea, but if you start at 32 you must offset the test (or just use a simple counter).

Here is a compact, robust pattern that prints codes 32..127 with 16 characters per line, shows a placeholder for non-printable codes, and avoids trailing spaces:

#include <iostream>
#include <cctype>

int main() {
    int printed = 0;
    for (int code = 32; code <= 127; ++code) {
        unsigned char uc = static_cast<unsigned char>(code);
        std::cout << (std::isprint(uc) ? static_cast<char>(uc) : '?');

        ++printed;
        if (printed % 16 == 0)
            std::cout << '\n';
        else
            std::cout << ' ';
    }
    if (printed % 16 != 0) std::cout << '\n';
    return 0;
}

Notes: use static_cast<unsigned char> when calling <cctype> functions to avoid UB on some platforms. If you only want visible characters skip 127 and stop at 126 (127 is DEL and normally not printable). If you prefer the modulo trick instead of a counter, test ((code - 32) % 16) == 15 (or similar) so the offset is handled correctly.

In short: start at the correct ASCII value (32), don’t nest loops for this task, and either keep a simple counter or adjust your modulo by the starting offset. This will give neat rows of 16 characters each.

Recommended Answers

All 2 Replies

Try

for (int i = 0; i <= 127; ++i)
 {
    for (int j = 0; j <= 16; j++)
    {
        cout << char(i) << " ";
        if (j == 16) 
        {
           cout << "\n";
           j = 0;
        }
    }
 }
commented: Solving the problem with no explanation is bad form. Giving explanation without code is better. -4

I am currently going through a c++ book trying to do the problems at the end of the chapters. They dont provide solutions so im kind of stuck. My problem is:

Write a program that uses a loop to display the characters for each ASCII code 32 through 127. Display 16 characters on each line with one space between characters.

Currently it prints out the ASCII characters fine but it looks like a mess. i dont know how to get it to do 16 characters on each line.


Here is The code i have with Sample output.

#include <iostream>

using namespace std;

int main()
{




    for (int i = 0; i <= 127; ++i)
    {
        cout << char(i) << " ";

        for (int j = 1; j <= 16; j++)
        {
         //   cout << endl;
        }



    }

    //cout << endl;

    return 0;
}

sample output: (the white space is included in the output. i did a straight copy and paste.


1 2 3 4 5 6 7 8 9 : ; < = > ? @ A B C D E F G H I J K L M N O P Q R S T U V W X Y Z [ \ ] ^ _ ` a b c d e f g h i j k l m n o p q r s t u v w x y z { | } ~ Press [Enter] to close the terminal ...

Not sure about Ichigo's solution. It looks like that would output each character 16 times. I don't think a nested loop should be used here. Instead use the modulo operator.

for (int i = 0; i <= 127; ++i){
  if (i % 16 == 0) cout << '\n'; //if i is divisible by 16, begin new line.  
  cout << char(i) << " ";
  }
commented: Solving the problem with no explanation is bad form. Giving explanation without code is better. -4
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.