Homework help needed for displaying ASCII Characters

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Oct 2009
Posts: 17
Reputation: C++ Beginner is an unknown quantity at this point 
Solved Threads: 0
C++ Beginner C++ Beginner is offline Offline
Newbie Poster

Homework help needed for displaying ASCII Characters

 
0
  #1
Oct 21st, 2009
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;
}
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 312
Reputation: Clinton Portis is an unknown quantity at this point 
Solved Threads: 32
Clinton Portis's Avatar
Clinton Portis Clinton Portis is online now Online
Posting Whiz
 
0
  #2
Oct 21st, 2009
Try this:
  1.  
  2. char letter = 32;
  3. int counter = 0;
  4.  
  5. while (letter < 128)
  6. {
  7. cout << letter << ' ';
  8.  
  9. counter++;
  10.  
  11. if(counter == 16)
  12. {
  13. cout << endl;
  14. counter = 0;
  15. }
  16.  
  17. letter++;
  18. }
Last edited by Clinton Portis; Oct 21st, 2009 at 11:43 am.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 17
Reputation: C++ Beginner is an unknown quantity at this point 
Solved Threads: 0
C++ Beginner C++ Beginner is offline Offline
Newbie Poster
 
-1
  #3
Oct 21st, 2009
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;
}
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 17
Reputation: C++ Beginner is an unknown quantity at this point 
Solved Threads: 0
C++ Beginner C++ Beginner is offline Offline
Newbie Poster
 
0
  #4
Oct 23rd, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,678
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 263
Lerner Lerner is offline Offline
Posting Virtuoso
 
0
  #5
Oct 23rd, 2009
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:
  1. #include iostream
  2. using namespace std;
  3. int main()
  4. {
  5. char letter = 32;
  6. cout << letter;
  7. cin.get();
  8. return 0;
  9. }
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:
  1. #include iostream
  2. using namespace std;
  3. int main()
  4. {
  5. char letter = (char)32;
  6. cout << letter;
  7. cin.get();
  8. return 0;
  9. }
though you may want to use the up to date C++ version of casting rather than the older C version I demonstrated above.
Klatu Barada Nikto
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 17
Reputation: C++ Beginner is an unknown quantity at this point 
Solved Threads: 0
C++ Beginner C++ Beginner is offline Offline
Newbie Poster
 
0
  #6
Oct 24th, 2009
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
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC