command line help

Hey everyone, I am trying to use command line arguments. The program should take 5 integers via the command prompt and then add all the numbers and print the sum. It is not working. I got a previous version to compile and run, but It was not correct. It essentially ran the program from the command propmt, instead of taking the arguments autmatically

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
  #include <iostream>
#include <limits>

using namespace std;

int main(int argc, char *argv[])
{
    //if the user did not enter enough arguments
    if(argc < 2)
    {
        //type out what is required
        cerr << "Usage: " << argv[0] << " integers" << endl;
        return 1;
    }

    int input[5];       //array declaration
    for(int i = 0; i < argc; ++i)
    {
        cout << i + 1 << ").  ";
        //input validation
        if((double)argv[i] != (int)arv[i])
        {
            cerr << "ERROR: Arguments not valid\n";
            return 1;
        }
        input[i] = argv[i];     //initializes each array index
    }

    int sum = 0;        //sum of the values entered
    //sums the integers entered
    for(int i = 0; i < 5; ++i)
        sum += input[i];

    cout << "Sum of all integers entered = " << sum << endl;

    return 0;
}
Use the atoi function to get integers and atof to get floating point numbers from argv. See an example here: http://www.cs.ucsb.edu/~pconrad/cs16/topics/cmdlineargs/
Casting an array of chars to int or to double is not supported in c++.
The program should take 5 integers

You have:
1
2
3
4
5
6
7
    //if the user did not enter enough arguments
    if(argc < 2)
    {
        //type out what is required
        cerr << "Usage: " << argv[0] << " integers" << endl;
        return 1;
    }

Besides your Usage: output being wrong, if it requires 5 integers and the user input 4 the error would not be displayed with if(argc < 2). They only need to enter two integers to pass that test. Usage: only asks for one integer.
what this function used for?
 
if((double)argv[i] != (int)arv[i])


if you want to convert char * into int you should just convert it and not compare it

CMIIW
@chipp: I believe the OP is trying to see if the number is a whole number.
If you really want to make sure they've entered an integer, use strtol()

1
2
3
4
5
6
7
char *endp;
long val;
...
val = strtol(argv[i], &endp, 10);
if (*endp) {
    // user didn't enter a number
}


This will work properly if they enter odd things like "-1" or "-123hello" or "" or "18.0"
I figured why not test the two functions?
strtol_vs_atoi.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <iomanip>
#include <cstdlib>
using namespace std;

int main(int argc, char* argv[]) {
	
	for(int i = 1; i < argc; i++) {
		cout << "argv[" << i << "]: " << argv[i] << "\n";
		cout << "atof: " << atof(argv[i]) << "\n";
		cout << "atoi: " << atoi(argv[i]) << "\n";
		cout << "integer? " << boolalpha << (atof(argv[i]) == atoi(argv[i])) << "\n";
		cout << "strtol: " << strtol(argv[i], NULL, 0) << "\n\n";
	}
	
	return 0;
}


How I compiled and ran the program:
g++ strtol_vs_atoi.cpp -o a.out && ./a.out -1 -123hello 18.0 19.6

The output I received:
argv[1]: -1
atof: -1
atoi: -1
integer? true
strtol: -1

argv[2]: -123hello
atof: -123
atoi: -123
integer? true
strtol: -123

argv[3]: 18.0
atof: 18
atoi: 18
integer? true
strtol: 18

argv[4]: 19.6
atof: 19.6
atoi: 19
integer? false
strtol: 19


In summary:
Teaching atoi to beginners is just easier. There will be no chance of them being confused by extra parameters. I say this even though I use strtol for better error checking in my production code.

Edit: I changed the code to also show detection of whether or not the number is an integer.
Last edited on
Wow guys thanks! I'll definitely do some research about this today and figure it out. I appreciate all the feedback! I'll update when I finish
Alright this works great!!

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
#include <iostream>
#include <cstdlib>
#include <limits>

using namespace std;

int main(int argc, char *argv[])
{
    //if the user did not enter enough arguments
    if(argc != 6)
    {
        //type out what is required
        cerr << "Usage: " << argv[0] << " int1 int2 int3 int4 int5\n";
        return 1;
    }

    int input[5];       //array declaration
    for(int i = 1; i < argc; ++i)
    {
        input[i-1] = atoi(argv[i]);     //converts input to int
    }

    int sum = 0;        //sum of the values entered
    //sums the integers entered
    for(int i = 0; i < 5; ++i)
        sum += input[i];

    cout << "Sum of all integers entered = " << sum << endl;

    return 0;
}


Again thanks for all the help!!
Topic archived. No new replies allowed.