944,040 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 9361
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Sep 24th, 2004
1

Prog to list ASCII codes (beginners' stuff)

Expand Post »
Hi,

I just started a program containing every single prog we have done for school so far and added some private stuff.
Searching through my older files I found a prog which lists all Integers between 1 and 255 and their respective ASCII char, so I decided to include it into my 'collection'.

The problem is that 'twas a Win32 executable and we're working under DOS. Well actually this wouldn't be much of a problem, but the script seems no to work under DOS.

Well, first of here's the script:
  1. # include <iostream.h>
  2. # include <conio.h>
  3.  
  4. main()
  5. {
  6. int i, j, ip, ipp;
  7. float k, l;
  8. char zeichen;
  9. for (i=1;i<=254;i++)
  10. {
  11. zeichen=i;
  12. cout<<"\n"<<i<<" - "<<zeichen;
  13. j=1;
  14. for (j=1;j<=25;j++)
  15. {
  16. k=1.0*i/20;
  17. l=1.0*j;
  18. if (k==l)
  19. {
  20. ip=i+1;
  21. ipp=i+20;
  22. if (i<=240) cout<<"\n\n(Press any key --> "<<ip<<" to "<<ipp<<" )";
  23. getch();
  24. clrscr();
  25. };
  26. }
  27. }
  28. getch();
  29. }

As I stated, no problems under Win32, but under DOS it stops at 26 and after pressing a key again the screen gets cleared again and stays black.

So my question is..is there any way to get something like this running under DOS?

I tried a simple "cin>>intvar; charvar=intvar; cout<<charvar;" aswell, but any value above 25 simply results in nothing being put out.

Well, thanks in advance.~
Anu
Reputation Points: 11
Solved Threads: 0
Newbie Poster
Anu is offline Offline
11 posts
since Sep 2004
Sep 24th, 2004
0

Re: Prog to list ASCII codes (beginners' stuff)

Skip 26. Or better yet, skip printing everything non-printable.
  1. #include <stdio.h>
  2. #include <ctype.h>
  3.  
  4. int main(void)
  5. {
  6. int i;
  7. for ( i = 0; i < 256; ++i )
  8. {
  9. putchar(isprint(i) ? i : '.');
  10. putchar(i % 16 == 15 ? '\n' : ' ');
  11. }
  12. return 0;
  13. }
  14.  
  15. /* my output
  16.  . . . . . . . . . . . . . . . .
  17.  . . . . . . . . . . . . . . . .
  18.   ! " # $ % & ' ( ) * + , - . /
  19.  0 1 2 3 4 5 6 7 8 9 : ; < = > ?
  20.  @ A B C D E F G H I J K L M N O
  21.  P Q R S T U V W X Y Z [ \ ] ^ _
  22.  ` a b c d e f g h i j k l m n o
  23.  p q r s t u v w x y z { | } ~ .
  24.  . . . . . . . . . . . . . . . .
  25.  . . . . . . . . . . . . . . . .
  26.  . . . . . . . . . . . . . . . .
  27.  . . . . . . . . . . . . . . . .
  28.  . . . . . . . . . . . . . . . .
  29.  . . . . . . . . . . . . . . . .
  30.  . . . . . . . . . . . . . . . .
  31.  . . . . . . . . . . . . . . . .
  32.  */
Last edited by Dave Sinkula; Sep 24th, 2004 at 6:45 pm. Reason: Added code.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Sep 24th, 2004
0

Re: Prog to list ASCII codes (beginners' stuff)

Quote originally posted by Dave Sinkula ...
Skip 26. Or better yet, skip printing everything non-printable.
Wow, thanks for the fast reply.

Well, but how to skip them most easily?

The first and more or less only thing that comes into my mind is like:

if ( i != 26 && i != anotherone && i != yetanotherone &&...
Anu
Reputation Points: 11
Solved Threads: 0
Newbie Poster
Anu is offline Offline
11 posts
since Sep 2004
Sep 24th, 2004
0

Re: Prog to list ASCII codes (beginners' stuff)

[Oops. I see your reply beat my edit.]

I'd use isprint.

  1. #include <stdio.h>
  2. #include <ctype.h>
  3.  
  4. int main(void)
  5. {
  6. int i;
  7. for ( i = 0; i < 128; ++i )
  8. {
  9. putchar(isprint(i) ? i : '.');
  10. putchar(i % 32 == 31 ? '\n' : ' ');
  11. }
  12. return 0;
  13. }
  14.  
  15. /* my output
  16.  . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
  17.   ! " # $ % & ' ( ) * + , - . / 0 1 2 3 4 5 6 7 8 9 : ; < = > ?
  18.  @ 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 [ \ ] ^ _
  19.  ` 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 { | } ~ .
  20.  */
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Sep 24th, 2004
0

Re: Prog to list ASCII codes (beginners' stuff)

I see, thanks again.

But seeing this...

The output under Win32 is like:
  1. 1 - ☺
  2. 2 - ☻
  3. 3 - ♥
  4. 4 - ♦
  5. 5 - ♣
  6. 6 - ♠
  7. 7 -
  8. 8 -
  9. 9 -
  10. 10 -
  11. 11 - ♂
  12. 12 - ♀
  13. 13 -
  14. 14 - ♫
  15. 15 - ☼
  16. 16 - ►
  17. 17 - ◄
  18. 18 - ↕
  19. 19 - ‼
  20. 20 - ¶
  21. 21 - §
And so on, so...

Aint I able to display the 255 ones of those under Dos?
Anu
Reputation Points: 11
Solved Threads: 0
Newbie Poster
Anu is offline Offline
11 posts
since Sep 2004
Sep 24th, 2004
0

Re: Prog to list ASCII codes (beginners' stuff)

Check out http://www.asciitable.com/

The full list of ASCII characters are there. The function isprint() knows which of the characters are printable. isprint() returns a nonzero value if the input is a printable character, including the space character (0x20 – 0x7E). The other characters are either extended, or do other features which aren't print friendly.

More information on isprint()
isprint() is included in the ctype library, ctype.h:

int isprint(int c);
» This function returns nonzero if c is a space or a character for which either isalnum() or ispunct() returns nonzero.

int isalnum(int c);
» This function returns nonzero if c is any of or other locale-specific alphabetic character.
According to AsciiTable.com
Characters	ASCII
a thru z	97 thru 122
A thru Z	65 thru 90
0 thru 9	48 thru 57
int ispunct(int c);
» This function returns nonzero if c is any of the following or other implementation-defined punctuation character:
According to AsciiTable.com
Characters			ASCII
! " # $ % & ' ( ) * + , - . /	33 thru 47
: ; < = > ? @			58 thru 64
[ \ ] ^ _ '			91 thru 96
{ | } ~				123 thru 126

Hope this helps,
- Stack Overflow
Last edited by Stack Overflow; Sep 24th, 2004 at 8:15 pm. Reason: More detailed information
Reputation Points: 26
Solved Threads: 4
Junior Poster
Stack Overflow is offline Offline
185 posts
since Sep 2004
Sep 24th, 2004
0

Re: Prog to list ASCII codes (beginners' stuff)

Thanks, might be quite of use, though probably not directly in what I'm thinking of.

See the attachments, I just want the fourth attachment (Dos, page2) to be like the second one (W32, page2).
Attached Thumbnails
Click image for larger version

Name:	w32a.JPG
Views:	272
Size:	8.5 KB
ID:	621   Click image for larger version

Name:	w32b.JPG
Views:	279
Size:	10.2 KB
ID:	622   Click image for larger version

Name:	dosa.JPG
Views:	278
Size:	10.7 KB
ID:	623   Click image for larger version

Name:	dosb.JPG
Views:	276
Size:	4.3 KB
ID:	624  
Anu
Reputation Points: 11
Solved Threads: 0
Newbie Poster
Anu is offline Offline
11 posts
since Sep 2004
Sep 24th, 2004
0

Re: Prog to list ASCII codes (beginners' stuff)

Quote originally posted by Stack Overflow ...
Check out http://www.asciitable.com/

The full list of ASCII characters are there. The function isprint() knows which of the characters are printable. isprint() returns a nonzero value if the input is a printable character, including the space character (0x20 – 0x7E). The other characters are either extended, or do other features which aren't print friendly.

More information on isprint()
isprint() is included in the ctype library, ctype.h:

int isprint(int c);
» This function returns nonzero if c is a space or a character for which either isalnum() or ispunct() returns nonzero.

int isalnum(int c);
» This function returns nonzero if c is any of or other locale-specific alphabetic character.
According to AsciiTable.com
 Characters	ASCII
 a thru z	97 thru 122
 A thru Z	65 thru 90
 0 thru 9	48 thru 57
int ispunct(int c);
» This function returns nonzero if c is any of the following or other implementation-defined punctuation character:
According to AsciiTable.com
 Characters			ASCII
 ! " # $ % & ' ( ) * + , - . /	33 thru 47
 : ; < = > ? @			58 thru 64
 [ \ ] ^ _ '			91 thru 96
 { | } ~				123 thru 126

Hope this helps,
- Stack Overflow
The greater utility of functions like ispunct and isalnum are that they also work with other character sets, some of which, for example, do not necessarily have a contiguous alphabet or such. They are designed to work regardless of the particular implementation.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Sep 24th, 2004
0

Re: Prog to list ASCII codes (beginners' stuff)

Quote originally posted by Anu ...
See the attachments, I just want the fourth attachment (Dos, page2) to be like the second one (W32, page2).
DOS has a particular issue with the value 26. Apparently it is an end-of-file marker. So on a DOS system, it will not work the way you'd like. Thus my recommendation to skip it.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Sep 25th, 2004
0

Re: Prog to list ASCII codes (beginners' stuff)

That was all I actually wanted to know. Thanks once again. ^^
Anu
Reputation Points: 11
Solved Threads: 0
Newbie Poster
Anu is offline Offline
11 posts
since Sep 2004

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: need little help bout this...
Next Thread in C Forum Timeline: how to place a specific info from input file





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


Follow us on Twitter


© 2011 DaniWeb® LLC