C Programming: How should I organize my code? I want to create a log-in screen that accepts a set of username and password and determines if the user has admin privileges or not. I've got the algorithm down, but how should I organize my code? Should I lump them all in 1 function or split them into 3 functions? If I split them, how should I deal with variables that need to be present in 2 of those functions? (aside from using global variables)
Generally speaking, modularity makes a better and clearer code. I would most of the times opt for 3 functions instead of 1. Also, you can pass the variables by reference, and if they are not modified inside a function, use the const modifier, i.e.: void ChangeSomething(int *x); void DoNotChangeSomething(const int *x);
There is some overhead in creating the function with pointers, and so on, see if it really is worth it instead of using one or two variables with file scope.
I'm afraid I don't understand. Say I have 2 functions, GetUsername and GetPassword. In GetUsername, I got the username. How do I pass this info to the other function?
Do you know about references in C? If you do, you should do something like this (assuming you passed it as a argument): char *GetUsername(some arguments) { char *Username; /* or char Username[N] */ /* Implementation depended */ return Username; } and char *GetPassword(char *username, more arguments) { char *password; /* or char password[M]; */ /* Implementation depended */ return password; }
My GetUsername function (or procedure) looks like this: void GetUsername() { char InUser[31]; int Done; while (!Done) { printf(" Username » "); scanf("%s", InUser); if (GetUserNum(InUser) == CA_UsersStructCount) { printf(" [Error!] Username is not in the database.\n\n"); continue; } break; } } Since it's just a snippet of my code and some variables are global, I'll give the gist of the code. So the process in encased in a loop. It asks for the username and stores it in InUser. Then it checks if the username is in the database. Now how do I pass InUser to GetPassword? Cause if I just put "return InUser;" , calling the function will just keep asking the user for the username.
It may be a problem with scanf. Scanf is a pretty bad function for getting string input. Consider other string I/O functions. Also, read http://www.gidnetwork.com/b-59.html It works (more or less) with the gets() function (avoid it also! It is very unsafe) and the prototype such as: void GetUsername(char UserName[31])
Just a question, why are you breaking the while loop? Isn't it better to just modify the Done value? First thing is that Done appears to be unitialized. How can you be sure of its value? Also, just modify it inside the loop, such as int Done = 0; and then, inside the loop, just do Done = 1; It's a lot more readable and just better code :-)
About the Done value: I'm not exactly done with this one yet, so there are some kinks I've yet to iron out. Like the one you mentioned. I'll change it accordingly.
Hold on, I still don't understand how to pass InUser to the next functions. Can you elaborate?
I will write a snippet for you, hold on.
http://codepad.org/nE9pEsE9 This is more or less what I meant. It's just a snippet, there are several other stuff that you can add, but it should give you a rough idea.
Just a tip: safer is to use the Password function as (const char *Username, char *Pass), because you don't want to modify the user name inside a Password call.
I finally understand it. This is passing parameters by pointers, yeah?
But there's one thing I don't fully understand. Why use pointers for the parameters of the function? For the record I'm not so knowledgable when it comes to subprograms.
The short answer is that you are passing a memory address (that is what a pointer is) to the called function, so it can access the memory location of the given pointer and change the value stored there. Another thing is that char * is C is - almost - always equivalent to a string, since the abstraction used by C is that strings are just character arrays with the last entry set to the special char '\0'. If you need a longer answer, be sure to look into other sources about passing parameters by address. I will try to write a more complete answer in the weekend, but until there I am just too busy. Sorry, mate.
Join our real-time social learning platform and learn together with your friends!