Go Back   CodingForums.com > :: Computing & Sciences > Computer Programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-04-2009, 11:27 PM   PM User | #1
hastefan2001
New to the CF scene

 
Join Date: Jul 2009
Location: alabama
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
hastefan2001 is an unknown quantity at this point
arrays

well I am new to the programming world i have been working out of a programming book and in the book it has asked me to' build an array to find average of a text and i have tried several things but cant seem to get it right and i keep getting the following errors and cant really pinpoint what the errors are and what i should do

Code:
#include <iostream>
#include <fstream> // I/O
#include <iomanip> // For setw()
using namespace std;


ofstream outputfile("output.txt");
const int MAX_FILE_NAME = 35; // Maximum file name length
const int MAX_NUMBER_SCORES = 40;  // Maximum number of values in array
void open_input(ifstream& input, char name[]); // Get file name & Open file
void read_values(ifstream& input, double v[], int size, int& used);  // Read values from file
double find_average(ifstream& input, double& average); // Find avg values
void output(const char name[], const double v[], int n, double find_average, ostream& os = cout); // Print results
int main()
// Parameters: None
// Returns: Zero
// Calls: open_input(), find_average(), output()
{ char again; // Does user want to go through loop again?
char file_name[MAX_FILE_NAME + 1]; // Name of file to be processed
ifstream input_numbers; // For working with input file

double average = 0.0;
int num_scores = 0;                   // Number of scores in file

cout << "This program can find the average numbers in a file\n" << endl;
system("pause"); // Hold message on screen until key is pressed

do
{
//system("cls"); // Clear screen
open_input(input_numbers, file_name); // Get file name & open file

find_average(input_numbers,   average); // Find average values in file
read_values(input_numbers, scores, MAX_NUMBER_SCORES, num_scores);       // Read values
output(file_name, num_scores,  average );
output(file_name, num_scores,  average, outputfile); // and outputfile

input_numbers.close(); // Close file
if (num_scores > 0)
{ find_average(scores, num_scores, average); // Find average of values in array
}
      else
      {  cout << "\n\n\aNo data in file: " << file_name << endl;
      }
cout << "\nDo you want to process another file (Y/N)? ";
cin >> again;
cin.ignore(256, '\n'); // Remove Enter key from keyboard buffer

} while ( again == 'y' || again == 'Y');

cout << "\nEnd of Program!" << endl;


return 0;
} // End of main()

void open_input(ifstream& input, char name[]) //Open file, exit on error
// Parameters: Variables for input file reference and input file name
// Returns: None
// Calls: None
{ int count = 0; // Count number of tries

do // Continue until we get a valid file name and can open file
{
count++;

if (count != 1) // Issue error message if we are trying again.
{ cout << "\n\aInvalid file name or file does not exist. Please try again."
<< endl;
}

cout << "\nEnter the input file name (maximum of " << MAX_FILE_NAME
<< " characters please)\n:> ";
cin.get(name, MAX_FILE_NAME + 1);// Gets at most MAX_FILE_NAME characters
cin.ignore(256, '\n'); // Remove Enter key from keyboard buffer

input.clear(); // Clear all error flags, if any, from prev try
input.open(name, ios_base::in); // Open only if file exists

} while (input.fail() ); // If can't open file, try again
} // End of open_input()

void read_values(ifstream& input, double v[], int size, int& used) // Read values
// Parameters: Variables for file reference, variable for array reference,
//             value for array size and variable for number of values in array
// Returns:    None
// Calls:      None
{  
   double value;        // Value from file
   int count = 0;       // Count number of values in file

   while (count < size && input >> value) // Continue as long as there is 
                                          // room in the array and we can read
   {  v[count] = value;                   // a number from file.
      count ++; 
   }

   used = count;
} // End of read_values()

void find_average(const double v[], int n, double& average) // Find average of values
// Parameters: Variables for array reference, value for number of values 
//             and variables for average of values
// Returns:    None
// Calls:      None
{  
   int i;   // Array index and loop counter
   
   average = find_average = v[0];
   
   for (i = 1; i < n; i++) // Start with 1 since max & min initialized to v[0]
   {  
      if (v[i] > average) average = v[i];
      if (v[i] < average) average = v[i];
   }
} // End of find_average()

double find_average(ifstream& input, double& average) // Find max & min values
// Parameters: Variables for file reference and max and min values
// Returns: None
// Calls: None
{
double avag;

double sum = 0;
int count = 0;

while (input >> avag) // Continue as long as we can read a number from file.
{
sum += avag;
count = count + 1;
}
average = sum/count;
return sum/count;
} // End of average file

void output(const char name[], double average, ostream& os) // Print results
// Parameters: File name, max & min values from file, output stream
// Returns: None
// Calls: None
{ os << "\n\nInput your File Name Please : " << name << endl;
os << "Your average : " << setw(8) << average<< endl;

}
void output(const char name[], const double v[], int n, 
			double max, double min, ostream& out) 
// Parameters: File name, array reference, number of values of average of values
// Returns:    None
// Calls:      None
{  int i;  // Array index and loop counter
   out.setf(ios::fixed);
   out.setf(ios::showpoint);
   out.precision(1);
   out << "\n\nInput File Name:         " << name << endl;
   out << "\nFile Contents:" << endl;
   for (i = 0; i < n; )
   {  out << setw(8) << v[i];
      if ( ++i % 5 == 0) out << "\n"; // Print only 5 values per line
   }
   if ( i % 5 != 0) out << "\n";      // Newline if last line is short
   out << "\nLargest  Number in File:        " << setw(8) << max << endl;
   out <<   "Smallest Number in File:        " << setw(8) << min << endl;
} // End of output()
and here are my errors that i am recieving:

(40) : error C2065: 'scores' : undeclared identifier
(41) : error C2660: 'output' : function does not take 3 arguments
42) : error C2664: 'output' : cannot convert parameter 2 from 'int' to 'const double []'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
(115) : error C2659: '=' : function as left operand

if you can help me fix this program to where it is functionalable i would appreciate it

although i have tried several thing i can seem to get any of the things i do to get rid of the errors that i am getting
hastefan2001 is offline   Reply With Quote
Old 07-05-2009, 01:12 AM   PM User | #2
tomws
Senior Coder

 
tomws's Avatar
 
Join Date: Nov 2007
Location: Arkansas
Posts: 2,644
Thanks: 29
Thanked 330 Times in 326 Posts
tomws will become famous soon enoughtomws will become famous soon enough
Go to the line numbers listed (in parentheses) and fix the given problems. The errors are fairly straightforward.
Quote:
Originally Posted by hastefan2001 View Post
(40) : error C2065: 'scores' : undeclared identifier
That means you're using a variable "scores" on line 40 that you've not previously declared/defined - just like it says. Easy.
Quote:
Originally Posted by hastefan2001 View Post
(41) : error C2660: 'output' : function does not take 3 arguments
That means the function named "output" that you're using on line 41 does not accept 3 arguments - just like it says. Easy.
Quote:
Originally Posted by hastefan2001 View Post
42) : error C2664: 'output' : cannot convert parameter 2 from 'int' to 'const double []'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
The second function parameter to that function is expected to be a constant double.
Quote:
Originally Posted by hastefan2001 View Post
(115) : error C2659: '=' : function as left operand
Not sure on this one since the code doesn't paste over correctly to my editor, apparently. Fix the others, isolate this problem, and post the relevant section of code.
__________________
Are you a Help Vampire?
tomws is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:28 PM.


Advertisement
Log in to turn off these ads.