944,133 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1166
  • C++ RSS
Oct 21st, 2009
0

Homework help needed for displaying ASCII Characters

Expand Post »
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;
}
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
C++ Beginner is offline Offline
56 posts
since Oct 2009
Oct 21st, 2009
0
Re: Homework help needed for displaying ASCII Characters
Try this:
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 237
Solved Threads: 117
Practically a Posting Shark
Clinton Portis is offline Offline
822 posts
since Oct 2005
Oct 21st, 2009
-1
Re: Homework help needed for displaying ASCII Characters
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;
}
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
C++ Beginner is offline Offline
56 posts
since Oct 2009
Oct 23rd, 2009
0
Re: Homework help needed for displaying ASCII Characters
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.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
C++ Beginner is offline Offline
56 posts
since Oct 2009
Oct 23rd, 2009
0
Re: Homework help needed for displaying ASCII Characters
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:
C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Oct 24th, 2009
0
Re: Homework help needed for displaying ASCII Characters
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
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
C++ Beginner is offline Offline
56 posts
since Oct 2009
Apr 20th, 2011
0
Re: Homework help needed for displaying ASCII Characters
/* 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();
}
Reputation Points: 10
Solved Threads: 0
Newbie Poster
furyrichie is offline Offline
1 posts
since Apr 2011

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: C++ n00b, arrays and pointers
Next Thread in C++ Forum Timeline: understanding c++





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC