How to pass integer parameter to main(int main)?

//This program demonstrates a funcion with three parameters
#include <iostream>
using namespace std;

//function prototype
void showSum(int, int, int);

int main(int x) { //Take notice int x parameter
int value1, value2, value3;

//get three integers
cout << "Enter three integers and I will display ";
cout << "their sum: ";
cin >> value1 >> value2 >> value3;

//call showSum passing three arguments.
showSum(value1, value2, value3);
cout << "Also, btw, x = " << x;
system("pause >nul");
return 124234; //Take notice return argument
}

void showSum(int num1, int num2, int num3) {
cout << (num1 + num2 + num3) << endl;
}

////////////////////////////////////////////////////////////////////////////////

I ran the program from command prompt:


C:\Windows\System32>"E:\CS I\a.exe"
Enter three integers and I will display their sum: 0
0
0
0
Also, btw, x = 1
C:\Windows\System32>echo %ERRORLEVEL% // return value (normally 0)
124234

C:\Windows\System32>"E:\CS I\a.exe" 2
Enter three integers and I will display their sum: 10
10
10
30
Also, btw, x = 2
C:\Windows\System32>"E:\CS I\a.exe" 3
Enter three integers and I will display their sum:
-
1956814207
Also, btw, x = 2
C:\Windows\System32>"E:\CS I\a.exe" 3
Enter three integers and I will display their sum: 0
0
0
0
Also, btw, x = 2
C:\Windows\System32>"E:\CS I\a.exe" 3 3 4 5 1 3 4
Enter three integers and I will display their sum: 0
0
0
0
Also, btw, x = 8
C:\Windows\System32>

My problem is I do not understand what is going on when I try to pass an argument to the program. (Since it is defined int main(int x)).

Any ideas?
There is no such function signature by standard definition. You must use int main(int argc, char **argv) else undefined behavior will happen when you try to use either argument.

This is actually a quirk of how C functions work. I won't go into details here but your actually seeing the result of the "argc" argument which is how many parameters you're passing to the program including the program itself (which should mostly be 1 or above).

If you want to pass an integer, you must use the above mentioned function signature, then figure out how to cast the string of the integer you want to an actual data represented integer.
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
//This program demonstrates a funcion with three parameters
#include <iostream>
using namespace std;

//function prototype
void showSum(int, int, int);

int main(int argc, char *argv[]) { //Take notice proper parameters

if (argc != 2) return -1; // need to invoke program with exactly one argument 

int value1, value2, value3;

//get three integers
cout << "Enter three integers and I will display ";
cout << "their sum: ";
cin >> value1 >> value2 >> value3;

//call showSum passing three arguments.
showSum(value1, value2, value3);
cout << "Also, btw, x = " << argv[1] << endl;

return 0; //Take notice return argument 
}

void showSum(int num1, int num2, int num3) {
cout << (num1 + num2 + num3) << endl;
}
Topic archived. No new replies allowed.