C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 06-29-2009, 11:55 AM   #1
Registered User
 
Join Date: Jun 2009
Posts: 3
help in finding average of a class of numbers

well i have this program that i have written error free but my return keeps coming out wrong can anyone help me resolve this issue. I am including my source code.


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


const int MAX_FILE_NAME = 35; // Maximum space allocated for file name

void open_input(ifstream& input, char name[]); // Get file name & Open file
double find_average(ifstream& input, double& average); // Find avg values
void output(const char name[], double find_average, ostream& os = cout); // Print results

int main()
// Parameters: None
// Returns: Zero
// Calls: open_input(), find_max_min(), 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;
double avag = 0;
// Maximum and minimum numbers from file

cout << "This program can find the average numbers in a file\n"
<< "of doubles.\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

input_numbers.close(); // Close file
output(file_name, avag);



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()

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 value;

double sum = 0;
int count = 0;

while (input >> value) // Continue as long as we can read a number from file.
{
average = sum/count;
sum += value;
count = count + 1;
}
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;

}
hastefan2001 is offline   Reply With Quote
Old 06-29-2009, 12:04 PM   #2
3735928559
 
Join Date: Mar 2008
Posts: 632
ican'treadyourcodebecauseyouhaven'tindenteditatall .
m37h0d is offline   Reply With Quote
Old 06-29-2009, 12:08 PM   #3
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
I now have a headache, but I read enough of your code to discover that you do calculate the average correctly, but don't care about it enough to actually keep it, instead discarding it before your print statement. You have to assign the return value of your function call to avag, if you want avag to actually be the average.
tabstop is offline   Reply With Quote
Old 06-29-2009, 12:31 PM   #4
Registered User
 
Join Date: Jun 2009
Posts: 3
Quote:
Originally Posted by tabstop View Post
I now have a headache, but I read enough of your code to discover that you do calculate the average correctly, but don't care about it enough to actually keep it, instead discarding it before your print statement. You have to assign the return value of your function call to avag, if you want avag to actually be the average.
ok so i changed my return function to (return avag) and go a successful build but i still didnt get a correct average in return i am still getting a 0 average and i just need to know where that function call needs to be changed at in order to get the correct average that i am looking for in this program. so here is what i have so far after i change that function call

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


const int MAX_FILE_NAME = 35; // Maximum space allocated for file name

void open_input(ifstream& input, char name[]); // Get file name & Open file
double find_average(ifstream& input, double& average); // Find avg values
void output(const char name[], double find_average, ostream& os = cout); // Print results

int main()
// Parameters: None
// Returns: Zero
// Calls: open_input(), find_max_min(), 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;
double avag = 0;
// Maximum and minimum numbers from file

cout << "This program can find the average numbers in a file\n"
<< "of doubles.\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

input_numbers.close(); // Close file
output(file_name, avag);



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 avag;
} // 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()

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 value;

double sum = 0;
int count = 0;

while (input >> value) // Continue as long as we can read a number from file.
{
average = sum/count;
sum += value;
count = count + 1;
}
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;

}
hastefan2001 is offline   Reply With Quote
Old 06-29-2009, 12:32 PM   #5
Registered User
 
hk_mp5kpdw's Avatar
 
Join Date: Jan 2002
Location: Northern Virginia/Washington DC Metropolitan Area
Posts: 2,772
Quote:
Originally Posted by hastefan2001 View Post
well i have this program that i have written error free but my return keeps coming out wrong can anyone help me resolve this issue. I am including my source code.
If it's "coming out wrong" then it isn't error free. It may compile without errors but there are still logic errors causing your symptoms.
__________________
On two occasions I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question.
--Charles Babbage, 1792-1871

09 F9 11 02 9D 74 E3 5B D8 41 56 C5 63 56 88 C0
hk_mp5kpdw is offline   Reply With Quote
Old 06-29-2009, 12:34 PM   #6
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
If you think this line:
Code:
find_average(input_numbers, average); // Find average values in file
does anything at all, you are mistaken.

Shame-faced edit: Oh hey, there is a & on that function after all. In that case, you should have been setting average instead of returning it. (Note that as it stands, average is set one time too soon in the loop and won't have the last value in it.)

Last edited by tabstop; 06-29-2009 at 01:22 PM.
tabstop is offline   Reply With Quote
Old 06-29-2009, 12:53 PM   #7
Registered User
 
Join Date: Mar 2009
Posts: 16
You are passing average by reference
For output
Code:
output(file_name, avag);
Where is avag set? Aren't you passing average by reference
to find_average function?
Shouldn' that be :
Code:
output(file_name, average);
zalezog is offline   Reply With Quote
Old 06-29-2009, 01:11 PM   #8
Registered User
 
Join Date: Jun 2009
Posts: 3
thanks

thanks all for your help in the forum i have been working on that for the past week and i could not quite pinpoint what the problem was right off hand and if i have 'any pther problems i will surly ask i hope that was what my boss was looking for

thanks
haste
hastefan2001 is offline   Reply With Quote
Reply

Tags
average, c++

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
matrix class shuo C++ Programming 2 07-13-2007 01:03 AM
Syntax trouble with class inheriting "redefinition of class.."? aciarlillo C++ Programming 8 10-02-2005 12:27 AM
problems finding the average of an array column mrgeoff C Programming 4 04-18-2005 11:49 PM
structure vs class sana C++ Programming 13 12-02-2002 07:18 AM
Abstract class problem VanJay011379 C++ Programming 9 07-31-2002 01:30 PM


All times are GMT -6. The time now is 01:02 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22