Handle negative numbers for error message
This commit is contained in:
parent
830f860e02
commit
4053efeaef
27
ex00/main.c
27
ex00/main.c
|
@ -6,7 +6,7 @@
|
||||||
/* By: jtorrez- <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: jtorrez- <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2023/03/18 20:15:26 by jtorrez- #+# #+# */
|
/* Created: 2023/03/18 20:15:26 by jtorrez- #+# #+# */
|
||||||
/* Updated: 2023/03/19 18:18:18 by tischmid ### ########.fr */
|
/* Updated: 2023/03/19 19:07:32 by tischmid ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
@ -19,15 +19,15 @@ void ft_putstr(char *str);
|
||||||
// Parses the program arguments and fails if the number of passed
|
// Parses the program arguments and fails if the number of passed
|
||||||
// arguments is not 2 or 0.
|
// arguments is not 2 or 0.
|
||||||
// Also fails if the passed arguments are out of
|
// Also fails if the passed arguments are out of
|
||||||
// bounds (200 for width, 100 for height).
|
// bounds (0-200 for width, 0-100 for height).
|
||||||
// argv[0] contains the path of the executable, eg ./rush00
|
// argv[0] contains the path of the executable, eg ./rush00
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
int width;
|
int width;
|
||||||
int height;
|
int height;
|
||||||
|
|
||||||
width = 5;
|
width = 10;
|
||||||
height = 3;
|
height = 5;
|
||||||
if (argc != 1 && argc != 3)
|
if (argc != 1 && argc != 3)
|
||||||
{
|
{
|
||||||
ft_putstr("Usage: ");
|
ft_putstr("Usage: ");
|
||||||
|
@ -40,26 +40,33 @@ int main(int argc, char *argv[])
|
||||||
width = ft_atoi(argv[1]);
|
width = ft_atoi(argv[1]);
|
||||||
height = ft_atoi(argv[2]);
|
height = ft_atoi(argv[2]);
|
||||||
}
|
}
|
||||||
if (width >= 200)
|
if (width < 0 || width > 200)
|
||||||
ft_putstr("Width must be less than 200\n");
|
ft_putstr("Width must be in the range of 0 to 200\n");
|
||||||
else if (height >= 100)
|
else if (height < 0 || height > 100)
|
||||||
ft_putstr("Height must be less than 100\n");
|
ft_putstr("Height must be in the range of 0 to 100\n");
|
||||||
else
|
else
|
||||||
rush(width, height);
|
rush(width, height);
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Convert a string to an integer
|
// Convert a string to a non-zero integer
|
||||||
int ft_atoi(char *str)
|
int ft_atoi(char *str)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
|
int sign;
|
||||||
int number;
|
int number;
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
number = 0;
|
number = 0;
|
||||||
|
sign = 1;
|
||||||
|
if (str[i] == '-')
|
||||||
|
{
|
||||||
|
sign = -1;
|
||||||
|
++i;
|
||||||
|
}
|
||||||
while (str[i] >= '0' && str[i] <= '9')
|
while (str[i] >= '0' && str[i] <= '9')
|
||||||
number = number * 10 + (str[i++] - '0');
|
number = number * 10 + (str[i++] - '0');
|
||||||
return (number);
|
return (sign * number);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Display a null-terminated string on standard output.
|
// Display a null-terminated string on standard output.
|
||||||
|
|
Loading…
Reference in New Issue