I have been trying to learn argc and argv. But to do that, I have been trying to implement it into a calculator with an extra calculator portion if argv doesn't have any data. But I can't seem to figure out how to do it.
Here's the calculator before argc/argv:
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main() {
double num1, num2;
char op;
char response('y');
cout << " /=========================\\" << endl;
cout << "( Operators are: + - * / )" << endl;
cout << " \\=========================/" << endl;
do {
cout << "> ";
cin >> num1;
cin >> op;
cin >> num2;
double result;
if(cin.fail()) {
cout << "ERROR" << endl;
cout << "Either, you put a letter, or the calculator can\'t calculate." << endl;
break;
};
if(op == '+') {
result = num1 + num2;
} else if(op == '-') {
result = num1 - num2;
} else if(op == '/') {
result = num1 / num2;
} else if(op == '*') {
result = num1 * num2;
} else {
cout << "Invalid Operator" << endl;
cout << "Exit code: ";
};
cout << result << endl;
cout << "Do you want to enter another calculation? (Y/N) ";
cin >> response;
} while (response == 'Y' || response == 'y');
return 0;
}
and here's it afterwards:
#include <iostream>
#include <string>
#include <sstream>
#include <cmath>
using namespace std;
int main(int argc, char** argv) {
if(*argv[1] == '-t') {
double num1, num2;
char op;
char response('y');
cout << " /=========================\\" << endl;
cout << "( Operators are: + - * / )" << endl;
cout << " \\=========================/" << endl;
do {
cout << "> ";
cin >> num1;
cin >> op;
cin >> num2;
double result;
if(cin.fail()) {
cout << "ERROR" << endl;
cout << "Either, you put a letter, or the calculator can\'t calculate." << endl;
break;
};
if(op == '+') {
result = num1 + num2;
} else if(op == '-') {
result = num1 - num2;
} else if(op == '/') {
result = num1 / num2;
} else if(op == '*') {
result = num1 * num2;
} else {
cout << "Invalid Operator" << endl;
cout << "Exit code: ";
};
cout << result << endl;
cout << "Do you want to enter another calculation? (Y/N) ";
cin >> response;
} while (response == 'Y' || response == 'y');
return 0;
} else {
double result;
int num1 = *argv[1] - 48;
int num2 = *argv[3] - 48;
if(*argv[3] == '+') {
result = num1 + num2;
}
cout << result << endl;
}
}
I'm very confused on how I can do it because if It has no response it give "Segmentation Fault" on my Linux terminal...