Your code shouldn't even compile.
>if(!isdigit(next))
next is a string, but isdigit expects an integer.
>double x= atof(next);
next is a string, but atof expects a pointer to const char.
How about something more like this:
#include <iostream>
#include <fstream>
#include <cstring>
#include <string>
#include <cctype>
#include <sstream>
using namespace std;
bool parse_double ( const string& s, double& value )
{
return stringstream ( s ) >> value;
}
int main()
{
/*ifstream reader;
reader.open("num.txt");*/
istream& reader = cin;
string next;
double total=0;
while(reader>>next)
{
double x;
if ( parse_double ( next, x ) )
total += x;
}
cout<<total;
return 0;
} You should avoid things like atof in favor of less hairy solutions (like stringstream).
Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401