[comp.sys.amiga] Compairing C values

rat@hotcity.UUCP (P W) (02/05/91)

        I have + as an input arguement via *argv[] and whenever I try to
compare its value it comes out as a 0.  For instance...  If I compile a
program like this:

#include <stdio.h>
#include <math.h>

void main(int argc, char *argv[])

{

        And I type "test +" (I would name the file test) it would just print a
equals 0.  Any help would be appereciated...
char a = argv[1];
if (a = '+') printf("a equals +");
if (a = '0') printf("a equals '0'");
if (a = 0) printf("a equals 0");
}

dueker@xenon.arc.nasa.gov (Chris Dueker) (02/06/91)

In article <rat.3380@hotcity.UUCP>, rat@hotcity.UUCP (P W) writes...
> 
>        I have + as an input arguement via *argv[] and whenever I try to
>compare its value it comes out as a 0.  For instance...  If I compile a
>program like this:
> 
>#include <stdio.h>
>#include <math.h>
> 
>void main(int argc, char *argv[])
> 
>{
> 
>        And I type "test +" (I would name the file test) it would just print a
>equals 0.  Any help would be appereciated...
>char a = argv[1];
>if (a = '+') printf("a equals +");
>if (a = '0') printf("a equals '0'");
>if (a = 0) printf("a equals 0");
>}

Umm, if it's not a typo above, you should use "==" instead of "=" in
the if stmts above.  The "=" assigns the character to variable a, then
if the result is true, the printf stmt is executed.

Chris
P.S.  I'd have expected output from the first two printf's, since those
      results are non-zero (meaning true?).
------------------------------------------------------------------------
"Ah, Benson, you are so mercifully free of the ravages of intellegence!"
"Oh, thank you, Master!"             - from the movie, TIME BANDITS
------------------------------------------------------------------------
dueker@xenon.arc.nasa.gov        |   Chris Dueker (The Code Slinger)
duke@well.sf.ca.us               |   Mtn. View, CA  (Sillycon Valley!)

wille@frith.uucp (Jeffrey Wille) (02/06/91)

Three things: First, argv[i] is a pointer to a char, not a char.  So, you should
be looking at *argv[i].  Second, you are using the assignment operator '=' in
your 'if' statement, not the comparison one '=='.  When you say,  if (a = 0) 
the value of the statement is the value being assigned, i.e., zero.  Third,
the CLI uses + at the end of a command to signify that it is a continuation,
so 'test +' is going to wait for you to type more stuff, and the '+' will
be absorbed.  Try:


#include "workbench/startup.h"

main(int argc, char *argv[]) {
   int a;
   if ((a = *argv[1]) == '&')
     printf("Yes it's an '&'\n");
}

Hope that helps.
  _ _   ---------------------------------------------------------------------- 
 (   )                                     Jeff Wille (wille@frith.egr.msu.edu)
  (   )      _(_      ^^^^   _^-^_           [KF8HZ]  (wille@happy.egr.msu.edu)
   (   )    (   )    /   /   \   /  _^^_   
    ( @@)    00 M    @ @/    <@@ >  >oo<     "Never say anything unless you're
     (_^>   (^)/    ( ^ )     o /    @        sure everyone feels exactly the 
    Marge   Homer    Bart    Lisa  Maggie     same way." -- Homer Simpson

dsg@cci632.UUCP (David Greenberg) (02/06/91)

>	I have + as an input arguement via *argv[] and whenever I try to
>compare its value it comes out as a 0.  For instance...  If I compile a
>program like this:
>
>#include <stdio.h>
>#include <math.h>
>
>void main(int argc, char *argv[])
>
>{
>
>        And I type "test +" (I would name the file test) it would just print a
>equals 0.  Any help would be appereciated...
>char a = argv[1];
>if (a = '+') printf("a equals +");
>if (a = '0') printf("a equals '0'");
>if (a = 0) printf("a equals 0");
>}

	I think you want to use a double '=' here ie : (a='+' sets a equal
		to plus) a=='+' tests for equality)

if (a == '+') printf("a equals +\n");
if (a == '0') printf("a equals '0'\n");
if (a == 0) printf("a equals 0\n");  /* don't know what you are doing here?? */

(Also, use \n (newlines) to output the line. C uses buffered output and will not
print until a newline (\n) is encountered (or an fflush(stdout) is done).

	OR you could use a switch statement:



	switch (a) {

case '+':	printf("a equals +\n");
		break;
case '0':	printf("a equals '0'\n");
		break;
default:	printf("Neither + or '0'\n");

	}


	Hope this is what you need..

						Dave

	

scott@erick.gac.edu (Scott Hess) (02/06/91)

In article <1991Feb5.173012.20398@nas.nasa.gov> dueker@xenon.arc.nasa.gov (Chris Dueker) writes:
   In article <rat.3380@hotcity.UUCP>, rat@hotcity.UUCP (P W) writes...
   >char a = argv[1];
   >if (a = '+') printf("a equals +");
   >if (a = '0') printf("a equals '0'");
   >if (a = 0) printf("a equals 0");
   >}

   Umm, if it's not a typo above, you should use "==" instead of "=" in
   the if stmts above.  The "=" assigns the character to variable a, then
   if the result is true, the printf stmt is executed.

You missed the most important part (among many) : a=argv[ 1]?  Note that
argv[ 1] is a 'char *', rather than a char.

Of course, no one's going to see this anyway!
--
scott hess                      scott@gac.edu
Independent NeXT Developer	GAC Undergrad
<I still speak for nobody>
"Tried anarchy, once.  Found it had too many constraints . . ."
"Buy `Sweat 'n wit '2 Live Crew'`, a new weight loss program by
Richard Simmons . . ."