I am having trouble with the formatting of my C++ assignment. I am working on a File I/O assignment and I have a text file with letters, numbers and whitespace. I want my program to read the numbers from the text file and display in the same format as the text file. Right now, my code is outputting the numbers but as a line instead of number values.
Here is the text file:
dsfj kdf 87 98.5 4vb
jfkd 9 jfkds000 94.3hjf
98.4 jd84. kfd
Here is the desired output:
We found the following magic numbers in test2.txt:
87 98.5 4 9 0 94.3 98.4 84
Here is my code:
#include <iostream>
#include <fstream>
using namespace std;
int main() {
char ans;
cout << "Welcome to Number Finder!\n";
cout << "============================\n";
do{
char in_file_name[20];
char c;
ifstream in_stream;
cout << "Which file do you want us to find numbers at?\n";
cin >> in_file_name;
cout << endl;
cout << "We found the follow magic numbers in " << in_file_name << ": \n";
in_stream.open(in_file_name);
if(in_stream.fail()){
cout << in_file_name << " is not found.\n";
}
in_stream.get(c);
while (!in_stream.eof()){
if(isdigit(c)){
cout << fixed;
cout.precision(2);
cout << c << " ";
}
in_stream.get(c);
}
double num;
while(in_stream >> num){
cout << fixed;
cout.precision(2);
cout << num << " ";
}
cout << endl;
cout << "-----";
cout << endl;
cout << "Do you want to process another file? \n";
cin >> ans;
in_stream.close();
} while (ans== 'Y' || ans=='y');
cout << "Thank you for using the Number Finder! Bye for now!";
return 0;
}