I am unable to print out the error message when there is no input. I've tried different methods for it, but I can't get it to work. What am I missing?
print should be:
p2
invalid input

but I get:
p2
segmentation fault

when I do this with an int or string, the code works.

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

#define TRUE 1     //Declare constants
#define FALSE 0

unsigned const int num;

	
int getPrime(int *, int Prime);
int testValid(int alpha);
int filling(int beta);
float isPrime(float Value);


int main(int argc, char * argv[])
{
	int atoi( const char *str );
	int Toi;
	Toi = atoi(argv[1]);
	printf("Program #2");
	testValid(Toi);
	return 0;
}

int testValid(int delta)
{

	if((delta > 10000) || (delta < 2))
		{
		inVal();
		}
	else if(delta <= 2)
		{
		printf("%d\n", delta);
		exit(EXIT_SUCCESS);
		}
	else
		{
		filling(delta);
		}	
	return(EXIT_SUCCESS);
}

int inVal()
{
	printf("Invalid Input.\n");
	exit(EXIT_FAILURE);
	return 0;
}

int filling(int zeta)
{
	int a[zeta];
	int index;
	printf("\nPrime numbers in the range 2..%d are: \n", zeta);
	for(index = 2; index < zeta; index++)
		a[index+1] = TRUE;
	getPrime(a, zeta);
	return(EXIT_SUCCESS);
}
	
int getPrime(int *arr, int P)
{
	int i, j;
	printf("2 ");
	for(i = 2; i <= P; i++){
		for(j = i*2; j <= P; j+=i)
		{
			arr[j] = FALSE;
		}
		
		if(arr[i] == TRUE)
		printf("%d ", i);
	}
	printf("\n");
	return(EXIT_SUCCESS);
}

argc is there for a reason, use it to determine if argv[1] even exists.

I've tried adding these in at separate times, was I wrong in doing so?

I know these are stupid questions but bare with me, I only learned C 2 weeks ago.

if(argv[1] != argc)

if(argv[1] != NULL)

if(argv[1] <argc)

never mind got it

int main(int argc, char * argv[])
{
	if(argc > 2)
	{
	int atoi( const char *str );
	int Toi;
	Toi = atoi(argv[1]);
	printf("Program #2, masc0385, Mike Truong\n");
	testValid(Toi);
	}
	else
		inVal();
	return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.