Command argument
My code is working, but I need add command argument for my files.
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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
|
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
template <class type> class DynamicArray
{
private:
type *arr;
int length = 0;
public:
DynamicArray(int size){
arr = new type[size];
}
void add(type newElement){
arr[length] = newElement;
++length;
}
void printArray(){
for (int i = 0; i<length; ++i){
cout << arr[i] << " ";
}
}
void printArrayReversed(){
for (int i = length - 1; i>0; --i){
cout << arr[i] << " ";
}
}
~DynamicArray(){
delete(arr);
}
};
int main(){
int size = -1;
ifstream file1;
ifstream file2;
ifstream file3;
try{
cout << "Please enter a size for the lists: ";
cin >> size;
if (size <= 0)
throw "Invalid size entered.";
DynamicArray<int> intArray(size);
DynamicArray<char> charArray(size);
DynamicArray<double> doubleArray(size);
string file1Name, file2Name, file3Name;
cout << "Enter first (int) file name: ";
cin >> file1Name;
if (file1Name.length() == 0)
throw "Inavlid file name.";
cout << "Enter second (char) file name: ";
cin >> file2Name;
if (file2Name.length() == 0)
throw "Inavlid file name.";
cout << "Enter third (double) file name: ";
cin >> file3Name;
if (file3Name.length() == 0)
throw "Inavlid file name.";
file1.open(file1Name, ios::in);
file2.open(file2Name, ios::in);
file3.open(file3Name, ios::in);
if (!file1.is_open() || !file2.is_open() || !file3.is_open())
throw "Unable to open input file";
//reading from first file (int)
int iTemp;
while (file1 >> iTemp){
intArray.add(iTemp);
}
//reading second file (char)
char cTemp;
while (file2 >> cTemp){
charArray.add(cTemp);
}
//reading second file (double)
double dTemp;
while (file3 >> dTemp){
doubleArray.add(dTemp);
}
//printing first (int) array in normal and reversed orders
cout << endl << endl;
intArray.printArray();
cout << endl;
intArray.printArrayReversed();
//printing second (char) array in normal and reversed orders
cout << endl << endl;
charArray.printArray();
cout << endl;
charArray.printArrayReversed();
//printing third (double) array in normal and reversed orders
cout << endl << endl;
doubleArray.printArray();
cout << endl;
doubleArray.printArrayReversed();
}
catch (const char *error){
cout << endl << "ERROR: " << error << endl << endl;
}
system("pause");
return 0;
}
|
Use int main(int argc, char argv[][])
parameter from command line will be save in argv[]
how do I save it It in argv[]? should I replace any of my variable by argv[]?
It depends
for example
1 2 3
|
int main(int argc, char argv[][]){
cout<<argv[1];
}
|
you also able to use it like this
1 2 3 4
|
int main(int argc, char argv[][]){
string x=argv[1];
cout<<x;
}
|
Topic archived. No new replies allowed.