Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

can someone help me fix the errors in my program code i cant seem to find them #

ID: 653200 • Letter: C

Question

can someone help me fix the errors in my program code i cant seem to find them

#include <iostream>
#include <fstream>
#include <cstring>
#include <iomanip>
#include <string>

using namespace std;

struct student
{
   string fname;
   string lname;
   char course;
   int test1;
   int test2;
   int finalex;
   double testavg;
};

double printresults(double);
char grade(char);

   struct student s[10];

int main()
{
   int i;
   ifstream inf;
   ofstream out1;
   char test1[100];
   char outputtext[100];
   double a;
   char b;

     
   do
   { inf.clear();
       cout << "Please enter the name of the input file"<< endl;
       cin >> test1;
       inf.open(test1);
       if (!inf)
           cout << "That is not a valid file"<< endl;
   }while(!inf);

   for( i=0; i<10; i++)
   {
       inf>>s[i].fname;
       inf>>s[i].lname;
       inf>>s[i].test1;
       inf>>s[i].test2;
       inf>>s[i].finalex;
       inf>>s[i].testavg; }


   do
   {
       cout << "Please enter the name of the output file" << endl;
       cin >> outputtext;

       out1.open(outputtext);
           if (!out1)
               cout << "That is not a valid file name" << endl;
   } while (!out1);


   out1 << "Student Grade Summary" << endl;

   out1 << "English Class" << endl;
   out1 << " Student Name Test Avg" << endl;

   if(s[i].course == 'E' || s[i].course =='e')
       out1 << s[i].fname << s[i].lname << ' ' << printresults(a) << grade(b) << endl;
  

   out1 << "History Class" << endl;
   out1 << "Student Name Test Avg" << endl;

   if(s[i].course =='H' || s[i].course == 'h')
       out1 << s[i].fname << s[i].lname << '/t' << printresults(a) << grade(b) << endl;

   out1 << "Math Class" << endl;
   out1 << "Student Name Test Avg" << endl;

   if(s[i].course == 'M' ||s[i].course == 'm')
       out1 << s[i].fname << s[i].lname << '/t' << printresults(a) << grade(b) << endl;

   inf.close();
   out1.close();

   return 0;
}

   double printresults(double testavg)
{     
   double testavg = 0;
   testavg = (s[i].test1 + s[i].test2)/2;
   return testavg;
   }

   char grade( char studentg)
{
   double testavg;

   if(testavg >= 90)
       return studentg = 'A';
   else if(testavg >= 80 && testavg < 90)
       return studentg = 'B';
   else if(testavg >= 70 && testavg < 80)
       return studentg = 'C';
   else if(testavg >= 60 && testavg < 70)
       return studentg = 'D';
   else(testavg < 60);
       return studentg = 'F';   

   }

Explanation / Answer

You are There almost but missed simple thing.You are accessing the 11 th element which has idex 10 from array s[]

array has length 10 but index 9 only ,but here you are trying to access 11th element i.e index 10 which is index out of bounds...

so make it to

for( i=0; i<9; i++)
   {
       inf>>s[i].fname;
       inf>>s[i].lname;
       inf>>s[i].test1;
       inf>>s[i].test2;
       inf>>s[i].finalex;
       inf>>s[i].testavg; }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote