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

I have tried solving this problem several ways. I either get blank screen or numbers 1-16. Can anyone lend a hand please?

This is what I got so far

#include <iostream>
using namespace std;

int main()
{
    int num = 0;
    char letter;

    while (num <= 16)
    {
        letter = 32;
        cout << letter << " " << endl;
        num++;
    }
    return 0;
}

tried:

#include <iostream>
using namespace std;

int main()
{
    int num = 0;
    char letter;

    while (num <= 16)
    {
        letter = 32;
        cout << num << " " << endl;
        num++;
    }
    return 0;
}

Try this:

char letter = 32;
int counter = 0;

while (letter < 128)
{    
     cout << letter << ' ';

     counter++;

     if(counter == 16)
     {
         cout << endl;
         counter = 0;
     }

     letter++;     
}

I tried that and my printout is blank

{
    char letter = 32;
    int counter = 0;

    while (letter < 128)
    {
        cout << letter << " ";
        counter++;
    }
    if (counter == 16)
    {
        cout << endl;
        counter = 0;
    }
    letter++;
    return 0;
}

I tried your advice but I am getting a blank output screen. this is what I put in, possible that I am missing something? Please not that I am a beginner and just started to learn this in August of 09. I may not understand what you post so if you could be kind and give an explanation on your code I would greatly appreciate it so I can better understand why it's done like this or that. I would learn more out of it and not just get an answer to my issue.

Notice the different placements of the curly brackets betweenn the code written by Clinton Portis and your code. He checks the value of counter within the while loop to start a new line if needed, you check it outside the while loop.

Try this:

#include iostream
using namespace std;
int main()
{
   char letter = 32;
   cout << letter;
   cin.get();
   return 0;
}

If that prints a char to the screen, then the code posted above this post should work ok, too. If not you could try an explicit cast from int to char like this:

#include iostream
using namespace std;
int main()
{
   char letter = (char)32;
   cout << letter;
   cin.get();
   return 0;
}

though you may want to use the up to date C++ version of casting rather than the older C version I demonstrated above.

Ok I see what I was doing wrong. I followed Portis script and it now displays the characters but it keeps looping, I have to hit Ctrl+C to terminate output screen. I will have to figure out why that happens, thanks for explaining to me why things were done in the syntax, I am a hardware tech and code is a new language for me :)

/* this the easiest way. im a kid bt i know programming*/
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
char ch;
int num;
cout<<"Enter the digit : ";
cin>>ch;
cout<<"\n";
num = ch;
cout<<"The ASCII CODE of the digit is : "<<num;
getch();
}

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.