Link to home
Start Free TrialLog in
Avatar of jonathanjeffrey
jonathanjeffrey

asked on

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.
Avatar of evilrix
evilrix
Flag of United Kingdom of Great Britain and Northern Ireland image

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
int new;
if( argc > 1 ){
  new= atoi(argv[1]);
}
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.
ASKER CERTIFIED SOLUTION
Avatar of evilrix
evilrix
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial