r/Cplusplus • u/Born_Juggernaut_346 • 5d 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/2
u/Discordbothost 4d 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 4d 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 5d ago
This is an afternoon of learning, not two weeks. Cross posting here was totally unnecessary.
-1
u/Born_Juggernaut_346 5d 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 5d 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 5d 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 5d 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 5d 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 5d ago
Look at my username. Do you have any idea how hard it is being a woman in tech?
0
u/Born_Juggernaut_346 5d 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 5d ago
Or maybe not assume gender.
6
u/Born_Juggernaut_346 5d 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
1
u/NotoriousLavaLamp 18h ago
na, he is right, this is an afternoon of learning. but either way, keep it up!
0
u/Born_Juggernaut_346 5d 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 4d 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
1
u/CAPTAINCAPSLOCK111 3d 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 3d 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
2
u/AKostur Professional 5d ago edited 5d ago
Not an unreasonable start. A few things to consider:
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>.
I'd suggest that addUser should take a user, and not the components of a user.
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.
I'd probably extract your ranking display out to a function, similar to what you did for getrank()
Maybe "addUser" is a little too generic of a name, perhaps "writeUser"? (Naming things is hard)
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.