Hi, I am trying to read into a file that has something like
I have 5 apples and 9 bananas.
Sam has 8 apples and 6 bananas.
and I need to replace the numbers with words. For example I need to change the "5" to five and so on. The problem is that im not sure how to access and then replace just the numbers. Any help will be appreciated.
#include <iostream>
#include <cmath>
#include <fstream>
#include <cstdlib>
#include <iomanip>
usingnamespace std;
int main()
{
ifstream in_stream;
in_stream.open("input.dat");
if(in_stream.fail())
{
cout<< "Input file opening failed.\n";
exit(1);
}
string line[30]; // creates SIZE empty strings
int i=0;
while(!in_stream.eof()) {
getline(in_stream,line[i]); // read the next line into the next string
++i;
}
int numLines = i;
for (i=0; i < numLines; ++i) {
cout << i << ". " << line[i]<<endl; // no need to test for empty lines any more
}
First: never loop on EOF. It can cause problems, rather loop on the getline itself: while (getline(in_stream, line[i])) { ++i; }
Now, you can probably use a map or an array or something to store the various numbers and their word counterparts, and then test to see if it is a number and replace it with the value in the map. Here is an example (assuming only 1-digit numbers):