How to handle argument params in C++

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream> 
#include <cstdlib>
#include <math.h>

long power(long a, long b)
{
     long c= pow(a, b);
     return c;
}


using namespace 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"
Last edited on
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.
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
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include <fstream>
#include <math.h>
#include <stdlib.h>

long power(long a, long b)
{
	return pow(a, b);
}

int main(int argc, char *argv[])
{
	if (argc > 3 || argc == 1)
	{
		std::cerr << "Error: Invalid number of parameters!\n";
		return 1;
	}

	switch (argc)
	{
	case 2:
		{
			std::ifstream is(argv[1]);
			if (!is)
			{
				std::cerr << "Cannot read: " << arg[1] << "\n";
				return 2;
			}
			std::string line;
			while (std::getline(is, line))
				std::cout << line << "\n";
		}
		break;

	case 3:
		std::cout
			<< "What value in power is (" << argv[1] << " , " << argv[2]
			<< ") = " << power(atoi(argv[1], atoi(argv[2])
			<< endl;
	}
}
Last edited on
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

Also I will
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:
argv[0] = /home/kbw/bin/test
argv[1] = 2
argv[2] = 3
argc = 3

If the program is run with 1 argument, the program will see two arguments. For example, if run with /var/log/authlog:
argv[0] = /home/kbw/bin/test
argv[1] = /var/log/authlog
argc = 2

And if the program is run without command line args:
argv[0] = /home/kbw/bin/test
argc = 1

Try to work out what the two cases in the switch statement do from that explanation and what you asked for the program to do.
Last edited on
Topic archived. No new replies allowed.