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);

}

	}

}
#include <iostream>
#include <fstream>

int main ()  {
   std::ifstream file("asd.txt");
   std::ofstream resfile("dsa.txt");
   int x=0;
   int result=0;
   char sign='+';
   while (!file.eof() && !file.fail()) {
      file >> x;
      file >> sign;
      if (sign=='+')  {
         result+=x;
       }
      else break;
    }
   resfile << result;
   resfile.close();
   file.close();
 }
Comments
A bit harsh, WaltP. Caut do take note you have to explain next time:)
Code with no explanation si mostly useless. EXPLAIN!!!

Im not allowed to hardcode filenames into it. Im trying to integrate what you use in your code into mine, but all it does is show the last integer of the file, instead of doing the sum.

//Esteban Efigenio
//Run in Command Line
#include<iostream>
#include<fstream>
#include<iomanip>

using namespace std;

int main (int argc,char *argv[])

{
	string filename;
	std::ifstream inputfile;
	std::ofstream outfile;

	if (argc>1)
	{
	inputfile.open(argv[1], ios::in);

	if(inputfile.is_open())
	{
	outfile.open("sum.txt", ios::app);

	int sum=0;
	int x=0;
	char sign='+';
	while(!inputfile.eof() && !inputfile.fail())
		
	inputfile>>x;
	if(sign=='+')
	sum+=x;
	cout<<sum;
	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);

}

	}

WOW! thanks had to move some stuff around so that it wouldn't keep displaying the numbers but actually give me the sum. Almost done with this project, just a little more. Thanks a lot.

//Run in Command Line
#include<iostream>
#include<fstream>
#include<iomanip>

using namespace std;

int main (int argc,char *argv[])

{
	int sum=0;
	int x=0;
	char sign='+';

	string filename;
	std::ifstream inputfile;
	std::ofstream outfile;

	if (argc>1)
	{
	inputfile.open(argv[1], ios::in);

	if(inputfile.is_open())
	{
	outfile.open("sum.txt", ios::app);

	
	while(!inputfile.eof() && !inputfile.fail())
	{
	inputfile>>x;
	if(sign=='+')
	{
	sum+=x;
	}
	}
	cout<<sum;
	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);

}

	}

I have one problem, i have to put some special cases in there like it cannot allow
(1+2+A) and (1+2 5+4) if these happen then i write invalid sequence to the output file. Help please.

//Run in Command Line
#include<iostream>
#include<fstream>
#include<iomanip>

using namespace std;

int main (int argc,char *argv[])

{
	int sum=0;
	int x=0;
	char sign='+';

	std::ifstream inputfile;
	std::ofstream outfile;

	if (argc>1)
	{
	inputfile.open(argv[1], ios::in);

	if(inputfile.is_open())
	{
	outfile.open("sum.txt", ios::app);

	
	while(!inputfile.eof() && !inputfile.fail())
	{
	inputfile>>x;
	if((x>char(32)) && (x<char(127)))
	{
	outfile.open("sum.txt", ios::app);
	outfile<<"Invalid Sequence"<<endl;
	outfile.close();
	exit(0);
	}
	else if (sign=='+')
	{
	sum+=x;
	}
	outfile<<"sum= "<<sum<<endl;
	inputfile.close();
	outfile.close();
	exit(1);	
	}
	}
	
	}
	else if(inputfile.fail()|| inputfile.bad() )
	{
	ofstream outfile;
	outfile.open("sum.txt", ios::app);
	outfile<<"Invalid Sequence"<<endl;
	outfile.close();
	exit (2);

}

	}

I have one problem, i have to put some special cases in there like it cannot allow
(1+2+A) and (1+2 5+4) if these happen then i write invalid sequence to the output file. Help please.

Look at each character one at a time. What are the acceptable characters that can follow the current character?
For example:
Currently a digit. Next can be digit, SPACE, operator. So set some booleans to TRUE.
Next character...
Now a SPACE. Next can be SPACE or operator only. Set two bools to TRUE, one to FALSE.

Something like that...

This article has been dead for over six months. Start a new discussion instead.