質問

I'm having some issues using the atoi(s) function. I'm trying to convert a command line argument into an integer, but the integers I am receiving from the atoi function are acting strange. I have checked the variables holding the conversion and they hold the correct integers, but when they run through my program they do not work as they should. The program accepts three arguments; the program itself, the function number(1-3), and an arbitrary integer. For example, the command line instruction would look like lab4p2 3 10.

Functions:

#include <stdio.h>
#include <stdlib.h>


//---------------------------------------------------------------------------
// Functions

// Sumation function adds all integers from 1 to the value.
int sumation(int x)
{
  int counter = 1;
  int value;
  while (counter < (x+1))
    {
      value += counter;
      counter++;
    }
  return value;
}

// Negation function returns the negation of the given value.
int negation(int x)
{
  int negator, value;

  negator = (x*2);
  value = (x - negator);
  return value;
}

// Square function returns the square of the input value.
int square(int x)
{
  int value;
  value = (x*x);
  return value;
}

//---------------------------------------------------------------------------

Main:

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

  int inputval, functionval, value;
  double doubleval;

  functionval = atoi(argv[1]);

  inputval = atoi(argv[2]);
  doubleval = atof(argv[2]);

  printf("%i", functionval);
  printf("%i", inputval);

  if(argc != 3)
    {
      printf("%s", "Invalid amount of arguments! 3 expected. \n");
    }
  else if ((functionval < 1)||(functionval > 3))
    {
      printf("%s", "Invalid function value. Expected values: 1-3 \n");
    }
  else if (inputval != doubleval)
    {
      printf("%s", "Invalid value! Integer expected. \n");
    }
  else if (functionval = 1)
    {
      value = sumation(inputval);

      printf("%s", "The sum from 1 to the input value = ");
      printf("%d", value);
    }
  else if (functionval = 2)
    {
      value = negation(inputval);

      printf("%s", "%d", "The negation of the input value = ", value);
    }
  else if (functionval = 3)
    {
      value = square(inputval);

      printf("%s", "%d", "The square of the input value = ", value);
    }
  else
    {
      printf("%s", "Something is wrong!");
    }

}

All of the error checks work correctly, but functions 2 and 3 are never accessed (despite input) and function 1 displays an incorrect answer. Does anyone know what the issue may be?

Thanks, Matt

役に立ちましたか?

解決

functionval = 1    assignment

functionval == 1   comparison

By the way, there's a typo when you get doubleval, too Now that I see that using argv[2] again with a different conversion function was deliberate, rather than a typo for argv[3], I'll instead suggest that if you care about conversion errors, you use something like:

errno = 0;
char* endptr;
long value = strtol(argv[2], &endptr, 10);
if (errno == ERANGE) {
  // Value was too big or too negative
} else if (endptr == argv[2] || errno == EINVAL) {
  // Not a number
} else if (endptr[0] != '\0') {
  // trailing characters.
}
// Value is OK.

which, if you use it a lot, is worth wrapping into something easier to call.

他のヒント

else if (functionval = 1)
else if (functionval = 2)
else if (functionval = 3)

should be

else if (functionval == 1)
else if (functionval == 2)
else if (functionval == 3)

otherwise you perform an assignment not a comparison.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top