Discussion:
ASCII Codes
(too old to reply)
Student Project
2024-09-08 20:45:54 UTC
Permalink
This one works as expected but there is definitely a room for
improvement. :-)

#include <iostream>
#include <vector>

using namespace std;

int main()
{
system("color 0A");
system("cls");
vector<string> obj = {
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"exclamation mark",
"quotation mark",
"number sign",
"dollar sign",
"percent sign",
"ampersand",
"apostrophe",
"left parenthesis",
"right parenthesis",
"asterisk",
"plus sign",
"comma",
"hyphen",
"period",
"slash",
"digit 0",
"digit 1",
"digit 2",
"digit 3",
"digit 4",
"digit 5",
"digit 6",
"digit 7",
"digit 8",
"digit 9",
"colon",
"semicolon",
"less-than",
"equals-to",
"greater-than",
"question mark",
"at sign",
"uppercase A",
"uppercase B",
"uppercase C",
"uppercase D",
"uppercase E",
"uppercase F",
"uppercase G",
"uppercase H",
"uppercase I",
"uppercase J",
"uppercase K",
"uppercase L",
"uppercase M",
"uppercase N",
"uppercase O",
"uppercase P",
"uppercase Q",
"uppercase R",
"uppercase S",
"uppercase T",
"uppercase U",
"uppercase V",
"uppercase W",
"uppercase X",
"uppercase Y",
"uppercase Z",
"left square bracket",
"backslash",
"right square bracket",
"caret",
"underscore",
"grave accent",
"lowercase a",
"lowercase b",
"lowercase c",
"lowercase d",
"lowercase e",
"lowercase f",
"lowercase g",
"lowercase h",
"lowercase i",
"lowercase j",
"lowercase k",
"lowercase l",
"lowercase m",
"lowercase n",
"lowercase o",
"lowercase p",
"lowercase q",
"lowercase r",
"lowercase s",
"lowercase t",
"lowercase u",
"lowercase v",
"lowercase w",
"lowercase x",
"lowercase y",
"lowercase z",
"left curly brace",
"vertical bar",
"right curly brace",
"tilde" };

cout << "\tCode" << "\tCharacter" << "\tWhat it means" << "\n";
cout << "\t#####################################\n\n";
for (int i = 33; i <= 126; i++)
{
cout << "\t" << i << "\t" << (char)i << "\t\t" << obj[i] << "\n";
}

return 0;
}
Keith Thompson
2024-09-08 21:32:59 UTC
Permalink
Post by Student Project
This one works as expected but there is definitely a room for
improvement. :-)
#include <iostream>
#include <vector>
using namespace std;
I suggest dropping this and using `std::vector`, `std::string`,
`std::cout` explicitly. It's a bit more typing, but IMHO it makes the
code clearer.
Post by Student Project
int main()
{
system("color 0A");
This set the console to black background (0) and light green foreground
(A), but only under cmd.exe on Windows.
Post by Student Project
system("cls");
This clears the screen, but only on Windows.

Both of these needlessly limit the portability of your program.

On a system that doesn't have "color" and "cls" commands, the calls to
system() will fail silently and do nothing, which is probably ok.

But why would you want to clear the screen and change the foreground and
background colors anyway? Why is that part of the desired behavior of
your program?

If I want to run a program that displays data on my screen, I'm going to
be seriously annoyed if it erases *my* existing data and messes with my
foreground and background colors.
Post by Student Project
vector<string> obj = {
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"exclamation mark",
"quotation mark",
[...]
Post by Student Project
"left curly brace",
"vertical bar",
"right curly brace",
"tilde" };
An observation: Writing the code this way makes it very easy to
introduce errors that are going to be hard to detect. If you one extra
or missing entry on that first line ("", "", "", ...), it's going to
invalidate the entire output. You haven't made such an error as far as
I can tell, but think about how might make the code more robust.

One possibility is to read the data from a file that's known to be
valid. Another is to write a program that reads from such a file and
generates C++ code that you include in your program. I'm not
necessarily suggesting that you do this right away, but it's something
to think about.
Post by Student Project
cout << "\tCode" << "\tCharacter" << "\tWhat it means" << "\n";
cout << "\t#####################################\n\n";
for (int i = 33; i <= 126; i++)
{
cout << "\t" << i << "\t" << (char)i << "\t\t" << obj[i] << "\n";
}
return 0;
The "return 0;" is not strictly necessary, but it's harmless.
Post by Student Project
}
--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+***@gmail.com
void Void(void) { Void(); } /* The recursive call of the void */
Chris M. Thomasson
2024-09-08 21:37:48 UTC
Permalink
Post by Keith Thompson
Post by Student Project
This one works as expected but there is definitely a room for
improvement. :-)
#include <iostream>
#include <vector>
using namespace std;
I suggest dropping this and using `std::vector`, `std::string`,
`std::cout` explicitly. It's a bit more typing, but IMHO it makes the
code clearer.
Post by Student Project
int main()
{
system("color 0A");
This set the console to black background (0) and light green foreground
(A), but only under cmd.exe on Windows.
Post by Student Project
system("cls");
This clears the screen, but only on Windows.
Both of these needlessly limit the portability of your program.
On a system that doesn't have "color" and "cls" commands, the calls to
system() will fail silently and do nothing, which is probably ok.
But why would you want to clear the screen and change the foreground and
background colors anyway? Why is that part of the desired behavior of
your program?
If I want to run a program that displays data on my screen, I'm going to
be seriously annoyed if it erases *my* existing data and messes with my
foreground and background colors.
[...]

Applesoft Basic Online:

https://www.calormen.com/jsbasic/
Chris M. Thomasson
2024-09-08 21:39:31 UTC
Permalink
Post by Chris M. Thomasson
Post by Keith Thompson
Post by Student Project
This one works as expected but there is definitely a room for
improvement. :-)
#include <iostream>
#include <vector>
using namespace std;
I suggest dropping this and using `std::vector`, `std::string`,
`std::cout` explicitly.  It's a bit more typing, but IMHO it makes the
code clearer.
Post by Student Project
int main()
{
     system("color 0A");
This set the console to black background (0) and light green foreground
(A), but only under cmd.exe on Windows.
Post by Student Project
     system("cls");
This clears the screen, but only on Windows.
Both of these needlessly limit the portability of your program.
On a system that doesn't have "color" and "cls" commands, the calls to
system() will fail silently and do nothing, which is probably ok.
But why would you want to clear the screen and change the foreground and
background colors anyway?  Why is that part of the desired behavior of
your program?
If I want to run a program that displays data on my screen, I'm going to
be seriously annoyed if it erases *my* existing data and messes with my
foreground and background colors.
[...]
https://www.calormen.com/jsbasic/
Be sure to click on the run button. The color is interesting to me... ;^)

Loading Image...
Chris Ahlstrom
2024-09-09 13:41:54 UTC
Permalink
Post by Keith Thompson
Post by Student Project
This one works as expected but there is definitely a room for
improvement. :-)
#include <iostream>
#include <vector>
using namespace std;
I suggest dropping this and using `std::vector`, `std::string`,
`std::cout` explicitly. It's a bit more typing, but IMHO it makes the
code clearer.
That's my feeling to. Explicit namespaces mean that the names of variables do
not have to be as long.
Post by Keith Thompson
<other good advice snipped>
--
void Void(void) { Void(); } /* The recursive call of the void */
Chuckling at the sig.
--
Someone is speaking well of you.

How unusual!
red floyd
2024-09-10 04:44:22 UTC
Permalink
Post by Keith Thompson
Post by Student Project
This one works as expected but there is definitely a room for
improvement. :-)
#include <iostream>
#include <vector>
using namespace std;
I suggest dropping this and using `std::vector`, `std::string`,
`std::cout` explicitly. It's a bit more typing, but IMHO it makes the
code clearer.
Post by Student Project
int main()
{
system("color 0A");
This set the console to black background (0) and light green foreground
(A), but only under cmd.exe on Windows.
Post by Student Project
system("cls");
This clears the screen, but only on Windows.
Both of these needlessly limit the portability of your program.
On a system that doesn't have "color" and "cls" commands, the calls to
system() will fail silently and do nothing, which is probably ok.
But why would you want to clear the screen and change the foreground and
background colors anyway? Why is that part of the desired behavior of
your program?
If I want to run a program that displays data on my screen, I'm going to
be seriously annoyed if it erases *my* existing data and messes with my
foreground and background colors.
Post by Student Project
vector<string> obj = {
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"exclamation mark",
"quotation mark",
[...]
Post by Student Project
"left curly brace",
"vertical bar",
"right curly brace",
"tilde" };
An observation: Writing the code this way makes it very easy to
introduce errors that are going to be hard to detect. If you one extra
or missing entry on that first line ("", "", "", ...), it's going to
invalidate the entire output. You haven't made such an error as far as
I can tell, but think about how might make the code more robust.
One possibility is to read the data from a file that's known to be
valid. Another is to write a program that reads from such a file and
generates C++ code that you include in your program. I'm not
necessarily suggesting that you do this right away, but it's something
to think about.
Or use a std::map<char, const char *> indexed by the character (or
alternatively, a std::map<char, std::string>...

const std::map<char, const char *> {
{ '!', "Exclamation mark" },
{ '"', "Quotation mark" },
...
};

That way, you don't have to worry about getting out of sync.
Bonita Montero
2024-09-09 12:32:21 UTC
Permalink
Ain't this more convenient as a map ?

#include <iostream>
#include <map>

using namespace std;

int main ()
{
system( "color 0A" );
system( "cls" );
map<char, char const *> obj =
{
{ '!', "exclamation mark" },
{ '"', "quotation mark" },
{ '#', "number sign" },
{ '$', "dollar sign" },
{ '%', "percent sign" },
{ '&', "ampersand" },
{ '\'', "apostrophe" },
{ '(', "left parenthesis" },
{ ')', "right parenthesis" },
{ '*', "asterisk" },
{ '+', "plus sign" },
{ ',', "comma" },
{ '-', "hyphen" },
{ '.', "period" },
{ '/', "slash" },
{ '0', "digit 0" },
{ '1', "digit 1" },
{ '2', "digit 2" },
{ '3', "digit 3" },
{ '4', "digit 4" },
{ '5', "digit 5" },
{ '6', "digit 6" },
{ '7', "digit 7" },
{ '8', "digit 8" },
{ '9', "digit 9" },
{ ':', "colon" },
{ ';', "semicolon" },
{ '<', "less-than" },
{ '=', "equals-to" },
{ '>', "greater-than" },
{ '?', "question mark" },
{ '@', "at sign" },
{ 'A', "uppercase A" },
{ 'B', "uppercase B" },
{ 'C', "uppercase C" },
{ 'D', "uppercase D" },
{ 'E', "uppercase E" },
{ 'F', "uppercase F" },
{ 'G', "uppercase G" },
{ 'H', "uppercase H" },
{ 'I', "uppercase I" },
{ 'J', "uppercase J" },
{ 'K', "uppercase K" },
{ 'L', "uppercase L" },
{ 'M', "uppercase M" },
{ 'N', "uppercase N" },
{ 'O', "uppercase O" },
{ 'P', "uppercase P" },
{ 'Q', "uppercase Q" },
{ 'R', "uppercase R" },
{ 'S', "uppercase S" },
{ 'T', "uppercase T" },
{ 'U', "uppercase U" },
{ 'V', "uppercase V" },
{ 'W', "uppercase W" },
{ 'X', "uppercase X" },
{ 'Y', "uppercase Y" },
{ 'Z', "uppercase Z" },
{ '[', "left square bracket" },
{ '\\', "backslash" },
{ ']', "right square bracket" },
{ '^', "caret" },
{ '_', "underscore" },
{ '`', "grave accent" },
{ 'a', "lowercase a" },
{ 'b', "lowercase b" },
{ 'c', "lowercase c" },
{ 'd', "lowercase d" },
{ 'e', "lowercase e" },
{ 'f', "lowercase f" },
{ 'g', "lowercase g" },
{ 'h', "lowercase h" },
{ 'i', "lowercase i" },
{ 'j', "lowercase j" },
{ 'k', "lowercase k" },
{ 'l', "lowercase l" },
{ 'm', "lowercase m" },
{ 'n', "lowercase n" },
{ 'o', "lowercase o" },
{ 'p', "lowercase p" },
{ 'q', "lowercase q" },
{ 'r', "lowercase r" },
{ 's', "lowercase s" },
{ 't', "lowercase t" },
{ 'u', "lowercase u" },
{ 'v', "lowercase v" },
{ 'w', "lowercase w" },
{ 'x', "lowercase x" },
{ 'y', "lowercase y" },
{ 'z', "lowercase z" },
{ '{', "left curly brace" },
{ '|', "vertical bar" },
{ '}', "right curly brace" },
{ '~', "tilde" }
};
cout << "\tCode" << "\tCharacter" << "\tWhat it means" << "\n";
cout << "\t#####################################\n\n";
for( pair<char const, char const *> &p : obj )
cout << "\t" << (int)p.first << "\t" << (char)p.first << "\t\t" <<
p.second << "\n";
return 0;
}
Ben Bacarisse
2024-09-09 12:44:40 UTC
Permalink
Post by Student Project
This one works as expected but there is definitely a room for
improvement. :-)
#include <iostream>
#include <vector>
using namespace std;
Students tend to do this because it save a little typing, but it's
generally considered a bad idea in anything but the shortest programs.
Post by Student Project
int main()
{
system("color 0A");
system("cls");
vector<string> obj = {
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"exclamation mark",
"quotation mark",
"number sign",
...
Post by Student Project
"digit 0",
...
Post by Student Project
"digit 9",
"colon",
...
Post by Student Project
"at sign",
"uppercase A",
...
Post by Student Project
"uppercase Z",
...
Post by Student Project
"grave accent",
"lowercase a",
...
Post by Student Project
"lowercase z",
"left curly brace",
"vertical bar",
"right curly brace",
"tilde" };
One thing that would help a lot would be to stop relying on getting the
order exactly right. You could, at the expense of more typing, use
assignments:

obj['!'] = "exclamation mark";

This would make any errors obvious.

And then, if you wanted to try something a little more sophisticated,
you could use a loop for the digits and the letters.
Post by Student Project
cout << "\tCode" << "\tCharacter" << "\tWhat it means" << "\n";
cout << "\t#####################################\n\n";
for (int i = 33; i <= 126; i++)
{
cout << "\t" << i << "\t" << (char)i << "\t\t" << obj[i] << "\n";
}
return 0;
}
--
Ben.
Chris Ahlstrom
2024-09-09 13:43:05 UTC
Permalink
Post by Ben Bacarisse
Post by Student Project
This one works as expected but there is definitely a room for
improvement. :-)
#include <iostream>
#include <vector>
using namespace std;
Students tend to do this because it save a little typing, but it's
generally considered a bad idea in anything but the shortest programs.
Or functions.
--
Push where it gives and scratch where it itches.
Bonita Montero
2024-09-09 16:52:34 UTC
Permalink
Post by Ben Bacarisse
One thing that would help a lot would be to stop relying on getting the
order exactly right. ...
This also would help not to construct so much temporary string objects.
Unfortunately C++ never moves from an initializer list.
I chose a map and don't use a string, but just a C-string pointer.
Student Project
2024-09-10 00:04:31 UTC
Permalink
Post by Bonita Montero
This also would help not to construct so much temporary string objects.
Removed empty strings in vector and changed the code slightly to this:

for (int i = 33; i <= 126; i++)
{
cout << "\t" << i << "\t" << (char)i << "\t\t" << obj[i - 33] << "\n";
}

The obj now starts from 0.
Bonita Montero
2024-09-10 08:32:01 UTC
Permalink
Post by Student Project
Post by Bonita Montero
This also would help not to construct so much temporary string objects.
for (int i = 33; i <= 126; i++)
   {
       cout << "\t" << i << "\t" << (char)i << "\t\t" << obj[i - 33] <<
"\n";
   }
The obj now starts from 0.
Better use a map as I've shown.
Thats's only slightly slower and much less error-prone.

Paavo Helde
2024-09-10 04:55:14 UTC
Permalink
Post by Student Project
This one works as expected but there is definitely a room for
improvement. :-)
#include <iostream>
#include <vector>
using namespace std;
int main()
{
system("color 0A");
system("cls");
vector<string> obj = {
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
I believe the last one above should be "space", not "". It is commonly
grouped together with other punctuation characters like "exclamation mark".
Post by Student Project
"exclamation mark",
"quotation mark",
Loading...