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;
}
#include <algorithm> //Lets us use max and min
#include <cmath> //Lets us use the absolute value function
#include <iostream>
usingnamespace 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)
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
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.