Hi,

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


[output] 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[/output]

Issue faced:
the program executes and displays 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:-)

Apologies for the lengthy code (as i'm new to c++)......i've tried using array of structs (albeit unsuccessfully, not sure where to declare/initialize/call etc. for writing to/reading from file) to reduce the variables declared for reading from file.

Would much appreciate help from the expert(s) or any of the members.

thank you for your patience,
sudhir


//compiler used turbo c++

#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;
}

Recommended Answers

All 5 Replies

is your program stored in the same directory of as the program - you also need to give it the root of where the file is

93-94 can be condensed into: stdnm[10][21]; 95 sub[6][11]; 97-107 mrks[60]; You can simply it's usage with loops such as, for(int i = 0; i < 60; i++) sfil>>mrks[i]; See how that works out?

Thanks for your reply mosaic. what about the student total (tot) and average (avg) variables....should they also be condensed into for loops as suggested, say tot[j] and avg[j]?

sudhir

You could give it a try.

Still not getting the desired output. maybe not setting n reading the arrays in the for...loops properly. will revert once successful. if there's any alternate way, kindly suggest.

thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.