![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Jan 2006
Posts: 55
Rep Power: 3
![]() |
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");
} |
|
|
|
|
|
#2 |
|
Banned
![]() ![]() |
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");
} |
|
|
|
|
|
#3 |
|
Programmer
Join Date: Jan 2006
Posts: 55
Rep Power: 3
![]() |
i know that there are some problems in it, but it literally took me 5 minutes to program. just thought that it was cool.
|
|
|
|
|
|
#4 |
|
Programmer
Join Date: Jan 2006
Posts: 55
Rep Power: 3
![]() |
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");
} |
|
|
|
|
|
#5 |
|
Hobbyist Programmer
|
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 |
|
|
|
|
|
#6 |
|
SEXY SHOELESS GOD OF WAR!
![]() Join Date: Jun 2005
Location: Wet west coast of Canada
Posts: 1,197
Rep Power: 5
![]() |
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 |
|
|
|
|
|
#7 | |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Quote:
![]()
__________________
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 |
|
|
|
|
|
|
#8 |
|
SEXY SHOELESS GOD OF WAR!
![]() Join Date: Jun 2005
Location: Wet west coast of Canada
Posts: 1,197
Rep Power: 5
![]() |
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 |
|
|
|
|
|
#9 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
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 |