Thread: If/else does not work as expected

  1. #1
    programmer hacker DevZero's Avatar
    Join Date
    Jul 2017
    Posts
    42

    If/else does not work as expected

    My code:
    Code:
    for(int i=1; i<argc; i++)
    	{
    		if( (strcmp(argv[i], "--help"))==0 )
               printHelp();
    		if(argv[i][0] == '-')
    		{
    		    for(int b=1; b<strlen(argv[i]); b++)
    		    {
    			    if(argv[i][b] == 'd')
    			        key_d=1;
    		        else if(argv[i][b] == 'c')
    		            key_c=1;
    	            else if(argv[i][b] == 'm')
    	                key_m=1;
    	            else if(argv[i][b] == 't')
    	                key_t=1;
    	            else if(argv[i][b] == 's')
    	                key_s=1;
                    else if(argv[i][b] == 'h')
                        printHelp();
                    else
                    {
                    	printf("%c: invalid key. Use-h or --help to get help", argv[i][b]);
                        _exit(0);
                    }
    			}
    		}
    I enter test -r but I do not see the help that should be output, in the printHelp method I have the help text written about the program!
    The text should be displayed after the else but it is not output! Maybe I do not know something yet?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    > I enter test -r but I do not see the help that should be output
    If you called your program 'test' and you're on a Unix/Linux system, you need to know that 'test' is also a built-in command of some shells.

    test -r
    Invokes the built-in command, and does nothing except set $?

    ./test -r
    Invokes your program called test (in the current directory)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Why would you think -r would output help? Your code looks for -h or --help.

  4. #4
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    956
    The printf() before _exit(0) doesn't end with a newline, and the output may be line-buffered. _exit() doesn't flush I/O buffers, so the printf output is probably lost when the program terminates. I suggest two things: put a newline at the end of your printf string, and call exit(0) instead of _exit(0).

  5. #5
    Registered User
    Join Date
    Apr 2017
    Location
    Quetzaltenango
    Posts
    82
    It is the newline like christop said. I see another problem though. The first if statement with the strcmp will print your help, but then it will be followed by the message "-: invalid key..."

  6. #6
    programmer hacker DevZero's Avatar
    Join Date
    Jul 2017
    Posts
    42
    I was too hasty and forgot to write ./ at the beginning of the team. I ran my team which I compiled and now I understand what the problem is and fixed it

  7. #7
    programmer hacker DevZero's Avatar
    Join Date
    Jul 2017
    Posts
    42
    No, my program produces an error

  8. #8
    programmer hacker DevZero's Avatar
    Join Date
    Jul 2017
    Posts
    42
    Quote Originally Posted by Salem View Post
    > I enter test -r but I do not see the help that should be output
    If you called your program 'test' and you're on a Unix/Linux system, you need to know that 'test' is also a built-in command of some shells.

    test -r
    Invokes the built-in command, and does nothing except set $?

    ./test -r
    Invokes your program called test (in the current directory)
    Quote Originally Posted by algorism View Post
    Why would you think -r would output help? Your code looks for -h or --help.
    Quote Originally Posted by christophergray View Post
    It is the newline like christop said. I see another problem though. The first if statement with the strcmp will print your help, but then it will be followed by the message "-: invalid key..."
    No, my program after the output of the message goes out, because the output function is specified in the function printHelp

  9. #9
    programmer hacker DevZero's Avatar
    Join Date
    Jul 2017
    Posts
    42
    Quote Originally Posted by christop View Post
    The printf() before _exit(0) doesn't end with a newline, and the output may be line-buffered. _exit() doesn't flush I/O buffers, so the printf output is probably lost when the program terminates. I suggest two things: put a newline at the end of your printf string, and call exit(0) instead of _exit(0).

    Yes it works fine, the problem is solved

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-16-2016, 11:48 PM
  2. My First programme will not work as expected
    By Macattack in forum C Programming
    Replies: 3
    Last Post: 02-06-2014, 08:26 PM
  3. One pointer that does not work as expected
    By h3ro in forum C++ Programming
    Replies: 13
    Last Post: 07-16-2008, 07:58 AM
  4. Why this do-while loop doesn't work as I expected?
    By Nutka in forum C Programming
    Replies: 4
    Last Post: 10-25-2002, 09:47 AM

Tags for this Thread