I wrote up a piece of trivial code to output sum total of ascii value, in both c++ and python, given an input file.
The c++ piece seems to have excluded '\n' while the python piece did include '\n' as part of its calculation, for the same input text file.
I was wondering if there is any step in my code that I overlooked.
code pieces:
import sys
try:
f=open(sys.argv[1]).read()
except:
print " file not found \n"
sys.exit()
sum=0
for line in f:
for character in line:
try:
if character=='\n':
pass
else:
print character
sum+=ord(character)
except:
print "failed \n"
pass
print "The sum is %d \n" %sum
ANd the c++ piece is :
#include "iostream"
#include "fstream"
#include "string"
int k;
int main(int argc, char *argv[])
{
int sum=0;
std::string line;
std::ifstream myfile (argv[1]);
if (myfile.is_open())
{while (myfile.good())
{
getline (myfile,line);
for (k=0;k<(line.length());k++)
{
sum=sum+int(line[k]);
}
}
std::cout<<" The total sum is : " <<sum<<std::endl;
}
else std::cout<< "Unable to open file ";
return 0;
}