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

Recommended Answers

All 7 Replies

Can you keep your numbers in an array? If not, you're going to have to "get" and keep a count of the number of valid digits (hint, you can use < and > with chars), and then "unget" to go back to convert them to numbers.

Do you need to convert at all? If you're not going to do any maths with the numbers then you just leave them and print them to the new file. If you do need to convert them, you can use atoi() to do it.

As Jonsca said, the simplest way is to read chunks of the file into memory and go through them there before printing them to the new file. for example, you can use getline() to read a line from the file. Most often, I use getline like this:

std::ifstream data("in.txt");
std::string block;
getline(data, block);

In this case, getline() gets data from the stream an puts it into block until it comes to a \n character. The good thing is that you can pass getline() a third argument to use as a delimiter, doing this relieves you of some of the work of splitting the data up yourself. All you have to worry about is getting the numbers out. So you can do:

char delim = '#';
getline(data, block, delim);

So, with the example you gave, the first time through a loop block will be set to $1,9,56 . So you just have to go through block and find the numbers. You can use the functions in <cctype> to do this.

I think some of that is probably a few lectures down the road for the OP, though. I agree in that I didn't understand the need to convert to int and then write it out again.

I think some of that is probably a few lectures down the road for the OP, though. I agree in that I didn't understand the need to convert to int and then write it out again.

Maybe, although I think the only extra details are the use of std::string and getline() , which isn't too bad. Mostly, the difference is in the approach, which I thought might be a good lesson anyway :) Point taken though :)

Thanks for all the tips everyone! My instructor said that we are not to use functions, unfortunately, and we should use loops. I suppose "if" the character is not a 0-9 then skip it, else "print", right? Maybe? Lol. I need to sum up these individual integers into one whole number after the inputs have been sorted. =/

I suppose "if" the character is not a 0-9 then skip it, else "print", right? Maybe? Lol. I need to sum up these individual integers into one whole number after the inputs have been sorted. =/

That should do it. Maybe with some extra bits to consider what to do when you hit a # , like make a new line or something. You don't need to store or sort anything in this case (unless you want to do some actual math with them), you can just output them as you go.

Have fun :)

Do you need to convert at all? If you're not going to do any maths with the numbers then you just leave them and print them to the new file. If you do need to convert them, you can use atoi() to do it.

I agree in that I didn't understand the need to convert to int and then write it out again.

Assignment. How to convert digits into an integer. That's why. A very basic process needed by programmers. Made slightly more complex by the addition of non-digits. Another basic process called problem solving. instructors usually have a reason for their weird assignments...

If you have an array of integers:
2,4,6,8
How do you make 2468 as one integer out of it? Think LOOP. Think 10's.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.