LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-07-2023, 05:41 PM   #1
jmgibson1981
Senior Member
 
Registered: Jun 2015
Location: Tucson, AZ USA
Distribution: Debian
Posts: 1,140

Rep: Reputation: 392Reputation: 392Reputation: 392Reputation: 392
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.
 
Old 04-07-2023, 06:17 PM   #2
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,263
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
I think your string comparison tests are the reverse sense they should be.

As shown in your example those will test true when they are not equal.

I suspect that what you intend is like this (changes shown in red):

Code:
  // prompt for name of new account holder
  if (strcmp(str, "name")==0) {
   ...
  }

  // prompt for deposit value
  if (strcmp(str, "deposit")==0) {
   ...
  }
 
2 members found this post helpful.
Old 04-07-2023, 06:24 PM   #3
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,777

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
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.
 
2 members found this post helpful.
Old 04-07-2023, 06:36 PM   #4
jmgibson1981
Senior Member
 
Registered: Jun 2015
Location: Tucson, AZ USA
Distribution: Debian
Posts: 1,140

Original Poster
Rep: Reputation: 392Reputation: 392Reputation: 392Reputation: 392
Thank you both.

Changing strcmp to test == 0 worked.

I will look into redirecting to stderr.
 
Old 04-11-2023, 07:57 PM   #5
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939
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.
 
Old 04-12-2023, 05:19 AM   #6
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,836

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
I guess reading the man page can help:
Quote:
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.
not that difficult, and works in most cases.
 
Old 04-12-2023, 09:45 AM   #7
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Logi Sales Manager on Ncurses (invoice, invoicing, orders, order, sale order, sales order...)? Xeratul Linux - Software 0 03-25-2017 02:45 PM
LXer: Strange things happening with my OpenBSD box, but excellent documentation saves LXer Syndicated Linux News 0 02-29-2008 02:10 PM
Wierd Things Happening During Install mattp Slackware 17 11-16-2005 07:31 AM
wired things happening Paxmaster Linux - Software 5 10-24-2004 05:46 PM
Weird things are happening saiz66 Slackware 8 05-17-2003 12:03 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:48 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration