identifying parameters

closed account (1bfSwA7f)
are there any parameters in this program (i dont think there are bc it is int main())? if not, how can I add them to the program?

#include <iostream>
#include <iomanip>

using namespace std;

int main(){

int const NUM_EXPENSES = 7;
double expenses[] = {
45.25,
49.38,
12.75,
123.5,
8.25,
19.95,
145.7};
double *ptr;
ptr = expenses;
double average;
double total = 0;
int number;

cout << "#" << setw(21) << "Amount" << endl;
cout << "----------------------" << endl;
for (number = 0; number < NUM_EXPENSES; number++){
cout << number+1 << setw(21) << ptr[number] << endl;

total = total + ptr[number];

}

cout << fixed;
cout << setprecision(2);

average = total/NUM_EXPENSES;

cout << "Total" << setw(17) << total << endl;
cout << "Average" << setw(15) << average << endl;

return 0;
}

If you want your program to be able to process command line arguments you must tell the compiler to be ready to retrieve the command line arguments by adding arguments to main().

1
2
3
4
int main(int argc, char **argv)
{
    // The code for main().
}


The first argument will be the count of the number of arguments that you entered on the command line when starting the program, the second argument will be an array of C-strings.

So if you run your program like yourProgram YourFirstName YourSecondName YourAge argc will have a size of the number of arguments (in this case 4), and note that argv[0] is normally the name of the program being run and argv[argc + 1] is guaranteed to be a nulptr.

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

#include <iostream>
#include <iomanip>

using namespace std;

int main(int argc, char** argv){
    
    if(argc <= 1)
    {
        std::cout << "Too few arguments\n";
        std::cout << "USAGE: General 1.2 45.1 ...\n";
        return -1;
    }
    
    int const NUM_EXPENSES = argc;
    double* expenses = new double[argc];
    
    for(int i = 1; i < argc; i++)
    {
        expenses[i] = strtod(argv[i], NULL);
    }
    
    
    double average;
    double total = 0;
    
    cout << "#" << setw(21) << "Amount" << endl;
    cout << "----------------------" << endl;
    for ( int i = 1; i < NUM_EXPENSES; i++)
    {
        cout << i << setw(21) << strtod(argv[i], NULL) << endl;
        
        total = total + strtod(argv[i], NULL);
        
    }
    
    cout << fixed;
    cout << setprecision(2);
    
    average = total/(argc - 1);
    
    cout << "Total" << setw(17) << total << endl;
    cout << "Average" << setw(15) << average << endl;
    
    return 0;
}


Run the program in whatever shell your system has and:

general 1 2 3 4 5
#               Amount
----------------------
1                    1
2                    2
3                    3
4                    4
5                    5
Total            15.00
Average           2.50


'general' is the name I gave to the executable, 1 2 3 4 5 are the different input Amounts.

You can give the executable any name you like.

EDIT: out by 1 with average.
Last edited on
Topic archived. No new replies allowed.