[SOLVED] Stuck on a c problem. Things happening out of order.
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Stuck on a c problem. Things happening out of order.
Code:
char * scan_data(char * str)
{
// declare & initialize
char temp[64];
clear_stdout();
// prompt for name of new account holder
if (strcmp(str, "name")) {
printf("enter name of new account holder --> ");
fgets(temp, 64, stdin);
no_more_newline(temp);
}
// prompt for deposit value
if (strcmp(str, "deposit")) {
printf("enter amount of deposit --> ");
scanf("%s", temp);
}
return(validate_data(str, temp));
}
I'm calling these from a do while loop.
Code:
// declare and initialize
char * name = NULL;
char * deposit = NULL;
do {
name = scan_data("name");
} while (name == NULL);
do {
deposit = scan_data("deposit");
} while (deposit == NULL);
The problem is that for some reason it fires the deposit option first. I can't figure why that is?
If I reverse which one is first in the do while loops it works correctly.
Last edited by jmgibson1981; 04-07-2023 at 05:42 PM.
Also, you are sending those prompt strings to stdout, which is line-buffered by default, and you would need to call "fflush(stdout)" to force the unterminated line to be displayed. Prompt strings are more commonly sent to stderr, which is less likely to be redirected away from the terminal, and which is, by default, unbuffered.
Very long ago in a "C" project I finally got tired of this nonsense and created a one-liner str_is() function ... "Boom. That's what I mean."
I went through the project – which as usual I did not originate – identified repetitive sections of logic (any one of which, in "C", could be "not-obviously tpyo'd...") and spun them off into "what I mean is ..." functions. (Along the way, I found quite a few bugs that hadn't been discovered yet.)
Last edited by sundialsvcs; 04-11-2023 at 08:06 PM.
RETURN VALUE
The strcmp() and strncmp() functions return an integer less than, equal to, or greater than zero if s1 (or the first n bytes thereof) is found, respectively, to be less than, to match, or be greater than s2.
This is one reason why I have come to prefer [compiled ...] languages that are "higher level" than good ol' "C." Yes, "it's a damned sight better than assembler language, but not by much." You can very(!) easily "typo" something, and it still compiles. You are constantly obliged to write source code to suit the language, not to express your intent.
Fortunately, we now have well-developed "compiler suites," such as the immortal gcc family, which now give you many ways to "express yourself on the front end," and they will all result in an efficient program file produced by the same "back end" technology. No matter how you choose to write it, it all comes out very much the same and it all runs very fast.
Of course, interpreters have also come a very long way. Internally, they're based on the same "compiler" technology but they "do it on the fly." And they're easily fast enough now to be useful.
Last edited by sundialsvcs; 04-12-2023 at 09:49 AM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.