-1

Question : Write a program in a C++ to display the sum of digits present in a text file.

-->

#include<iostream.h>  
#include<fstream.h>  
int main()  
{  
 ifstream f1("Fees.txt");  
 int sum=0;  
 char n;  
 while(f1.eof()==0)  
 {  
  f1>>n;  
  if(isdigit(n))  
   {  
   sum=sum+n;  
   }  
 }  
    cout<<sum;  
}  

I know the program's not working because string is changed to digit,( 1 changes to its ASCII code 49 instead of 1)
Please guide me
And is there a better way to approach the question?

UPDATE Ok so I changed the line sum+=n to sum+=n-'0'. Still the program only seems to be working when there is a special character in addition to numbers in the text file Fees.txt.
Example:

1 2 9

This does not work

1 2 9ok

This does work, any idea what's going on here?

user3500780
  • 11
  • 1
  • 8
  • 2
    you're almost there - just substract ASCII '0' from n – hauron Jun 06 '16 at 12:43
  • If you know the file is ASCII, then you can subtract the ASCII code of 0, i.e. `'0'`, to the char to get the proper digit. This is because the ASCII codes of 0,1,2,3... are consecutive, so adding 4 to the ASCII code of 0 you get the code for 4. – chi Jun 06 '16 at 12:44
  • 1. It's not c++. 2. You should [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – luk32 Jun 06 '16 at 12:45
  • 2
    Somebody seems to be teaching you 1990s C++. – Lightness Races in Orbit Jun 06 '16 at 12:46
  • @luk32 This is C++. It is old C++(most likely they are using TurboC++) but it is C++. There is no `iostream` or `ifstream` in C – NathanOliver Jun 06 '16 at 12:46
  • @NathanOliver: I think we can agree that pre-standard C++ is no longer "C++" in the general sense, without further qualification. It's been almost twenty years. – Lightness Races in Orbit Jun 06 '16 at 12:53
  • @NathanOliver Oh yea. My bad. `fstream` is certainly not c. But is `ifstream.h` c++? – luk32 Jun 06 '16 at 12:53
  • @LightnessRacesinOrbit Do we have a tag we can use to mark this as pre-standard C++? – NathanOliver Jun 06 '16 at 12:56
  • @luk32 It is from before C++ was standardized. Compilers like Turbo C++ use `` instead of ``. I believe they cannot compile ``. – NathanOliver Jun 06 '16 at 12:58
  • 1
    @NathanOliver: If only! I think a fair compromise for the time being is for OPs to tag [tag:turbo-c++] if they're using it. That almost always signifies the same thing (as you pointed out before), and seems reasonable given that by definition we'll need to know what non-standard dialect of primordial C++ is in use. We'd have to create the tag, though (or perhaps there's a would-be synonym already that I haven't found yet). We already use [tag:c++03], [tag:c++11] and [tag:c++14], after all. – Lightness Races in Orbit Jun 06 '16 at 12:58
  • @NathanOliver Ok, fair enough. I don't think you can call something c++ if it doesn't conform to any of the standard. At least here people tend to be very strict. But I don't mean to go into discussion as it's a rather POV based. I was honestly asking out of curiosity because I didn't recall standard libraries to have `.h` suffix, even in pre c++11, which would be c++99 I guess. Thanks for sharing the historical context. – luk32 Jun 06 '16 at 13:04
  • @LightnessRacesinOrbit Well we do have [tag:turboc++]. Most likely it will need to be compiler specific since there was different flavors of pre-standard C++. – NathanOliver Jun 06 '16 at 13:05
  • @NathanOliver: Yep, as I said. :) Good to know the tag does exist. Perhaps we can make the version with a hyphen into a synonym for clarity. – Lightness Races in Orbit Jun 06 '16 at 14:05
  • @LightnessRacesinOrbit yeah my bad, I actually use but teachers don't accept that, is there any other thing that points out I'm outdated? :P – user3500780 Jun 07 '16 at 04:32
  • @luk32 please see the update – user3500780 Jun 07 '16 at 04:41
  • @user3500780: It's a shame your teachers are training you to use obsolete technology that won't be of use in the job market :( – Lightness Races in Orbit Jun 07 '16 at 13:39
  • `while(f1.eof()==0)` is not right btw - you're checking too early and, although not invalid, the comparison to literal `0` is just weird – Lightness Races in Orbit Jun 07 '16 at 13:39
  • @LightnessRacesinOrbit pls suggest what is new technology which I should be learning? – user3500780 Jun 09 '16 at 03:55
  • @LightnessRacesinOrbit isn't THIS c++? – user3500780 Jun 11 '16 at 17:37
  • @LightnessRacesinOrbit thenewboston and many teachers call THIS c++ tho? – user3500780 Jun 13 '16 at 05:53
  • @user3500780: Yes, sadly, many people do and say wrong things. – Lightness Races in Orbit Jun 13 '16 at 09:28
  • @LightnessRacesinOrbit do u have a link or any book that teaches thereal deal? – user3500780 Jun 14 '16 at 07:41
  • @user3500780 http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – Lightness Races in Orbit Jun 14 '16 at 09:51

2 Answers2

3

As you point out, the ASCII codes for digits do not start at zero, which is your problem. Luckily though, they do lie in a continuous range, so all you need to do is change the line:

sum=sum+n;

to

sum += (n - '0');
Smeeheey
  • 9,906
  • 23
  • 39
1

This has nothing to do with ASCII. The C and C++ standards require that the values of '0'..'9' be contiguous and increasing, so converting a character that represents a digit simply requires subtracting '0', regardless of the character encoding. So:

sum += n - '0';

will fix the problem.

Pete Becker
  • 74,985
  • 8
  • 76
  • 165