Reputation: 365
How can make it so the program reads any two integers input before the program is run?
I want the output to look like this, with x
and y
being any variables typed in (I am using Cygwin):
$ ./a x y
(product of x and y)
(sum of x and y)
I used int main(int argc, char *argv[])
. I tried to assign argv[2]
to x
and argv[3]
to y
, but when I compile the program it says assignment makes integer from pointer without cast. What does this mean and how do I fix it?
Upvotes: 26
Views: 142206
Reputation: 3257
Assuming that you are using a bash shell, you can use $1, $2, etc for those arguments. If, however, you are using C, your code should look something more like this:
#include <stdio.h>
#include <stdlib.h>
main(int argc, char *argv[]) {
if(argc<=1) {
printf("You did not feed me arguments, I will die now :( ...");
exit(1);
} //otherwise continue on our merry way....
int arg1 = atoi(argv[1]); //argv[0] is the program name
//atoi = ascii to int
//Lets get a-crackin!
}
Hope this helps.
Upvotes: 40
Reputation: 14895
Assuming the C language:
argv
array - argv[1]
, argv[2]
etc.atoi
function.printf
function.[Trying to teach you to fish, rather than providing a fish. Good luck!]
Upvotes: 57
Reputation: 533
Simply using atoi() will convert the char type console input into int
int main(argc, char* argv[])
{
int first = atoi(argv[1]);
printf("%i", first);
}
If you ask why argv[1] instead argv[0], the answer is the first ever argument is the name of your executable file ./some 2 int this case argv[0] will point to 'some'.
Upvotes: 1
Reputation: 2880
My code would look like this.
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
// argc is number of arguments given including a.out in command line
// argv is a list of string containing command line arguments
int total = 0;
int i;
char *value;
for(i = 1; i < argc; i++)
{
// The integers given is read as (char *)
value = argv[i];
printf("Command line index: %d value: %s in ascii: %d\n", i, value, *value);
// Convert ascii to integer.
// atoi function is defined in stdlib.h
total += atoi(value);
}
// .2f limits the decimals to two digits after '.'
printf("Total of given integers is %d\n", total);
}
Upvotes: 4
Reputation: 21
int arg1 = argv[1];
Will not work because it is an array of pointers which holds all the addresses of argv[0]....argv[n]
to get the value of argv[..]
suppose argv[1]
you have to write:
int n=*argv[1]-'0'; // Direct atoi
Upvotes: 2
Reputation: 1
In command line arguments, char*argv[] is string type. We need to convert it into integers. We do this by type casting but in oop we do this by the atoi function(method), it works like typecasting(means method of convert one data type to other)
Upvotes: 0
Reputation: 6122
Firstly, if you run your C program as
./a x y
then a is argv[0], x is argv[1], and y is argv[2], since C arrays are 0 based (i.e. the first item in the array is indexed with 0.
Realize that argv is an array (or I've always thought of it as an ARGument Vector, though you might think of it as an array of ARGument Values) of character string pointers. So, you need to convert the strings to integers. Fortunately, C has library functions to convert ASCII to integer. Look at the stdlib.h documentation.
Good luck!
Upvotes: 5