creating and writing to a file

How do I create and write to the file that i will call "p6.dat"?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
  #include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
   const int STUDENTS = 5;
   const int TESTS = 3;
   int scores[STUDENTS][TESTS];

   ifstream infile;
   string fName;

   cout << "Enter the file name: ";
   cin >> fName;

   infile.open(fName);

   for(int i = 0; i < STUDENTS; i++)
   {
       for(int j = 0; j < TESTS; j++)
       {
           infile >> scores[i][j];
       }
   }

   cout << endl;
   for(int i = 0; i < STUDENTS; i++)
   {
       double sum = 0;
       for(int j = 0; j < TESTS; j++)
       {
           sum += scores[i][j];
       }

       double avg = sum / 5.0;
       printf("%.2f ", avg);
   }

   cout << endl;

   for(int j = 0; j < TESTS; j++)
   {
       double sum = 0;
       for(int i = 0; i < STUDENTS; i++)
       {
           sum += scores[i][j];
       }

       double avg = sum / 5.0;
       printf("%.2f ", avg);
   }

   cout << endl;
   int high = scores[0][0];
   int count90s = 0;

   for(int i = 0; i < STUDENTS; i++)
   {
       for(int j = 0; j < TESTS; j++)
       {
           if(high < scores[i][j])
               high = scores[i][j];

           if(scores[i][j] >= 90)
               count90s++;
       }
   }

   cout << high << endl;
   cout << count90s << endl << endl;

   system("pause");
   return 0;
}

It isn't letting me write to the file that i need to. what am i doing wrong?
@jpengineer

For one, you don't have the correct syntax. Try it this way..
1
2
3
4
5
6
7
8
9
10
ofstream outfile(fname);
for (int i = 0; i<STUDENTS; i++)
{
	for(int j = 0; j < TESTS; j++)
        {
               outfile <<  scores[i][j] << " "; 
	}
outfile << endl;
}
outfile.close();
Why can't i enter the integers for the array to the file? what is wrong?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
 #include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
	int tests = 3;
	int students = 5;
	int scores[tests][students];
	string filename;
	ofstream outputFile;


	cout<<"Please enter the file name: ";

	outputFile.open(filename);


	cout<<"Please enter the scores:";



	for (int i = 0; i<students; i++)
	{
		for(int j = 0; j < tests; j++)
	        {
	               outputFile <<  scores[i][j] << " ";
		}
	outputFile << endl;
	}
	outputFile.close();



   system("pause");
   return 0;
}
You should fill the array BEFORE saving the file. I see no cin >> scores[i][j] anywhere. Also, you seemed to have flipped the variables in the scores definition from int scores[STUDENTS][TESTS] to int scores[tests][students]. First, you had 5 groups of 3, now you have 3 groups of 5.
Where would a cin>> go? i thought thats what the outputFile<<scores did in the loop? and when am i saving the file in here?
No, the outputfile ONLY saves the data to a file. You have to add the two loops with a cin to fill the array.
ie:

1
2
3
4
5
6
7
for (int i = 0; i<students; i++)
{
	for(int j = 0; j < tests; j++)
        {
              cin  >>  scores[i][j] << " ";
	}
}
oh ok, that makes sense. Would i just put that loop after i ask them to enter the scores? I tried that and it gave me an odd error
@jpengineer

You should ask for the scores, and fill them, BEFORE you request the filename, open and save the file.

ie:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
cout<<"Please enter the scores:";
for (int i = 0; i<students; i++)
{
	for(int j = 0; j < tests; j++)
        {
              cin  >>  scores[i][j] << " ";
	}
}

ofstream outputFile;

cout<<"Please enter the file name: ";

cin >> filename;
outputFile.open(filename);

for (int i = 0; i<students; i++)
{
	for(int j = 0; j < tests; j++)
        {
               outputFile <<  scores[i][j] << " ";
	}
outputFile << endl;
}
outputFile.close();
Last edited on
Topic archived. No new replies allowed.