I/O using string failed so many times

hi guys i have been using c++forum for quick review some times but never had an intend to register until today .. so guys i really need help on inputting file that have string and double .

Here are my coding :

#include <iostream>
#include <fstream>
#include <cstring>
#include <string>


using namespace std;

int main (void)
{ string name="";
double first,second,third,fourth;
double avg;

ifstream in ("department.dat");
ofstream out1("budget1.dat");
ofstream out2("budget2.dat");
if(! in )
{
cout<<"File doesnt exist!"<<endl;
exit(-1);
}


while( std::getline(in , name))
{

in>>first>>second>>third>>fourth;


cout<<name<<" "<<first<<" "<<second<<" "<<third<<" "<<fourth<<endl;


avg = (first+second+third+fourth)/4;
if ( avg > 1000.00 )
{
out1<<name<<"\t"<<first<<"\t"<<second<<"\t"<<third<<"\t"<<fourth<<endl;
}
else if ( avg < 1000.00 )
{
out2<<name<<"\t"<<first<<"\t"<<second<<"\t"<<third<<"\t"<<fourth<<endl;
}
}
// out>> ... dkat luar


system("pause");
}


Here is the txt file of input ( department.dat):
Administration 1590.45 1231.50 1005.10 1500.00
Computer Science 2190.30 956.80 745.10 2000.00
System Science 894.90 479.50 214.96 900.00

*note that i separated it with a space .

P.S : I could run it but i have a some sort of garbage value
Last edited on
So, what's the problem? Looking at the code, I can guess what it is, but you didn't actually ask a question so far.
im trying to get data from my text file which have space in it for example : Computer Science ..
You should try to read it as a number first, and if it fails then try reading it as a string instead. Remember to clear the error state before trying again.
Topic archived. No new replies allowed.