•
•
•
•
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
![]() |
•
•
Join Date: Dec 2007
Posts: 2
Reputation:
Rep Power: 0
Solved Threads: 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
Thanks for the help
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
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)
3)
4)
Can't make head nor tail of your average function:
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.
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.
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 Age is unimportant -- except in cheese
•
•
Join Date: Dec 2007
Posts: 2
Reputation:
Rep Power: 0
Solved Threads: 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
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()andconio.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 togetline(fin)orfin.ignore(100,'\n')
Can't make head nor tail of your average function:
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.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
![]() |
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- Finding the average of numbers (C++)
- How can I get Highest, Lowest and Average ? (C++)
- Getting a Grade Average (C++)
- Help! Errors in program (average characters) (C++)
Other Threads in the C++ Forum
- Previous Thread: How to use ACE logging.
- Next Thread: Help With Pascal's Triangle in C++



Linear Mode