Path: csiph.com!news.mixmin.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c++,alt.comp.lang.c++.misc,free.test Subject: Re: ASCII Codes Date: Sun, 08 Sep 2024 14:32:59 -0700 Organization: None to speak of Lines: 76 Message-ID: <87h6ap3k6s.fsf@nosuchdomain.example.com> References: MIME-Version: 1.0 Content-Type: text/plain Injection-Date: Sun, 08 Sep 2024 23:33:09 +0200 (CEST) Injection-Info: dont-email.me; posting-host="cc0e421bd7ae7ddfd4a32af9b891a7a5"; logging-data="2175094"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+I4z8P5xDDXiP1Ej8YABuZ" User-Agent: Gnus/5.13 (Gnus v5.13) Cancel-Lock: sha1:PENNI4J0CLW9Sn1HI1ft9T6LG8g= sha1:KS06lQBL0AnmPXPMriIk0j33XmY= Xref: csiph.com comp.lang.c++:119999 alt.comp.lang.c++.misc:7 free.test:1369 Student Project writes: > This one works as expected but there is definitely a room for > improvement. :-) > > #include > #include > > 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 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 */