0

I am trying to take the following code so that my bottom function first pulls out the first three scores of a text file and averages them, then pulls the last three scores out and averages them. I am using a known good text file that has 7 entries with first and last name folowed by the 6 scores followed by a return and the next entry.

Here is the current code, everything works up to the calcAveragefn

//**************************************
//IT 210 Business Applications with C++
// Programmer: Randell Wright
// Date:Oct 23, 2007
// Program:  Template Program
//**************************************


//Preprocessor Directives
#include<iostream>
#include<conio.h>
#include<iomanip>
#include<fstream>
#include<string>
using namespace std;

ifstream fin; //global variable


//Global constants/global variables/function prototypes

//declarations
  string names[40][2];
  int scores[40][6];
  int total;


//function prototypes
void headerfn();  //void function with no parameters
void inputfn(string names[40][2], int scores[40][6], int &counter);
int calcAverage(float average[40][6], string names[40][2], int scores[40][6], int &counter);



//char assignGrade(int average);


//Definition of main function

void main()
{
  headerfn();  //call to headerfn
  //start of block in main
  

float average[40][6];
  fin.open("input.txt");
  if(!fin)
  {
    cout<<"input error\npress any key"<<endl;
    getch();
	return;
    }//end of error check

    int counter;
    inputfn(names,scores, counter);

calcAverage(average, names, scores, counter);

  //end of block in main


  cout<<"\n\nPress any key to continue"<<endl;
  getch();


}//end of main

//*****************************************************************
void headerfn()
{
  cout<<"********************************************"<<endl;
  cout<<"IT210 Business Applications with C++"<<endl;
  cout<<"Programer:  Randell Wright "<<endl;
  cout<<"Date     :  October 23, 2007"<<endl;
  cout<<"Program  :  Template program"<<endl;
  cout<<"********************************************"<<endl;
}
//*****************************************************************
void inputfn(string names[40][2], int scores[40][6], int &counter)

{
int total[40];
counter=0;
 while(fin&&counter<40)
 {
   fin>>names[counter][0]>>names[counter][1];
   for(int col=0;col<6;col++)fin>>scores[counter][col];
   counter++;
   if(fin.peek()=='\n')fin.ignore();
  }//end of while

for (int row =0; row<counter; row++)
     {
     total [row]=0;
     for (int col=0; col<6; col++)
     total[row]= total [row] + scores [row][col];     }//end of outer loop

for (int row=0; row<counter; row++)     {cout<<left<<setw(20)<<names[row][0]+' '+names[row][1];
     for (int col=0; col<6;col++)       {cout<<setw(5)<<scores[row][col];}       cout<<setw(5)<<total[row];
       cout<<endl; 
    }//end of outer for
}//end of inputfn

//*****************************************************************
int calcAverage(float average[40][6], string names[40][2], int scores[40][6], int &counter)
{
counter=0;


//for (int row=0; row<counter; row++)
 //    {
     for (int col=2; col<8; col++)

	 {
	   return average[40][6];
	   cout<<average;
	 }
		 int Ptotal[40][3];
		 Ptotal =0;
	 for(int col =2; col<5;col++){Ptotal += scores[col]
	 }cout<<Ptotal;


}//end of outer for

//*****************************************************************

Thanks for the help

2
Contributors
3
Replies
4
Views
9 Years
Discussion Span
Last Post by WaltP
0

A few comments on the code:
1) Please use better and consitant formatting so the program can be understood. It help both you and us.
2) getch() and conio.h -- They are not standard and you don't need them. cin.get() is standard and does what you need.
3) void main() -- main is an int, never a void.
4) if(fin.peek()=='\n')fin.ignore(); -- what are you testing here? What if the next character is a SPACE? Probably better just to getline(fin) or fin.ignore(100,'\n')

I am trying to take the following code so that my bottom function first pulls out the first three scores of a text file and averages them, then pulls the last three scores out and averages them. I am using a known good text file that has 7 entries with first and last name folowed by the 6 scores followed by a return and the next entry.

Can't make head nor tail of your average function:

int calcAverage(float average[40][6], string names[40][2], int scores[40][6], int &counter)
{
    counter=0;
    for (int col=2; col<8; col++)     //// start a loop....  why 2 to 8?
    {
        return average[40][6];        //// first thing done in the loop is return an 
                                      //// illegal value (last value is average[39][5])
        cout<<average;                //// never reaches this line
    }
    int Ptotal[40][3];                //// Never got here, already returned.
    Ptotal =0;
    for(int col =2; col<5;col++){Ptotal += scores[col]
    }cout<<Ptotal;
}//end of outer for

Are you trying to calculate one of the two averages here? If so, just pass in the first 3 values and return the average. Call the function twice.

0

Thank you for the help, I can see where my idea of the loop went very wrong. It really helped when you commented to each line to show what starts first and where it ended. I will also try to format better.

Thanks again

A few comments on the code:
1) Please use better and consitant formatting so the program can be understood. It help both you and us.
2) getch() and conio.h -- They are not standard and you don't need them. cin.get() is standard and does what you need.
3) void main() -- main is an int, never a void.
4) if(fin.peek()=='\n')fin.ignore(); -- what are you testing here? What if the next character is a SPACE? Probably better just to getline(fin) or fin.ignore(100,'\n') Can't make head nor tail of your average function:

int calcAverage(float average[40][6], string names[40][2], int scores[40][6], int &counter)
{
    counter=0;
    for (int col=2; col<8; col++)     //// start a loop....  why 2 to 8?
    {
        return average[40][6];        //// first thing done in the loop is return an 
                                      //// illegal value (last value is average[39][5])
        cout<<average;                //// never reaches this line
    }
    int Ptotal[40][3];                //// Never got here, already returned.
    Ptotal =0;
    for(int col =2; col<5;col++){Ptotal += scores[col]
    }cout<<Ptotal;
}//end of outer for

Are you trying to calculate one of the two averages here? If so, just pass in the first 3 values and return the average. Call the function twice.

0

It really helped when you commented to each line to show what starts first and where it ended.

It really helps more when you comment your code... FYI :icon_wink:

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.