strange input/output results

Hello all, I am fairly new to C++ programming and have been struggling immensely with a program I am trying to write. Basically I have a document with words and numbers and I am trying to sift out only the numerical data from the file and output just the numerical data to a separate file. I have tried literally everything from stringstreams to buffers, yet my ouptut is not what I want... Here is the program I developed:

#include <iostream>
#include <ios>
#include <fstream>
#include <sstream>
using namespace std;

int main()
{
double a, b, c, d, e;
char type[100];
std::fstream ss;

std::ifstream in("data.txt", ios::in);
ss << in.rdbuf();

ofstream out("numbers", ios::out | ios::trunc);

ss >> type >> a >> type >> type >> type >> type >> b >> c >> d >> e;
out << a << " " << b << " " << c << " " << " " << d << " " << e;

in.close();
out.close();
return 0;
}


The following is the file input:
Run 141544 event 5Njets 0m1: pt,eta,phi,m= 231.277 0.496237 -2.22082 0.1

However, instead of isolating the numbers above, I get the output file:
2.01522e-307 8.38576e+266 7.85045e-307 1.91463e-302 -1.89701e-218

Can some please point me int he right direction? I have been banging my head on this for two days now. Again, I apologize because I am very new to C++.
Last edited on
You mean use the values from the input to do stuff? If so here's a simple example:
Input file says John 99 86 88.

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string firstName;
int midterm1, midterm2, midterm3;
double average;

ifstream inFile;
ofstream outFile;

inFile.open("C:\\text3.txt");
outFile.open("C:\\text4.txt");

if(!inFile.good())
{
cout << "Unable to open file";
}



while (inFile.good())
{
inFile >> firstName >> midterm1 >> midterm2 >> midterm3;


}
average = (midterm1 + midterm2 + midterm3)/3;
outFile << firstName << " has an average of: " << average <<endl;

inFile.close();
outFile.close();

return 0;
}
Last edited on
Thank you for the post, but mmmm... still not working, it still gives still gives me a random out put of numbers:
141544 3.63308e-317 3.47556e-308 1.44224e-306 1.97626e-323

for some reason though the first number prints, but none of the next in the input series:
Run 141544 event 5Njets 0m1: pt,eta,phi,m= 231.277 0.496237 -2.22082 0.1

Any other suggestions would be much appreciated.
Topic archived. No new replies allowed.