I'm not understanding files!

Hello all,

thanks for over looking my code. I'm very new to programming and I really want to understand files. My knowledge of them is just the 40 pages of my text book that I read.

Basically, I'm curious why nothing besides "Processing Data" is being displayed. I'm very aware it's because it says cout. I'm wanting to know how I can get prompted to enter test scores for students. I want to be able to enter student data in my program and then it will return the average. I hope that makes sense and maybe you can help me, thank you!

2 // Practice using files
3
4 #include <iostream>
5 #include <fstream>
6 #include <iomanip>
7 #include <string>
8
9 using namespace std;
10
11 int main ()
12
13 {
14 ifstream inFile;
15 ofstream outFile;
16
17 double test1, test2, test3, test4, test5;
18 double average;
19
20 string firstName;
21 string lastName;
22
23 inFile.open("test.txt");
24 outFile.open("testavg.out");
25
26 outFile << fixed << showpoint;
27 outFile << setprecision(2);
28
29 cout << "Processing Data" << endl;
30
31 inFile >> firstName >> lastName;
32 outFile << "Student name: " << firstName
33 << " " << lastName << endl;
34
35 inFile >> test1 >> test2 >> test3
36 >> test4 >> test5;
37 outFile << "Test scores: " << setw(6) << test1
38 << setw(6) << test2 << setw(6) << test3
39 << setw(6) << test4 << setw(6) << test5
40 << endl;
41
42 average = (test1 + test2 + test3 + test4 +test5) / 5.0;
43
44 outFile << "Average test score: " << setw(6)
45 << average << endl;
46
47 inFile.close();
48 outFile.close();
49
50 return 0;
51 }
That's easy. Just because all the words except "Processing Data" was written into outFile instead of cout. Words that is written into files won't be showen on screen.
Just change all outFile to cout. If you want to write them both into files and onto screen, insert them into a stringstream and insert it to both outFile and cout.
Topic archived. No new replies allowed.