take argument from command line

EDIT: I have made it get my 2 numbers, but now it will not do any of the calculations. Updated code.
I have a project that I cannot finish because I do not know how to implement the argv function into it. I have been reading about it for hours and I understand what it does. I just don't know how to make it so that it takes an argument from the command line. Using linux via Putty, my project is called assign1 . So I'm supposed to call it ./assign1 # # and then enter two numbers directly from the command line. It should run the program without asking the user to enter the two numbers, but idk exactly how to code that part. Here is my VS code:

#include <iostream>
#include <cmath> //Lets us use the absolute value function
#include <algorithm> //Lets us use max and min
using namespace std;

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

for (int i = 0; i < argc; i++) //these next three lines i got from online, but it does not do
{ //what i need it to do.
cout << argv[i];
}
//all this is my actual project and is fine.
double a=0, b=0, sum, diff, product, avg, absval;
cout << "Enter two numbers: ";
cin >> a;
cin >> b;
sum = a + b; //adds a and b
diff = a - b; //subtracts a and b
product = a * b; //multiplies a and b
avg = a / b; //gets the average of a and b
absval = abs(diff); //will get the difference from above, and make it an absolute value, or distance
cout << "sum:" << sum << " diff:" << diff << " prod:" << product << " avg:" << avg << " absvalue:" << absval << " max:" << std::max(a, b) << " min:" << std::min(a, b);
return 0;
}
Last edited on
your code indented
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <algorithm> //Lets us use max and min
#include <cmath>     //Lets us use the absolute value function
#include <iostream>
using namespace std;

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

	// these next three lines i got from online, but it does not do what i need
	// it to do.
	for(int i = 0; i < argc; i++)
	{
		cout << argv[i];
	}
	// all this is my actual project and is fine.
	double a = 0, b = 0, sum, diff, product, avg, absval;
	cout << "Enter two numbers: ";
	cin >> a;
	cin >> b;
	sum = a + b;        // adds a and b
	diff = a - b;       // subtracts a and b
	product = a * b;    // multiplies a and b
	avg = a / b;        // gets the average of a and b
	absval = abs(diff); // will get the difference from above, and make it an
	                    // absolute value, or distance
	cout << "sum:" << sum << " diff:" << diff << " prod:" << product
	     << " avg:" << avg << " absvalue:" << absval
	     << " max:" << std::max(a, b) << " min:" << std::min(a, b);
	return 0;
}


> these next three lines i got from online, but it does not do what i need it to do.
of course not, ¿do you realise what those lines do?
at least, you should learn how the arguments are passed

1
2
3
argv[0] //program name
argv[1] //first argument
argv[2] //second argument 

each one is of type char *, a c-string (null terminated)

to convert them to a number check
http://www.cplusplus.com/reference/string/stod/
http://www.cplusplus.com/reference/cstdlib/atof/
http://www.cplusplus.com/reference/cstdlib/strtod/
I should've clarified a little better. I do know how the arguments are passed. I just don't know how to properly code it so it can work properly. Maybe I don't fully understand... I can pass them and make them come up in my console, but they do not do anything. I have to manually enter those two numbers again in order for the code to run. ex: ./assign1 10 5 should automatically add, subtract, multiply, divide. But it will not, I have to manually enter 10 5 for the calculations to run.
1
2
3
4
5
6
int main(int argc, char* argv[]) {

	for(int i = 0; i < argc; i++)
	{
		cout << argv[i];
	}


When ran, it will come up as: ./assign1105 and thats its. I have to put in 10 5 for it to run properly
EllMickey wrote:
When ran, it will come up as: ./assign1105 and thats its.


Isn't that what you would expect?

It outputs ./assign1
and then without a further space or line break 10
and then without a further space or line break 5

If you want new lines put
cout << argv[i] << '\n';

For running your calculator (as opposed to checking your command-line reading) this is irrelevant. It is clearly picking up (c-string versions of) those numbers correctly. Use the links to functions atof etc. that @ne555 gave you to turn them from c-strings to numerical variables.
Last edited on
Topic archived. No new replies allowed.