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

What I want to do is take an integer as input.  How do I declare main?
Would I say:
int main(int argc, int* argv[])???

If so, how then do I copy the argument I passed into a new variable?

Thanks.
jonathanjeffreyAsked:
Who is Participating?
 
evilrixConnect With a Mentor Senior Software Engineer (Avast)Commented:
Re: http:#20848565
Below is a very simple working example.
#include <stdio.h>
#include <stdlib.h>
 
int main(int argc, char * argv[])
{
	if(argc>1)
	{
		int n = atoi(argv[1]);
		printf("%d was passed.\n", n);
	}
	else
	{
		puts("Not enough parameters!");
	}
 
	return 0;
}

Open in new window

0
 
evilrixSenior Software Engineer (Avast)Commented:
Your declaration of main is correct. The numeric value will be passed into main in a stringized format as one of the values of argv. Assuming you pass it in as your first command line parameter it'll be in argv[1]. You can then use atoi() to convert it to an integer value.

Something like...
int n = atoi(argv[1]);

http://www.cplusplus.com/reference/clibrary/cstdlib/atoi.html
0
 
ozoCommented:
int new;
if( argc > 1 ){
  new= atoi(argv[1]);
}
0
 
lucky_jamesCommented:
i doubt if you can change the signature of main method as:
int main(int argc, int* argv[])

what you can do is to convert char* into integers. this you can do using atoi method.

check out:
http://www.cplusplus.com/reference/clibrary/cstdlib/atoi.html

Hope it helps.
0
All Courses

From novice to tech pro — start learning today.