# How to Change a cin Input to a Command Line Argument Input in C++

I am trying to change my code from asking for a cin user input of x and y values to using a single command line argument with the name of a file. The file has a series of doubles for x and y, one pair on each line separated by a space. I understand how to write the code like I did here, but when it comes to command line arguments, I get confused. If anyone could offer any tips or help in converting the code, I would be very appreciative.

```
1 #include <iostream>
2 #include <vector>
3 #include <cmath>
4
5 using namespace std;
6
7 double correlation_r(const vector<double> &x, const vector<double> &y);
8 double mean(const vector<double> &x_obs);
9 double st_dev(const vector<double> &x_obs);
10 void least_squares_regression(const vector<double> &x, const vector<double> &y);
11
12 int main(){
13
14 double x;
15 double y;
16 vector<double> x_obs;
17 vector<double> y_obs;
18
19 cout << "Please enter two real number values for x and y: ";
20
21 while(cin >> x >> y){
22 if (!cin.good()){
23 break;
24 } else {
25 x_obs.push_back(x);
26 y_obs.push_back(y);
27
28 cout << "Please enter two real number values for x and y or a string to terminate: ";
29 }
30 }
31
32 if(x_obs.size()==0||x_obs.size()==1){
33 cout << "The sample was too small to be useful.";
34 return 0;
35 }
36
37 cout << "The sample mean of x is " << mean(x_obs) << endl;
38 cout << "The sample mean of y is " << mean(y_obs) << endl;
39
40 cout << "The sample standard deviation of x is " << st_dev(x_obs) << endl;
41 cout << "The sample standard deviation of y is " << st_dev(y_obs) << endl;
42
43 if(st_dev(x_obs)==0.0){
44 cout << "The correlation is undefined." << endl;
45 } else if(st_dev(y_obs)==0.0&& st_dev(x_obs)!=0.0){
46 cout << "The correlation is undefined." << endl;
47 } else {
48 cout << "The sample correlation is " << correlation_r(x_obs, y_obs) << endl;
49 }
50
51
52 least_squares_regression(x_obs, y_obs);
53
54 return 0;
55 }
56 void least_squares_regression(const vector<double> &x, const vector<double> &y){
57 double m;
58 double b;
59 if(st_dev(x)!=0 && st_dev(y)!=0){
60 m=correlation_r(x,y)*(st_dev(y)/st_dev(x));
61 b= mean(y)-(m*mean(x));
62 cout << "The least squares regression line is y=" << m << "x+ " << b << endl;
63 } else if(st_dev(x)==0){
64 cout<< "The least squares linear regression line is undefined."<< endl;
65 } else if(st_dev(y)==0 && st_dev(x)!=0){
66 cout<< "The least squares linear regression line is y=" << mean(y) << endl;
67 }
68 return;
69 }
70
71
72 double correlation_r(const vector<double> &x, const vector<double> &y){
73 double r = 0.0;
74 for(int i = 0; i < x.size(); i++){
75 r += (x[i] - mean(x))*(y[i] - mean(y))/st_dev(x)/st_dev(y);
76 }
77 return r = r / (x.size() - 1.0);
78 }
79
80
81 double mean(const vector<double> &x_obs){
82 double total = 0.0;
83 for (int i = 0; i < x_obs.size(); i++){
84 total += x_obs[i];
85 }
86 return total/x_obs.size();
87 }
88
89 double st_dev(const vector<double> &x_obs){
90 double x_bar = mean(x_obs);
91 double total = 0.0;
92 for (double elem : x_obs){
93 total += pow(elem - x_bar, 2);
94 }
95 return pow( total/(x_obs.size() - 1.0), .5);
96 }
```

### 1 Answer

You need to change your main definition to

`int main(int argc, char ** argv)`

`argc`

is the number of arguments, separated by whitespaces, you passed to your program

`argv`

is an array of c strings which hold your arguments. `argv[0] is always programs path.

So tp do what ypu want to achieve, change your main and take the command line arguments to create a `fstream`

or `ifstream`

object which acts prettyuch like your stabdard `istream`

friend `cin`

just that it reads from files instead of `stdin`