/*****You need the files goodsrc.dat and badsrc.dat to run this program.****/ // This program illustrates inputting data from a file using ifstream. // Note the header . The file badsrc.dat produces an unexpected // result because the second item listed in the file is a double type // constant, while the program expects the variable j to be int type. // The moral is to check to make sure that the types of the input data // and the variables used in the program match. #include #include void main() { double x; int i,j; ifstream infile1("goodsrc.dat"); ifstream infile2("badsrc.dat"); infile1 >> i; infile1 >> j >> x; infile1.close(); cout << "From goodsrc.dat, i = " << i << ", j = " << j << ", x = " << x << endl; infile2 >> i; infile2 >> j >> x; infile2.close(); cout << "From badsrc.dat, i = " << i << ", j = " << j << ", x = " << x << endl; }