2d array: File I/O

Hi,

The program below takes input from user the marks of 10 students in 6 subjects
and gives output as:

Maths Phy Chem Bio C.Sc Eng Tot Stud_Avg
studentname1    10   20   30  40   50  60  210   35
studentname2    20   30   40  50   60  70  270   45
   --
   --
studentname10   30   40   50  60   70  80  330   55


Issue faced:
the program executes and outputs the above in the output window. It,however, does not write to and read from a file in the same manner...just displays the 6 subject names on the file:-)

i've tried file i/o using object sfil of classes ofstream & ifstream. Is this correct? Could any of the experts have a look at the program & give a solution....would be of much help.

Also, apologize for the lengthy program as i'm new to c++.

thank you for your patience,
Sudhir


//compiler used turbo c++

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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include<iostream.h>
#include<fstream.h>
#include<conio.h>
class StudList
      {
         char stud_name[10][20],ans;
         char subject[6][20];
         int stud_marks[10][6],sum[10],i,j,num,sub;
         float studavg[10],subavg[6];
         public:
         void getinp()
            {
               for(ans='y',i=0;i<10&&ans=='y';i++)
	     {
	       cout<<"Enter student name"<<i+1<<endl;
	       cin>>stud_name[i];
	       cout<<"More ? (y/n)"<<endl;
	       cin>>ans;
	      }
	    for(ans='y',j=0;j<6&&ans=='y';j++)
	      {
	       cout<<"Enter subject"<<j+1<<endl;
	       cin>>subject[j];
	       cout<<"More ? (y/n)"<<endl;
	       cin>>ans;
	      }
	   num=i;
	   sub=j;
            }
           void acceptmarks()
             {
                for(i=0;i<num;i++)
	     {
	      for(j=0;j<sub;j++)
	            {
		stud_marks[i][j]=0;
		cout<<"Enter marks for "<<stud_name[i]<<" in "<<subject[j<<endl;
		cin>>stud_marks[i][j];
	            }
	     }
              }
            void calc_totmarks()
              {
	   for(i=0;i<num;i++)
	        {
	           sum[i]=0;
	           for(j=0;j<sub;j++)
		 {
		  studavg[i]=0;
		  sum[i]+=stud_marks[i][j];
		  studavg[i]+=(float)sum[i]/sub;
		 }
	         }
               }
             int disp_records()
                {
	   cout<<"       Maths Phy Chem Bio C.Sc Eng Total Stud_Avg"<<endl;
	   for(i=0;i<num;i++)
	       {
	         cout<<stud_name[i]<<" ";
	         for(j=0;j<sub;j++)
	              {
		if((stud_name[i]==stud_name[j])||(i!=j))
		cout<<stud_marks[i][j]<<"   ";
	              }
	          cout<<sum[i]<<"  ";
	          cout<<studavg[i]<<endl;
	        }
	   return 0;
	}
  };
int main()
  {
   ofstream sfil("prog97.txt");
   clrscr();
   StudList obj;
   obj.getinp();
   obj.acceptmarks();
   obj.calc_totmarks();
   sfil<<"Student Database:"<<endl;
   sfil<<"-------------------------\n"<<endl;
   sfil<<"       Maths Phy Chem Bio C.Sc Eng Total Stud_Avg"<<endl;
   sfil<<obj.disp_records();
   sfil.close();
   cout<<"Do you want to view the contents of the file(y/n)?"<<endl;
   char choice;
   if(choice=='Y'||choice=='y')
     {
       ifstream sfil("prog97.txt");
       ifstream in("prog97.txt");
       char str1[21],str2[31];
       char stdnm1[21],stdnm2[21],stdnm3[21],stdnm4[21],stdnm5[21],stdnm6[21];
       char stdnm7[21],stdnm8[21],stdnm9[21],stdnm10[21];
       char sub1[11],sub2[11],sub3[11],sub4[11],sub5[11],sub6[11];
       char tot[11],avg[11];
       int mrks1,mrks2,mrks3,mrks4,mrks5,mrks6,mrks7,mrks8,mrks9,mrks10;
       int mrks11,mrks12,mrks13,mrks14,mrks15,mrks16,mrks17,mrks18,mrks19,
           mrks20;
       int mrks21,mrks22,mrks23,mrks24,mrks25,mrks26,mrks27,mrks28,mrks29,
           mrks30;
       int mrks31,mrks32,mrks33,mrks34,mrks35,mrks36,mrks37,mrks38,mrks39,
           mrks40;
       int mrks41,mrks42,mrks43,mrks44,mrks45,mrks46,mrks47,mrks48,mrks49,
           mrks50;
       int mrks51,mrks52,mrks53,mrks54,mrks55,mrks56,mrks57,mrks58,mrks59,
           mrks60;
       int tot1,tot2,tot3,tot4,tot5,tot6,tot7,tot8,tot9,tot10;
       float avg1,avg2,avg3,avg4,avg5,avg6,avg7,avg8,avg9,avg10;
       in.get(str1,21);
       in.get(str2,31);
       in.get(sub1,11);
       in.get(sub2,11);
       in.get(sub3,11);
       in.get(sub4,11);
       in.get(sub5,11);
       in.get(sub6,11);
       in.get(tot,11);
       in.get(avg,11);
       in.get(stdnm1,21);
       sfil>>mrks1;
       sfil>>mrks2;
       sfil>>mrks3;
       sfil>>mrks4;
       sfil>>mrks5;
       sfil>>mrks6;
       sfil>>tot1;
       sfil>>avg1;
       in.get(stdnm2,21);
       sfil>>mrks7;
       sfil>>mrks8;
       sfil>>mrks9;
       sfil>>mrks10;
       sfil>>mrks11;
       sfil>>mrks12;
       sfil>>tot2;
       sfil>>avg2;
       in.get(stdnm3,21);
       sfil>>mrks13;
       sfil>>mrks14;
       sfil>>mrks15;
       sfil>>mrks16;
       sfil>>mrks17;
       sfil>>mrks18;
       sfil>>tot3;
       sfil>>avg3;
       in.get(stdnm4,21);
       sfil>>mrks19;
       sfil>>mrks20;
       sfil>>mrks21;
       sfil>>mrks22;
       sfil>>mrks23;
       sfil>>mrks24;
       sfil>>tot4;
       sfil>>avg4;
       in.get(stdnm5,21);
       sfil>>mrks25;
       sfil>>mrks26;
       sfil>>mrks27;
       sfil>>mrks28;
       sfil>>mrks29;
       sfil>>mrks30;
       sfil>>tot5;
       sfil>>avg5;
       in.get(stdnm6,21);
       sfil>>mrks31;
       sfil>>mrks32;
       sfil>>mrks33;
       sfil>>mrks34;
       sfil>>mrks35;
       sfil>>mrks36;
       sfil>>tot6;
       sfil>>avg6;
       in.get(stdnm7,21);
       sfil>>mrks37;
       sfil>>mrks38;
       sfil>>mrks39;
       sfil>>mrks40;
       sfil>>mrks41;
       sfil>>mrks42;
       sfil>>tot7;
       sfil>>avg7;
       in.get(stdnm8,21);
       sfil>>mrks43;
       sfil>>mrks44;
       sfil>>mrks45;
       sfil>>mrks46;
       sfil>>mrks47;
       sfil>>mrks48;
       sfil>>tot8;
       sfil>>avg8;
       in.get(stdnm9,21);
       sfil>>mrks49;
       sfil>>mrks50;
       sfil>>mrks51;
       sfil>>mrks52;
       sfil>>mrks53;
       sfil>>mrks54;
       sfil>>tot9;
       sfil>>avg9;
       in.get(stdnm10,21);
       sfil>>mrks55;
       sfil>>mrks56;
       sfil>>mrks57;
       sfil>>mrks58;
       sfil>>mrks59;
       sfil>>mrks60;
       sfil>>tot10;
       sfil>>avg10;
     }
   getch();
return 0;
}
Last edited on
Can't you find a way to put all those marks in an array or something? With so many variable lying around, how can you be sure you've not made a mistake?

Are you familiar with struct? If you are, you should consider using an array of structs of stuff withing the class.

In your code:
1
2
	char choice;
	if(choice=='Y'||choice=='y')

choice is used before it's assigned a value.

EDIT: Can you use the code format tags for your code.
Last edited on
once again, apologies for the cluttered and lengthy code (i've already mentioned my newbie status).....in the meanwhile will try arrays and revert.

As regards the variable "choice", should the code be modified to

Original:
1
2
3
cout<<"Do you want to view the contents of the file(y/n)?"<<endl;
   char choice;
   if(choice=='Y'||choice=='y')



Modified:
1
2
3
4
   char choice;
   cout<<"Do you want to view the contents of the file(y/n)?"<<endl;
   cin>>choice;
   if(choice=='Y'||choice=='y')


to take appropriate user input? If yes, it does not change the output on the file [still only the 6 subject names displayed without the student names, marks etc...]
Topic archived. No new replies allowed.