So I'm a C++11 newbie and I'm having some difficulties with manipulating the number of params of an argument when passed to a function. Let's say I have some basic code like this
#include <iostream>
#include <cstdlib>
#include <math.h>
long power(long a, long b)
{
long c= pow(a, b);
return c;
}
usingnamespace std;
int main(int argc, char *argv[]) {
if (argc > 2) {
cerr << "Error: Invalid number of parameters!" <<endl;
cout << "What value in power is (20, 30) = " << power (20, 30) << endl;
return EXIT_SUCCESS;
}
Playing around with this, I will like to handle the effects of how the nature in which arguments are passed results in program behaviour.
1 2
if (argc > 2) {
cerr << "Error: Invalid number of parameters!" <<endl;
The intention of the above snippet is to throw an error when the number of arguments are greater than two however this doesn't work for me. Also I will like to know how to handle the arguments such that if only one parameter is passed to this program via the command line then it will treat it will read it in as an external file and then display the content of that file. Also if I add another function, e.g. a sqrt function from the math library then is it possible that it could display the results of the two functions if I decide not to specify any of the functions from the command line?
1 2 3 4
squareRoot (long a, long b) {
long c = sqrt(a, b);
return c;
}
So with this snippet of code added to the previous one, is it possible to do just call from the command line "program 20 30" and get both the squareRoot and power function results be printed out (I know with "program 20 30 power" the power function result will be printed out and same for squareRot function). Also if I do something like "program cppismoredifficultthanruby.txt" from the command line, it will print out the data contained in "cppismoredifficultthanruby.txt"
The intention of the above snippet is to throw an error when the number of arguments are greater than two however this doesn't work for me.
How are you running your program and passing the arguments? Are you using an IDE or are you running your program from a console/command line?
I will like to know how to handle the arguments such that if only one parameter is passed to this program via the command line then it will treat it will read it in as an external file and then display the content of that file.
I'm actually trying in it out both in Dev C++ IDE and via the command line however the end goal is definitely to run it via the command line. Thanks for the explanation anyways, for some reason I'm quite positive about C++ and the advantages of converting. You can view what I'm currently doing and how I'm going about it on http://goo.gl/S3IlVP if you'd like. It's nothing complex but handling these use cases in situations like this gives me a better understanding of the general flow of the language. My codes are always commented anyways so it'll be quite easy for you to understand what use cases I'm trying to handle and I'm keeping it as basic as possible so it's not like some complicated Ruby thingy :)
Anyways could you expatiate on the switch cases two and three? As much as I love codes I'd like some basic explanation of it flow also, that's if you do not mind anyways. Thanks for the answer all the same
The IDE will have some place where you can set command line parameters. When you run the program it will apply them to the program each time.
If you have a program called test, and you run it with 2 arguments, the program will see three arguments. For example, if the program was run with 2 3: