Command line arguements

I need to write a program that finds the sum, product, or mean of a set of numbers using the command line. I'm thinking this should work but now it says cout is ambiguous. what am I doing wrong?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;

int main(int argc, char *argv[])
{
	string option1 = "sum";
	string option = string(argv[1]);
	if(argv[1] == option1)
    {double sum = 0;
    for(int counter = 0; counter < argc; counter ++)
    {
        sum += atof(argv[counter]); //compact form of :  sum = sum + atof(argv[counter]);
    }
    cout << "Sum = " << sum << endl;
	return 0;
	}
}


Update: I added std::cout and the squiggle went away but now the program crashes. it says invalid NULL pointer.
Last edited on
You need to check the number of arguments before trying to use them. There's no guarantee that argv[1] will even be a valid index.
You are thinking the wrong way about how the program is taking input. Trash your thought process of using the arguments in main. Start over I will give you one example that you can use to help you to finish your project.

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
#include <iostream>
using namespace std;

int main()
{
       int x, y, sum;
        
      cout << "Enter first value to be added. " << endl;
      cin >> x;

      cout << "Enter second value to be added. " << endl;
      cin >> y;
     
      sum = x + y;
     // for division use        sum = x/y;
     // for subtraction use     sum = x-y;
     // for multiplication use  sum = x*y;
     // ***of course you want to change the sum name to fit exactly what 
     // you are doing like if you are subtracting, a more appropriate name would
    // be difference. For multiplication product. 
     

      cout << "The sum of " << x << " + " << y << " = " << sum << endl;

       system("PAUSE");
       return 0;
}
Last edited on
novellof

That I can do I need to use the command prompt. I wrote the whole program already without using the command prompt and that's not what they wanted.

Basically I need to do this in the end.

user>Project.exe sum 1 2 3
Sum = 6

or
user>Project.exe product 2 3 4
product = 6

Lb I get what you're saying but I'm not sure how that works

the hint the paper has is

string option = string(argv[1])

but that makes it not work
Last edited on
That hint doesn't tell you to make that the very first thing in the main function. Before you use that, you need to check that you actually have two or more arguments.
Ok I'm working it out and kinda taking a different approach to check for the arguments. Still I get the string null problem here's the new code.
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71


#include<iostream>
#include<string>
#include<cmath>
#include<iomanip>

using namespace std;

int main(int argc, char** argv)
{
	float sum = 0;
	float product;
	float mean;

		
	int index = 2;
	

	// Calculate sum and product as long as the user enters numbers. 
	while(index <= argc)
	{
		float number = atof(argv[index]);
		sum += number;
		product *= number;
		index ++;

	}
		
	
	//Return 1 from main and do not print anything because the program is called without any arguments
	if (argc < 2)
	{
		return 1;
	}
	string option = string(argv[1]);
	if(option != "sum" && option != "product" && option != "mean")
	{	//Return 1 from main and do not print anything because the first argument does not match any of the three expected strings
		return 1;
	}

	//Return 1 from main and do not print anything because no numbers are provided
	else if(argc<3)
	{
		return 1;
	}
	
	//User selected sum option. Print the sum of the numbers entered by the user.
	else if(option == "sum")
	{
		cout<<sum;
		return 0;
	}

	//User selected product option. Print the product of the numbers entered by the user.
	else if(option == "product")
	{
		cout<<product;		
		return 0;
	}

	//User selected mean option. Print the mean of the numbers entered by the user.
	else if(option == "mean")
	{
		float mean = sum/(argc-2);
                cout<<mean;
		return 0;
	}

	
}
Last edited on
On line 21 you accidentally used <= instead of <
Thanks for your help. I finally got it all to work!
Hey this is interesting...Is this really a beginner thing? I have never seen the
utilization of variables from the main function..except for win32 programming.
Where can I learn more about this technique?
closed account (48T7M4Gy)
Try this:

http://www.cplusplus.com/articles/DEN36Up4/
Topic archived. No new replies allowed.