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 ...

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.