User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 455,985 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,759 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 611 | Replies: 3
Reply
Join Date: Dec 2007
Posts: 2
Reputation: rlwright12 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
rlwright12 rlwright12 is offline Offline
Newbie Poster

Average help

  #1  
Dec 3rd, 2007
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
AddThis Social Bookmark Button
Reply With Quote  
Join Date: May 2006
Posts: 2,779
Reputation: WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold 
Rep Power: 15
Solved Threads: 229
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Maven

Re: Average help

  #2  
Dec 4th, 2007
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')

Originally Posted by rlwright12 View Post
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.
Age is unimportant -- except in cheese
Reply With Quote  
Join Date: Dec 2007
Posts: 2
Reputation: rlwright12 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
rlwright12 rlwright12 is offline Offline
Newbie Poster

Re: Average help

  #3  
Dec 4th, 2007
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

Originally Posted by WaltP View Post
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.
Reply With Quote  
Join Date: May 2006
Posts: 2,779
Reputation: WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold 
Rep Power: 15
Solved Threads: 229
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Maven

Re: Average help

  #4  
Dec 4th, 2007
Originally Posted by rlwright12 View Post
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
Age is unimportant -- except in cheese
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 9:28 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC