1// command line arguments in c++ are stored in an array of c-strings
2// # of arguments: argc
3// actual arguments: argv
4
5#include <iostream>
6using namespace std;
7
8int main(int argc, char** argv){
9 for(int i = 0; i < argc; i++){
10 cout << "argv" << i << ": " << argv[i] << endl;
11 }
12 return 0;
13}
14
15/*
16
17./main.exe hello world
18
19argv0: main.exe
20argv1: hello
21argv2: world
22
23*/
24
1#include <iostream>
2#include <cstdarg>
3using namespace std;
4
5double average(int num,...) {
6 va_list valist; // A place to store the list of arguments (valist)
7 double sum = 0.0;
8 int i;
9
10 va_start(valist, num); // Initialize valist for num number of arguments
11 for (i = 0; i < num; i++) { // Access all the arguments assigned to valist
12 sum += va_arg(valist, int);
13 }
14 va_end(valist); // Clean memory reserved for valist
15
16 return sum/num;
17}
18
19int main() {
20 cout << "[Average 3 numbers: 44,55,66] -> " << average(3, 44,55,66) << endl;
21 cout << "[Average 2 numbers: 10,11] -> " << average(2, 10,11) << endl;
22 cout << "[Average 1 number: 18] -> " << average(1, 18) << endl;
23}
24
25/*
26NOTE: You will need to use the following 'data_types' within the function
27va_list : A place to store the list of arguments (valist)
28va_start : Initialize valist for num number of arguments
29va_arg : Access all the arguments assigned to valist
30va_end : Clean memory reserved for valist
31*/
1// Use command lines
2
3int main(int argc, char *argv[])
4{
5
6 for(int i = 1; i < argc; i++){
7 if(!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help") ){
8 printf("Usage: App <options>\nOptions are:\n");
9 printf("Option list goes here");
10 exit(0);
11 }else if(!strcmp(argv[i], "-c") || !strcmp(argv[i], "--custom")){
12 printf("argument accepted");
13 }else{
14 if(i == argc-1){
15 break;
16 }
17 MessageBox(NULL, TEXT("ERROR: Invalid Command Line Option Found: \"%s\".\n", argv[i]), TEXT("Error"), MB_ICONERROR | MB_OK);
18 }
19 }
20
21 MessageBox(NULL, TEXT("ERROR: No Command Line Option Found. Type in --hep or -h"), TEXT("Error"), MB_ICONERROR | MB_OK);
22}