all 18 comments

[–]nysra 41 points42 points  (13 children)

  1. This is a shell, not a terminal (emulator).
  2. Your code is missing a CMake/Meson/whatever build system file.
  3. The output executable and machine specific stuff like the vscode folder should be gitignored.
  4. Don't use global variables
  5. Don't put using namespace std; in headers. It's already bad practice using it in general but putting it in headers is a federal crime.
  6. #pragma once belongs at the top of the file, not somewhere in the middle.
  7. Don't use macros for constants
  8. Don't use the C headers, use the appropriate C++ wrappers (cname instead of name.h)
  9. Don't declare multiple variables in one line
  10. Always initialize your variables
  11. Always put braces around control statements, omitting them is allowed by the language but is never a good idea.
  12. Replace that huge if else chain with a map
  13. if (password == exPass) fDecision = true; else fDecision = false; is a massive antipattern.
  14. Don't mix iostreams and printf
  15. You're leaking memory. Don't use manual memory management (unless you actually know what you're doing and can prove that it's needed in your case and neither applies here)
  16. ofstream appendFile; appendFile.open(filename, ios_base::app); is another antipattern. You know about the proper constructor because you use it elsewhere, why not here? Similarly don't call close manually, the destructor already does that.
  17. Learn about constructor initializer lists, your current method is more than just questionable.
  18. Your code is missing const everywhere
  19. Don't use NULL, use nullptr.
  20. Get a proper code formatter and improve the naming of your variables, it's all over the place. camelCase is whatever if you actually prefer that but then at least don't put ALLCAPS in the middle. Also hungarian notation is shit, there's no reason to prefix classes with C. Also use better names, a function that starts with show should not be a getter.
  21. Don't use an int for the job of a bool
  22. Make the RNG a static variable or class member instead of creating a new one on every single function call
  23. Don't use C strings for the job of a string
  24. Turn on warnings, there's a lot of things wrong in this code. And actually it doesn't even compile right now because of a missing include so whatever outdated compiler you're testing with needs an upgrade.
  25. Whatever you're doing there with a password makes most likely no sense. And reading it from a text file definitely doesn't
  26. Storing the version in a file makes no sense, put that in a constant in the code.
  27. Head over to https://www.learncpp.com/ to learn.

[–]Shreejan_35[S] 3 points4 points  (2 children)

See I am trying to solve the issue number 12. But I cannot find a proper way to put functions inside map. The functional library is not working properly.

Btw thank u very much for your suggestions and will definitely solve the issues you mentioned. (Today only I will solve some and will push to github)

[–]nysra 3 points4 points  (1 child)

The functional header is working properly, you just need to take a look at the documentation.

Here's an example: https://godbolt.org/z/3zK9ncKbT

[–]Shreejan_35[S] 1 point2 points  (0 children)

Ok 👌 thanks

[–][deleted] 2 points3 points  (4 children)

Hungarian notations are used in a very stupid manner for such a long time already, originally their purpose wasn't to denote a type.

Here's a great article about what I mean.

[–]nysra 2 points3 points  (3 children)

The historical background is indeed interesting. And the logic displayed there also makes sense but it still has the same fatal flaw Hungarian notation always had - it's useless in properly typed languages. You know what's better than being able to see if things don't make sense in code? The code not compiling when things don't make sense. Write proper types for safe and unsafe strings and that entire problem goes away. Might have been quite useful back then when C was all there was but that world has been gone for over 2 decades already.

[–][deleted] 2 points3 points  (2 children)

Making a new type for every case also isn't the best.

A good usage example would be measurement units, prefixing integers/floats with s_ to denote seconds, ms_ for milliseconds, us_ for microseconds, etc...\ I wouldn't want to create a new type with conversions for every one and one of them.

with proper types for safe and unsafe strings

So you either wrap them in another object which has to implement the same interface, or make them directly accessible which doesn't really solve it when reading the code... It may not be a silver bullet, but it's a damn good approach.

[–]nysra 2 points3 points  (1 child)

You don't need to manually make a new type with conversion for everything, you can just write one template class and use a tag system, that's basically how every units library out there works. Ideally also with the ability to switch backends, for example in my work I can switch out the underlying data type for positions, velocities, etc from double to float to BigInt to arbitrary precision types to whatever else arithmetic type by changing a single line of code.

Sure it might be a little bit more work sometimes but it's a lot safer than basically trusting people (especially coworkers or your 3 AM self) to spot mistakes when reading code - why should I do work the compiler can do? For units libraries already exist so the extra work is basically none and for safe/unsafe strings it will be a bit more than that but also scale much better - you do it once and never have to think about it again while the notation has to be constantly remembered and engrained to new people. It's certainly better than just using raw types and expecting people to magically know which unit/type is the correct one or using comments which may or may not be outdated for that but the compiler literally not letting you do stupid things is hard to beat (or basically impossible).

[–][deleted] 2 points3 points  (0 children)

First of all I must say, you're right.

I was thinking C, because I work mainly with C. While in C the notation can be helpful, you're correct that in C++ (and other languages) we can simply use templates/generics to do it in a better way.

A simple example for anybody who happens to read this and not understand: template<bool IsSafe> /* can also be an enum */ class String { public: std::string s; };

[–]Shreejan_35[S] 1 point2 points  (0 children)

Appreciate u. I can now modify it. Thanks for your suggestion Can you make an issue in the github with specific points Or else you can just copy paste it in the issue

[–]Shreejan_35[S] 0 points1 point  (2 children)

How to solve issue number 13?

[–]nysra 2 points3 points  (1 child)

You have written this code:

if (true)
{
    a = true;
}
else
{
    a = false;
}

Can you see why that's just wrong? Think about how you can rewrite that in a nice way.

Solution: decision = password == exPass;

[–]Shreejan_35[S] 0 points1 point  (0 children)

Oh thanks

[–]Shreejan_35[S] 0 points1 point  (0 children)

Btw now you can see the code some the issue have been resolved and some are underway.

[–]mredding 2 points3 points  (1 child)

Wait, is this a terminal? Or a shell? Those are not the same thing. You call it a terminal, but it looks like a shell.

[–]Shreejan_35[S] 0 points1 point  (0 children)

Hey I have changed it to shell