Hello...
I am still a newbie! I have an assignment to write a C++ program to cover a series of ASCII character digits into numbers.
Like if the input (that I have ti input from a saved file) was $1,9,56#%34,9 it would output
1956
349
on two seperate lines ignoring all characters except the digits using a character processing algorithm (pasted below)and nested loops and decisions. The # symbol is what would separate the numbers...The final numbers have to be whole numbers, does that make sense? Then my coversions need to be output to a file.
I have the processing algorithm, but its the conversion part that is stumping me. I think I am overthinking this and I can't find a similar example on the web in C++.
I know for example '5' in ASCII is 53 and if I subtract '0' from that which is 48, I end up with 5, but how to I write a loop for that? And then how do I sum up the indivdual numbers to make one whole number? I'm lost...Any advice would help...thanks!
/*
* Description: Convert ASCII symbols into numbers using
* nested loops and decisions and the character processing
* algorithm.
*/
//Character Processing Algorithm
#include <fstream.h>
#include <iostream.h>
char const nwln = '\n';
int main ()
{
char ch;
ifstream data;
ofstream out;
data.open ("a:\\file1.txt");
if (!data)
{
cout << "Failure to Open file1.txt" << endl;
return 1;
}
out.open ("a:\\out.txt");
if (!out)
{
cout << "Failure to Open out.txt" << endl;
return 1;
}
data.get (ch); // priming read for end-of-file loop
while (data)
{
while ((ch != nwnl) && data)
{
out.put (ch); // all processing here
data.get (ch); // update for inner loop
} // inner loop
if (data)
{
out.put (ch); // write last newline
data.get (ch); // update for outer loop
} //if
} //outer loop
cout << "The End..." << endl;
data.close (); out.close ();
return 0;
} //main