The Big Programming Thread - Page 837
Forum Index > General Forum |
Thread Rules 1. This is not a "do my homework for me" thread. If you have specific questions, ask, but don't post an assignment or homework problem and expect an exact solution. 2. No recruiting for your cockamamie projects (you won't replace facebook with 3 dudes you found on the internet and $20) 3. If you can't articulate why a language is bad, don't start slinging shit about it. Just remember that nothing is worse than making CSS IE6 compatible. 4. Use [code] tags to format code blocks. | ||
Mataru
Norway356 Posts
| ||
Shield
Bulgaria4824 Posts
![]() | ||
dsyxelic
United States1417 Posts
On February 08 2017 06:36 Acrofales wrote: If you don't have a server, I don't think there's much point to logging in at all, but in that case I can think of 2 ways of doing this: 1) A static database that you ship with the app, in which there are predefined users (some of which are flagged as admin), and within the app some functionality is locked (invisible) if the admin flag is off. 2) Two different apps, one for regular and one for admin users. Regarding your second question: yes. You can either lock the access (in other words, the button to access the admin stuff is simply not there for regular users), lock the functionality (the users can see the admin functionality, but it doesn't do anything) or both. The most secure option is both, the easiest to implement is just to lock access. ah thank you this was very helpful. will probably go with the static database + locking access to regular users. yeah we realized how wildly impractical this app was but the whole project is graded on the process of development and simply for learning the development process so apparently it's ok even if the app is stupidly impractical. will give it a go this weekend or so and hopefully I get some sort of functionality that I want | ||
Deleted User 3420
24492 Posts
is this oversimplified example an easy way to create a loop in a function that needs to meet some criteria before stopping? since this will just loop and then when it meets the criteria you can just return whatever it is you want to return ? or are loops typically done this way?
I guess this is a dumb question. It probably just depends on what's better for what you are trying to do. But really I am trying to make sure that nothing breaks in the first one. | ||
Blitzkrieg0
United States13132 Posts
break; Would be the statement you're looking for I think. | ||
Blisse
Canada3710 Posts
On February 08 2017 09:56 Blitzkrieg0 wrote: break; Would be the statement you're looking for I think. Return works as well, though it's also missing a return at the end of the function. On February 08 2017 09:40 dsyxelic wrote: ah thank you this was very helpful. will probably go with the static database + locking access to regular users. yeah we realized how wildly impractical this app was but the whole project is graded on the process of development and simply for learning the development process so apparently it's ok even if the app is stupidly impractical. will give it a go this weekend or so and hopefully I get some sort of functionality that I want Yeah I don't really understand why there is an admin function if it doesn't sync to an server, but if it's just for practice, sure. However there's a potential problem with implementing things in a way that you wouldn't do in a real app, which makes programming decisions strange to debate about. If you're just whitelisting the admin usernames and locally registering the user then any one can become an admin if they figure out the admin usernames. But it's not like being an admin would mean anything. | ||
Deleted User 3420
24492 Posts
On February 08 2017 09:56 Blitzkrieg0 wrote: break; Would be the statement you're looking for I think. so are you saying that instead of just returning the value from within the loop, first break (to take me out of the loop?) and then return the value? Is there a reason to not doing the former? | ||
Blisse
Canada3710 Posts
On February 08 2017 09:59 travis wrote: so are you saying that instead of just returning the value from within the loop, first break (to take me out of the loop?) and then return the value? Is there a reason to not doing the former? It depends on your coding standards and the control flow. + Show Spoiler +
etc etc... all equivalent, whatever fits your scenario, whatever code you like more. | ||
Blitzkrieg0
United States13132 Posts
On February 08 2017 09:59 travis wrote: so are you saying that instead of just returning the value from within the loop, first break (to take me out of the loop?) and then return the value? Is there a reason to not doing the former? I'm saying don't make a new function that is just a loop so you can terminate it with a return value. It's not wrong, but I don't see why you'd want to write code that way. If this is just a small part of a larger example, I would suggest using a break statement if this function has more than one possible return. When you're debugging things it is advantageous to have exactly one place that the function returns. + Show Spoiler [example code] + int fun() { int fun() { Simple example, but when you're debugging something and the logic becomes more complicated I find it easier to debug with the former than the latter. | ||
Shield
Bulgaria4824 Posts
On February 08 2017 10:05 Blitzkrieg0 wrote: I'm saying don't make a new function that is just a loop so you can terminate it with a return value. It's not wrong, but I don't see why you'd want to write code that way. If this is just a small part of a larger example, I would suggest using a break statement if this function has more than one possible return. When you're debugging things it is advantageous to have exactly one place that the function returns. + Show Spoiler [example code] + int fun() { int fun() { Simple example, but when you're debugging something and the logic becomes more complicated I find it easier to debug with the former than the latter. First, you shouldn't use while (1). That's awful. You should #include <stdbool.h> and use while (true). Secondly, you never return error. There's no condition to reach that code in your example. Finally, if you really want to do this properly, you'd usually use a thread with a callback function instead of blocking main thread like that (I assume you're doing that). I also see no problem with this example:
Hard to debug? If you find this hard to debug, you need to change your IDE or learn how to do it. It's super easy to debug in IDE like Visual Studio. | ||
Blitzkrieg0
United States13132 Posts
On February 08 2017 10:53 Shield wrote: First, you shouldn't use while (1). That's awful. You should #include <stdbool.h> and use while (true). Secondly, you never return error. There's no condition to reach that code in your example. Finally, if you really want to do this properly, you'd usually use a thread with a callback function instead of blocking main thread like that (I assume you're doing that). You never return error is precisely why it's an error. I prefer self documenting code instead of adding a comment that says "never gets here" like Blisse, for example ERR_UNREACHABLE. Travis is just learning C so lets assume his application isn't multi-threaded. | ||
Neshapotamus
United States163 Posts
I have seen people write this code in every which way (code at bottom) I listed several of them at the bottom. Anyone have any preference to which version they write? More recently, I prefer to write "fun1" way as this is the easiest to reason about meaning it maps directly into math. You also get other benefits as its easier to parallelize. However, you need a TCO language to execute the statement. The second most intuitive is fun2. Anyone who writes fun5, I just avoid...
| ||
Hanh
146 Posts
because, why would you even loop? But in the more general case, I'd go for
In order of best to worse, I'd rate 2, 4, 3, 5, 1. Btw, these functions aren't doing exactly the same thing. | ||
plasmidghost
Belgium16168 Posts
| ||
WolfintheSheep
Canada14127 Posts
struct Test //creates a structure So first question is, why you are using ampersands here? Which should tell you the exact answer about why this isn't working. + Show Spoiler + Hint: 134615508 is a memory address. | ||
plasmidghost
Belgium16168 Posts
On February 08 2017 14:33 WolfintheSheep wrote: struct Test //creates a structure So first question is, why you are using ampersands here? Which should tell you the exact answer about why this isn't working. + Show Spoiler + Hint: 134615508 is a memory address. Wow, okay, that fix was actually incredibly simple. Whenever I executed it before, I got the error "Segmentation fault (core dumped)" and I saw someone say that putting an ampersand fixed it Now that everything's working fine, I do have a question about this statement:
It currently will work correctly if the user hits the enter key with 10 characters or less inputted, but if they input 11 or more characters and hit Enter, the rest of the code immediately executes, meaning the user can't put in any numbers. Is there a way to fix that? I tried doing a for loop to accept 10 characters, 1 at a time, but it had the problem that if you inputted 4 characters, you had to press enter 6 times to get to the next part of the code, as well as it would take more than 10 characters if you inputted them all at once | ||
Neshapotamus
United States163 Posts
On February 08 2017 13:33 Hanh wrote: Well, in this particular case I'd prefer
because, why would you even loop? But in the more general case, I'd go for
In order of best to worse, I'd rate 2, 4, 3, 5, 1. Btw, these functions aren't doing exactly the same thing. I should have added a little more in the template. I was just making a point that you can loop in multiple ways. From a compiler perspective, yes, they aren't doing the same thing. From an abstraction perspective, they are. Unless you can tell me otherwise? | ||
Hanh
146 Posts
| ||
Blisse
Canada3710 Posts
On February 08 2017 12:45 Neshapotamus wrote: After seeing travis post his simple question. I have seen people write this code in every which way (code at bottom) I listed several of them at the bottom. Anyone have any preference to which version they write? More recently, I prefer to write "fun1" way as this is the easiest to reason about meaning it maps directly into math. You also get other benefits as its easier to parallelize. However, you need a TCO language to execute the statement. The second most intuitive is fun2. Anyone who writes fun5, I just avoid... + Show Spoiler +
fun2 is the only one I would ever conceivably write. The rest are complete no-gos and i'd never accept fun3-5 in a code review when there exists an equivalent form in fun2. It's my anti-while loop bias. | ||
Shield
Bulgaria4824 Posts
On February 08 2017 11:29 Blitzkrieg0 wrote: You never return error is precisely why it's an error. I prefer self documenting code instead of adding a comment that says "never gets here" like Blisse, for example ERR_UNREACHABLE. Travis is just learning C so lets assume his application isn't multi-threaded. Well, in that case it's nice to have assert(false). | ||
| ||