Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 2nd, 2006, 10:32 PM   #1
RemoteC2
Programmer
 
RemoteC2's Avatar
 
Join Date: Jan 2006
Posts: 55
Rep Power: 3 RemoteC2 is on a distinguished road
ascii character generator

nothing too special, just an ascii character generator. just thought that it was kinda cool.

//generate ascii table
#include <iostream>
using namespace std;

int main()
{
    int num[255];
    char str[255];
    for(int i=0; i<255; i++)
    {
            num[i] = i;
            str[i] = (char) num[i];
    }
    for(int j=0; j<255; j++)
    {
            cout << str[j] << "-" << num[j] << endl;
    }
    system("pause");
}
RemoteC2 is offline   Reply With Quote
Old Nov 2nd, 2006, 10:37 PM   #2
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 2,076
Rep Power: 6 Sane will become famous soon enough
Send a message via MSN to Sane
Why do you use arrays? You can just print out the values according to 'i', instead of needlessly storing them in memory.

//generate ascii table
#include <iostream>
using namespace std;

int main()
{
    for(int i=0; i<255; i++)
    {
            cout << (char) i << "-" << i << endl;
    }
    system("pause");
}

Furthermore, if you do actually need it stored in memory, why do you have two different loops? You can output them while you assign them:

//generate ascii table
#include <iostream>
using namespace std;

int main()
{
    int num[255];
    char str[255];
    for(int i=0; i<255; i++)
    {
            num[i] = i;
            str[i] = (char) num[i];

            cout << str[i] << "-" << num[i] << endl;
    }
    system("pause");
}
Sane is online now   Reply With Quote
Old Nov 3rd, 2006, 12:05 AM   #3
RemoteC2
Programmer
 
RemoteC2's Avatar
 
Join Date: Jan 2006
Posts: 55
Rep Power: 3 RemoteC2 is on a distinguished road
i know that there are some problems in it, but it literally took me 5 minutes to program. just thought that it was cool.
RemoteC2 is offline   Reply With Quote
Old Nov 3rd, 2006, 12:11 AM   #4
RemoteC2
Programmer
 
RemoteC2's Avatar
 
Join Date: Jan 2006
Posts: 55
Rep Power: 3 RemoteC2 is on a distinguished road
not that anyone cares, but here is the revised version

//generate ascii table v1.2
#include <iostream>
using namespace std;

int main()
{
    int num;
    char str;
    for(int i=1; i<257; i++)
    {
            num = i;
            str = (char) num;
            cout << str << "-" << num << endl;
    }
    system("pause");
}
RemoteC2 is offline   Reply With Quote
Old Nov 3rd, 2006, 12:29 AM   #5
mrynit
Hobbyist Programmer
 
mrynit's Avatar
 
Join Date: Mar 2006
Location: WA, USA
Posts: 343
Rep Power: 3 mrynit is on a distinguished road
Send a message via AIM to mrynit Send a message via MSN to mrynit Send a message via Yahoo to mrynit Send a message via Skype™ to mrynit
again you are doing more than is needed.

num = i;//why do you need num?
str = (char) num;//num is i so you can just use i here
cout << str << "-" << num << endl;// use i here too

//generate ascii table v1.2
#include <iostream>
using namespace std;

int main()
{
    for(int i=1; i<257; i++)
    {
            cout << (char)i << "-" << i << endl;
    }
    system("pause");
}
__________________
i dont know much about programming but i try to help
mrynit is offline   Reply With Quote
Old Nov 3rd, 2006, 12:32 AM   #6
lectricpharaoh
SEXY SHOELESS GOD OF WAR!
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Wet west coast of Canada
Posts: 1,186
Rep Power: 5 lectricpharaoh will become famous soon enough
You shouldn't go from 1 to 256; rather, you probably intended to go from 0 to 255. I realize that, given an 8-bit char type (which is usually, but not universally, true), it will wrap back to zero, but that's not the point. Also, ASCII only defines 7-bit codes (the upper bit is zero), so ASCII is actually 0 to 127. That said, you'll probably want to skip codes below 32, as they are defined as control characters in ASCII encoding. Last, why do you use an int, and then cast it to char? You can count with a char, too (it's just an integer type with a (usually) smaller range).

Having said all that, you could try to beautify the layout by arranging the output in a table or grid of some kind. Then it would be easier to read.
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot.
- Vaarsuvius, Order of the Stick
lectricpharaoh is offline   Reply With Quote
Old Nov 3rd, 2006, 12:40 AM   #7
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Quote:
given an 8-bit char type (which is usually, but not universally, true)
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Nov 3rd, 2006, 12:58 AM   #8
lectricpharaoh
SEXY SHOELESS GOD OF WAR!
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Wet west coast of Canada
Posts: 1,186
Rep Power: 5 lectricpharaoh will become famous soon enough
I was under the impression the C and C++ standards did no define the exact sizes of the data types. For example, a system could have a 9-bit byte, which would likely mean a 9-bit char type as well, wouldn't it? Clickie.
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot.
- Vaarsuvius, Order of the Stick
lectricpharaoh is offline   Reply With Quote
Old Nov 3rd, 2006, 2:12 AM   #9
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Just wanting to emphasize that while a byte MIGHT have 9 (or more) bits, a char is always ONE byte. The same is not true of an int, for example, which might vary in its byte length. Consequently, types are not necessarily brothers under the skin as regards their exactness.

Off that subject, and onto another, many new users might not have noticed that functions that ask for a char (one byte) often return an int. This is so that values representing an anomolous condition may be distinguished from a full suite of values that represent a char. If you want to have some fun, mess around with EOF.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Reading character input into an array (raw mode) shoeyfighter C 3 Nov 2nd, 2006 4:49 PM
incrementing character array elements value n00b C++ 7 Jun 24th, 2006 4:44 AM
My RPG Field Map Generator Sane Python 5 Oct 28th, 2005 6:20 PM
binary and ascii Nellie C++ 4 Jun 8th, 2005 2:39 PM
ASCII Adjust after Multiplication rick barclay Assembly 1 Apr 25th, 2005 10:42 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 11:56 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC