I am trying to write an application that accepts command line argument like 1 + 2, calculates and prints the output. I am very new to c++, so i wrote some code - that's does not work. I think my main problem is lack of knowledge, i cannot figure out how to parse and convert this expression
1 has to go into int/double + has to remain a string to go through the switch statement 2 has to go into int/double
I already know that if there are spaces then each argument will contain the piece of string accordingly having argv[2] = "1" argv[3] = "+" argv = "2" so all i need now is to convert them to the right datatype so i can perform calculations, i have tried to convert using strtod also but every one of them throws exception, plus apparently i cannot write switch statement the way it it so its needs to be a string. I am very confused with all the conversions. Please let me know if i even think in the right way?
// Calculate.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <string>
#include <tchar.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
double oper1 = atof(argv[2]);
double oper2 = atof (argv[4]);
switch(char[3]){
case "+":
cout<< oper1 + oper2;
break;
}
return 0;
}