Hello! I need a little more help. I have managed to convert all my chars input from a text file into digits.
Example:
Input from file:
$1,9,56#%34,9
!4.23#$4,983
Output:
1956
349
423
4983
Now, I need to take those individual digits the 1 9 5 6 and make it read as a whole number. The output would look the same but they would actually be whole numbers. Make sense? I have to do this in my outer loop. It also has to be an EOF loop. So, I know I need to take the first digit and multiply it by 10 and add the next digit then multiply all that by 10 until I reach the last number. How can I write that in an efficient non-crashing way?
I attached my input .txt file if you need to see it.
This is what I have so far...

THANK YOU SOOOO MUCH!!!

/*
 */
 
 //Character Processing Algorithm
 
 #include <fstream>
 #include <iostream>
 #include <cctype>                                                                                                                         
 using namespace std;
 
 char const nwln = '\n';
 
  int main ()
   {
     ifstream data;
     ofstream out;
     char ch;
     char lastch;
     int sum;
          
     data.open ("lincoln.txt"); //file for input
     if (!data)
        {
        cout << "Error!!! Failure to Open lincoln.txt" << endl;
        system ("pause");
        return 1;
        }
     out.open ("out.txt"); //file for output
     if (!out)
        {
        cout << "Error!!! Failure to Open out.txt" << endl;
        system ("pause");
        return 1;
        }
        
     data.get (ch); // priming read for end-of-file loop
     
     while (data)
           {
           sum = 0;
           while ((ch != nwln) && data)
                 {
                 if (isdigit(ch))
                    out<<ch;
                 
                 if (ch == '#')
                    out<<endl;
                 {
                 ;
                 }
              
                                  lastch = ch;
                 data.get (ch); // update for inner loop
                 } // inner loop
                 if (lastch != '#')
                     out<<endl;
                 
           
              
              data.get (ch); // update for outer loop
              
           } //outer loop
           
     cout << "The End..." << endl;
     data.close (); out.close ();
     system ("pause");
     return 0;
     } //main

Recommended Answers

All 7 Replies

Well there is a relatively simple method for this. You can take your filtered text input as character string and make those into c++ strings, then append them into a string stream (like how you can to cout), then read them back like you can from cin.

assuming you have a character string chStr the following code should show you the concept

//in your includes 
#include <sstream>
#include <string>

//in your code 
//build the string from the cstring you had
std::string convert(chStr); 

std::stringstream ss;

//append strings to ss
ss<<convert;

//get real numbers back
int realNum;
ss>>realNum;

Of course you will have to bend that code to fit your application but its a betetr learning expierence for you now you have a plan of attack for how to achieve your goal.

Hope this helps

I need to take those individual digits the 1 9 5 6 and make it read as a whole number.

I remember this from before, but you'll have to clarify what you mean by "make it read a whole number." Like I had said on the other thread, you need to keep track of how many characters the number consists of and multiply by 10 ^ (power -1), etc.

e.g.,

1%4$%5#
//how many are digits? Determine that 3 are
//unget the last 6 characters
//multiply 1 by 10 ^(3-1) = 10^2, add it to a running sum
//march along to 4, multiply it by 10^(3-1-1) = 10^1, add to the same running sum
//march along to 5, multiply it by 10^(3-1-1-1) = 10^0,add to the same running sum

since 145 = 1*10^2 + 4*10^1 + 5*10^0
It's a convoluted way to do it (as writing out the characters as you read them is far easier), but it sounds like that's what your instructor wants.

It shouldn't be hard. Read the input as a string. And move everything thats not a number to the right, and everything that is a number to the left. and output.

I think (from another thread with the OP) that the get() (and perhaps unget())is the only thing allowed. I agree that there are many other easier ways to do this.

I think (from another thread with the OP) that the get() (and perhaps unget())is the only thing allowed. I agree that there are many other easier ways to do this.

In that case, if it hasn't been suggested, you can get the character if it is digit, else he can discard it. Even easier.

You would still need 2 passes, once to count how many digits there are to establish the place value for the first digit, and another to read in the digits and do the multiplication.

If all he needs to do is output it then he can simply use string concatenation.

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.