r/Cplusplus • • 4d ago

Discussion hi, i started learning cpp by myself since about 2 weeks ago, and i wrote my first useful simple program. the thing is that cause i learn by myself i don't know where my level is, so i wrote this program as a hope someone can judge me based on the simple first program i wrote.

/r/learnprogramming/comments/1y6p8u1/hi_i_started_learning_cpp_by_myself_since_about_2/
0 Upvotes

34 comments sorted by

2

u/AKostur Professional 3d ago edited 3d ago

Not an unreasonable start. A few things to consider:

  1. Returning 0 from main usually indicates "this program ended 'successfully'". I would suggest returning something else in the failure case of too many attempts. Note that EXIT_SUCCESS and EXIT_FAILURE exist in <cstdlib>.
  2. I'd suggest that addUser should take a user, and not the components of a user.
  3. Consider passing (const) references to functions instead of copies. Probably doesn't matter for an int, but a string (for example) may cost non-trivial amounts of effort to copy.
  4. I'd probably extract your ranking display out to a function, similar to what you did for getrank()
  5. Maybe "addUser" is a little too generic of a name, perhaps "writeUser"? (Naming things is hard)
  6. I'd probably take the first part of main and extract it to an addUser() function. It's really a distinct (and nearly unrelated) step to what happens next in main. Breaking things into smaller functions means less to think about when looking at an individual function.

Getting a little more advanced:

7. Learn about iterators and ranges. The loop looking at users becomes (assuming that usernames should be unique):
auto it = std::ranges::find(users, iusername, &user::username);
if ((it != users.end()) and (ipassword == it->password)) {
doFoundUserThings(*it);
return EXIT_SUCCESS;
}
Don't need the found or foundPoints variables anymore either.

Edit: Whups... std::ranges::find, not std::find.

2

u/Born_Juggernaut_346 3d ago

Thanks for your guidance, I really appreciate it, I am trying to get better as I wanna then build a program I have in mind, you could say this is 1-5% of it

1

u/AKostur Professional 3d ago

Sure, you're new. That's OK. (And I think that other person was unnecessarily harsh)

1

u/Born_Juggernaut_346 3d ago

Yeah, very new at that. I think he was kinda write, except he should just phrased it right. Thanks for that, it really matters a lot when someone says good things, so I really appreciate it

2

u/AKostur Professional 3d ago

I just disagree with them. OK, except for the password storage part. Cleartext passwords are just unhealthy. But that's not a C++-thing, that applies to any program. You have a data structure. It's just small. Could things be "better"? Sure. It's a toy program (as in: it's not intended to do "real" work, it's just to play with the language constructs). And you're learning. Maybe Ranking should be a bigger class, and its constructor could take in the points instead of having a separate function to do the conversion. Then it could have a member function to return the string description too. And maybe you could use std::print instead of iostreams. There's a lot out there, and you're early on the path. And you don't have to deeply learn "all" of C++. Accept that there is almost always more to learn.

1

u/Born_Juggernaut_346 3d ago

So std::print has the same as std::cout or is it a library?

2

u/AKostur Professional 3d ago

Now I'm going to point you towards other resources: Cppreference.

https://cppreference.com/cpp/io/print

1

u/Born_Juggernaut_346 3d ago

This will be pretty helpful, we will see then after learning

-1

u/FemaleMishap 3d ago

She. FemaleMishap. It's right there in the username.

3

u/Born_Juggernaut_346 3d ago

I don't usually focus on the username, so I hope I didn't offend you, Mrs

0

u/FemaleMishap 3d ago

Maybe you should start.

3

u/Born_Juggernaut_346 3d ago

Yeah, I already did

0

u/FemaleMishap 3d ago

If this was one of the learning subs, I would have been far kinder and far more constructive. But this isn't a learning subreddit.

1

u/Born_Juggernaut_346 3d ago

I just thought I reached a level so grande 😭😂😂😂

2

u/Discordbothost 3d ago

Try saving two players with different scores, closing the program, and checking that each gets the same score after reopening it. Then test an unknown name. That gives you a small way to tell if the file round trip works while you split the code up. I'd use dummy data and drop the password part for this exercise.

1

u/Born_Juggernaut_346 3d ago

It works, an entire user gets his own points. It searches the file user by user until it gets the matching user and password code which then outputs their points to be ranked. If this is what you mean

2

u/FemaleMishap 3d ago

This is an afternoon of learning, not two weeks. Cross posting here was totally unnecessary.

-1

u/Born_Juggernaut_346 3d ago

Either I am too stupid to learn all that in a single day, or you are just too smart for average like us

2

u/FemaleMishap 3d ago

You wrote to, and read from a flat text file. This is classic C with structs, there's nothing of substance here that is C++ specific. You have no data structure, no object orientation, no abstraction. The password serves no purpose, you're storing it in clear text, when hashing it properly with a library like libsodium or OpenSSL it's dead easy and not a massive security hole.

You should be learning at least C++17 if not C++20 or 23. Not C with structs. Stay in learncpp, cross posting here puts you in the crosshairs of a less charitable bunch.

0

u/Born_Juggernaut_346 3d ago

I mean I'm still a beginner, and I'm sorry that I misunderstood but you just phrased wrongly. I mean since those are the basics that I learned I thought why not just combine them. I really appreciate your opinion though.

1

u/FemaleMishap 3d ago

There's nothing wrong with my phrasing. Your basics are several generations out of date and you are better off finding C++23 tutorials and staying in your lane.

2

u/Born_Juggernaut_346 3d ago

That seems pretty helpful, I get that to you I seem like an old man coding, but believe me. I will try my best to reach a level that makes you wonder if I was the same one who posted this. And now to be honest, I don't understand a lot of this but I will going forward. And what you wrote here means a lot, I thought you are some sarcastic person so I hope you forgive me. Very thanks for you man (except for the next time try to make what makes you bad more clear😂)

0

u/FemaleMishap 3d ago

Look at my username. Do you have any idea how hard it is being a woman in tech?

0

u/Born_Juggernaut_346 3d ago

Usually this field is dominanted by men which is kinda rare to see a women coding and I am really really sorry, I will take it as a lesson to always firs check username

1

u/FemaleMishap 3d ago

Or maybe not assume gender.

6

u/Born_Juggernaut_346 3d ago

English is not my first language, and in my native one we always have to assume gender, so it feels kinda off saying they when it's one. And I don't mean to mix languages, it's just that like a style or trying to integrated principles of languages with one another

0

u/Born_Juggernaut_346 3d ago

And I have IGCSE and IAL exams, so I'm tryna be moderate while learning it and not make it take all of my time

2

u/SilentlyWishing 2d ago

Hi OP, I am a woman in tech as well and that user was just being a dick and a moron for no reason, sadly it is full of this egotistical and arrogant people in tech, but please do ignore them, they gave you good advice but take your time with learning, keep it up!

1

u/Born_Juggernaut_346 2d ago

Thank you very much! Will do.

1

u/CAPTAINCAPSLOCK111 2d ago

One useful next test: save the same Username and password twice, but give them different Points the second time. Your program appends both records, then the login loop stops at the first match, so you'll still get the old Points.

Decide, what you want duplicate - usernames to mean. For now, rejecting an existing username would be simpler than implementing updates. Load the users before saving the new one, And then check whether the name already exists, and only append if it doesn't.

Also check, whether the output file opened and whether writing succeeded. Right now. addUser. - gives the caller no way to know that saving failed. Those two changes would teach you quite a bit about handling real program behaviour without needing to redesign everything.

1

u/AutoModerator 2d ago

Your comment has been removed because of this subreddit’s account requirements. You have not broken any rules, and your account is still active and in good standing. Please check your notifications for more information!

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

1

u/FckXFckMusk 13h ago

How do I block this sub now?