I have been trying to write this code that calls a text file in the command line, it then reads the file and if the file has integers that look like this (1+2+3+5)it takes there sum and outputs it into a different text file, if it looks like this (1+2 4+AB)it does not take the sum but goes straight to the output file and gives an error message. If anyone can help i would really appreciate it, thanks.
//Run in Command Line
#include<fstream>
#include<iomanip>
using namespace std;
int main (int argc,char *argv[])
{
int sum=0;
string filename;
fstream inputfile;
ofstream outfile;
argv[1]="filename";
if (argc>0)
{
fstream inputfile;
inputfile.open(argv[1],ios::in);
if(inputfile.is_open())
{
ofstream outfile;
outfile.open("sum.txt", ios::app);
char ch[256]={0};
while(!inputfile.eof())
for (int i=0;i<256;i++)
if(ch[i]>char(32) && ch[i]< char(127) && ch[i]!=' ')
{
inputfile>>argv[1]>>ch[i];
sum+=(int)ch[i];
outfile<<"sum= "<<sum<<endl;
inputfile.close();
outfile.close();
exit(0);
}
}
else if(inputfile.fail())
{
ofstream outfile;
outfile.open("sum.txt", ios::app);
outfile<<"Invalid Sequence"<<endl;
outfile.close();
exit (1);
}
}
}