Programmer :
Description : A program which checking number of arguments that are passed
from the command-line. The program itself will determine
what kind of argument that has been passed by the user, whether
a number, a palindrome, a word

if((a%2.0)==0.0)
puts("The number %f is a real number that is even\n",a);
else
puts("The number %f is a real number that is odd\n",a);
return 1;
}

int check_int(char argv[])
{
int b = 0;
b = atoi(argv);
if((b%2)==0)
puts("The number %d is a integer number that is even\n",a);
else
puts("The number %d is a integer number that is odd\n",a);
return 1;

}

int check_alpha(char argv[])
{
puts("%s\n", argv);
return 1;
}

09-26-2001

QuestionC

Umm, I can't rewrite this, but I can go through it line by line...

Code:

// I suggest not using this macro, as there is a library
// function puts.
#define puts printf

for(i=1;i<argc;i++)
{
// isdigit and ispunct operate on ints, not strings.
// so first off, argv[i] is a char *, not an int. You should try
// isdigit(*argv[i]), so you can pass it a char, (which
// will work as an int)
// Second, since these are for comparing characters,
// you'll never find a char that's both a digit and a punct.

// isdigit (*argv[i]) && ispunct (*argv[i])
if(isdigit(argv[i])&&ispunct(argv[i]))
// So this function will never be called.
c_numb+=check_float(argv[i]);

// isdigit (*argv[i])
else if(isdigit(argv[i]))
c_numb+=check_int(argv[i]);
}
// I don't see the reason in using 2 for loops that do the
// same conditions like this.
for(i=1;i<argc;i++)
{
// isalpha(*argv[i])
if(isalpha(argv[i]))
c_word+=check_alpha(argv[i]);
}

// The floats may be stored with round off errors so
// that this is not accurate...
if((a%2.0)==0.0)
puts("The number %f is a real number that is even\n",a);
else
puts("The number %f is a real number that is odd\n",a);
return 1;
}

int check_int(char argv[])
{
int b = 0;
b = atoi(argv);
if((b%2)==0)
// You're passing a, but you need to pass b. That's why this
// function won't work.
puts("The number %d is a integer number that is even\n",a);
else
puts("The number %d is a integer number that is odd\n",a);
return 1;
}

int check_alpha(char argv[])
{
puts("%s\n", argv);
return 1;
}

The only thing I'm not ure about is the isdigit(), ispunct() stuff, since you're using C++, those may operate differently, but as it is in C, those will only check the first character, and you have to pass them an int or a char, not a char *.