-
What am i doing wrong?
This is a program that reads data from a disk and outputs data to the same disk.
The data is:
@2,89#3,*67
$187,3#34,72#123#
*3*7*1#*3,4,8
The program must exclude the symbols & just output the digits with the '#' starting a new line.
Output should be:
289
367
1873
3472
123
371
348
#include <fstream.h>
#include <iostream.h>
#include <cctype>
using namespace std; //introduces namespace std
char const nwln = '\n';
int main ( )
{
char ch;
ifstream data;
ofstream dataout;
int n;
int sum;
data.open ("a.\\data.txt");
if (!data)
{
cout << "ERROR -- Cannot open data.txt for input" << end1;
return 1;
}
dataout.open ("a:\\dataout.txt");
if (!dataout)
{
cout << "ERROR -- Cannot open dataout.txt for output" << end1;
return 1;
}
data.get(ch);
while (data)
{ sum = 0;
cdigit = 0;
while ((ch != '\n') && data)
{
if (isdigit(ch))
cdigit = ch - '0';
sum = (sum*10) + n
dataout.put(ch);
data.get(ch);
}
if (ch == '#')
dataout << sum << end1;
sum = 0;
}
}
data.close(); dataout.close();
return 0;
}
-
This should work:
Code:
while (data.get(c)) {
if (c == '#')
dataout<<'\n';
if (!isdigit(c)
continue;
dataout<< c;
}
-
grrrr
Code:
#include <fstream>
#include <iostream>
#include <cctype>
using namespace std; //introduces namespace std
-
:D
You have got to be kidding.
-
PHP has a great function for this type of thing, explode. But, if you are using C++ you will probably have to use strtok. google it.
-
>But, if you are using C++
I'd say that's a fair assumption seeing as how the web site is called Cprogramming.com and the forum being posted in is called C++ Programming. Not to mention that the original post including quite a bit of C++ source.
>you will probably have to use strtok.
strtok breaks up a string based on delimiters you pass to it. If you include all of the non-digit characters, strtok will split up the string too much. If you only include the '#' as a delimiter to strtok, you still have to deal with the other special characters. Conclusion: strtok doesn't solve the problem well enough to be worth using it. And of course there's the fact that the original post didn't say anything about using strings, just character by character stream input.
-
true, just suggestions.....