Groups | Search | Server Info | Login | Register


Groups > alt.comp.lang.c++.misc > #7

Re: ASCII Codes

From Keith Thompson <Keith.S.Thompson+u@gmail.com>
Newsgroups comp.lang.c++, alt.comp.lang.c++.misc, free.test
Subject Re: ASCII Codes
Date 2024-09-08 14:32 -0700
Organization None to speak of
Message-ID <87h6ap3k6s.fsf@nosuchdomain.example.com> (permalink)
References <Q%nDO.343686$8jx2.215799@fx17.iad>

Cross-posted to 3 groups.

Show all headers | View raw


Student Project <student@invalid.invalid> writes:
> 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.

> 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.

>     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.

>     vector<string> obj = {
>         "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
>         "exclamation mark",
>         "quotation mark",
[...]
>         "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.


>     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.

> }

-- 
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
void Void(void) { Void(); } /* The recursive call of the void */

Back to alt.comp.lang.c++.misc | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

ASCII Codes Student Project <student@invalid.invalid> - 2024-09-08 20:45 +0000
  Re: ASCII Codes Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-09-08 14:32 -0700
    Re: ASCII Codes "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2024-09-08 14:37 -0700
      Re: ASCII Codes "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2024-09-08 14:39 -0700
    Re: ASCII Codes Chris Ahlstrom <OFeem1987@teleworm.us> - 2024-09-09 09:41 -0400
    Re: ASCII Codes red floyd <no.spam.here@its.invalid> - 2024-09-09 21:44 -0700
  Re: ASCII Codes Bonita Montero <Bonita.Montero@gmail.com> - 2024-09-09 14:32 +0200
  Re: ASCII Codes Ben Bacarisse <ben@bsb.me.uk> - 2024-09-09 13:44 +0100
    Re: ASCII Codes Chris Ahlstrom <OFeem1987@teleworm.us> - 2024-09-09 09:43 -0400
    Re: ASCII Codes Bonita Montero <Bonita.Montero@gmail.com> - 2024-09-09 18:52 +0200
      Re: ASCII Codes Student Project <student@invalid.invalid> - 2024-09-10 01:04 +0100
        Re: ASCII Codes Bonita Montero <Bonita.Montero@gmail.com> - 2024-09-10 10:32 +0200
  Re: ASCII Codes Paavo Helde <eesnimi@osa.pri.ee> - 2024-09-10 07:55 +0300

csiph-web