I have a simple code wherein i am finding simple interest and compound interest. The issue I am facing is that I have to use CLI for input. I need one int and two floats to work with. Earlier i was using cin.fail() which was doing the type checking for me and that worked perfectly but i need to take CLI such that inputs such as 1000 1? 5 are treated as invalid. Please help.
#include<iostream>
#include<cmath>
#include<iomanip>
#include<cstdlib>
using namespace std;
int main(int argc, char *argv[])
{
int p;
float r,t,ci;
p=atoi(argv[1]);
r=atof(argv[2]);
t=atof(argv[3]);
if(p<=0 || r==0 || t<=0) {
//we want p>0 t>0 and r==0 from the logic that atof will retrun 0 if r is non numeric
cout<<"Invalid Input"<<endl;
}
else {
float si=(p*r*t)/100;
cout<<"Simple interest:"<<fixed<<setprecision(2)<<si<<endl;
ci=p*pow((1+r/100),t)-p;
cout<<"Compound interest:"<<ci<<endl;
}
return 0;
}