I am having a problem with an assignment, I have got a file and then once saved I need to work out the Sum of the data, the biggest number, smallest and average.
Any ideas?
Liam.
// CO1401 Reassessment 2008/9
#include <iostream>
#include <fstream>
#include <string>
#include <cctype>
usingnamespace std;
double max(constdouble *data, int N);
double min(constdouble *data, int N);
double sum(constdouble *data, int N);
double mean(constdouble *data, int N);
int main ()
{
char N, filein[256];
ifstream myfilein;
double sum=0;
constint NMAX = 10; // Maximum number of items to be processed
do
{
cout << "Enter the name of an existing text file: ";
cin.get (filein,256);
myfilein.open (filein);
if (!myfilein.is_open())
{
cout << "Unable to open file, sorry!" << endl;
cout << "Please enter the file name that you which to be processed : ";
cin.get (filein, 256);
}
}
while (!myfilein.is_open());
cout << "These are the contents of the file: " << endl;
while (myfilein.good()) // loop while extraction from file is possible
{
N = myfilein.get(); // get character from file
if (myfilein.good())
cout << N;
}
while (myfilein >> N)
{
sum=sum+N;
}
cout << endl << endl;
cout << "The sum of the data is: " << sum << endl;
myfilein.close(); // close file
return 0;
}
Hope that helps, you will also need a text file to go with it, but thats just a 5 numbers
After loop which reads all the characters from the file, you would have reached the end of it so you won't be able of reading it the second time. You can close and reopen the file before the second loop to solve this
After you've read it at all the file would be still open but no more in a good state and you would have reached the end.
You can close and re-open it or set the state to good and restart from the beginning
For the latter solution:
1 2
f.clear(); // set state to good
f.seekg(0); // go to the beginning of the file
The rest of your code looks fine ( excluding indentation you should line up the blocks of code in a better way http://en.wikipedia.org/wiki/Indent_style ) and sum=sum+N; could be written as sum += N;
Thanks, that worked a treat, apart from the sum is coming up as like 629, when it should be 337.6.
Also if I had say 6 numbers in a text file is there a way to just process the first 5 numbers?
The numbers I was processing are
100
89
79.6
4
65
// CO1401 Reassessment 2008/9
#include <iostream>
#include <fstream>
#include <string>
#include <cctype>
usingnamespace std;
double max(constdouble *data, int N);
double min(constdouble *data, int N);
double sum(constdouble *data, int N);
double mean(constdouble *data, int N);
int main ()
{
char N, filein[10];
ifstream myfilein;
double sum=0;
double mean =0;
double max =0;
double min=0;
constint NMAX = 10; // Maximum number of items to be processed
do
{
cout << "Enter the name of an existing text file: ";
cin.get (filein,10);
myfilein.open (filein);
if (!myfilein.is_open())
{
cout << "Unable to open file, sorry!" << endl;
cout << "Please enter the file name that you which to be processed : ";
cin.get (filein, 10);
}
}
while (!myfilein.is_open());
cout << "These are the contents of the file: " << endl;
while (myfilein.good()) // loop while extraction from file is possible
{
N = myfilein.get(); // get character from file
if (myfilein.good())
cout << N;
}
myfilein.clear(); // set state to good
myfilein.seekg(0); // go to the beginning of the file
while (myfilein >> N)
{
sum += N;
}
myfilein.clear(); // set state to good
myfilein.seekg(0); // go to the beginning of the file
while (myfilein >> N)
{
mean = sum/5;
}
myfilein.clear(); // set state to good
myfilein.seekg(0); // go to the beginning of the file
while (myfilein >> N)
{
if (filein[10] > N)
max = filein[10];
}
myfilein.clear(); // set state to good
myfilein.seekg(0); // go to the beginning of the file
while (myfilein >> N)
{
if (filein[10] < N)
min = filein[10];
}
cout << endl << endl;
cout << "The sum of the data is: " << sum << endl;
cout << "The average value of the data is: " << mean << endl;
cout << "The biggest number is: " << max << endl;
cout << "The smallest number is: " << min << endl;
myfilein.close(); // close file
return 0;
}
This is what I've got now, and all the answers I got from the processing the data were wrong, I'm not sure why?
Any ideas?
Thanks Liam
That worked, but now it won't show the contents of the file as it's just coming as a line of random numbers, but it's finding the correct sum and average.
I think it's because it's still finding characters, in the get function.
If you want to read a char, use a char. istream::get returns the ASCII value of the next character in the stream. You are seeing those values as doubles instead of chars
I did that, and it's showing the correct contents of the file again, but now all the sum and average and max and min are wrong. I'm getting the 629 again as the sum of the contents.
// CO1401 Reassessment 2008/9
//Liam Moncur BSc Computer Networking
#include <iostream>
#include <fstream>
#include <string>
usingnamespace std;
double max(constdouble *data, int N);
double min(constdouble *data, int N);
double sum(constdouble *data, int N);
double mean(constdouble *data, int N);
int main ()
{
char N; //This is the changechar filein[10];
ifstream myfilein;
double sum=0;
double mean=0;
double max=0;
double min=0;
constint NMAX = 10; // Maximum number of items to be processed
do
{
cout << "Enter the name of an existing text file: ";
cin.get (filein,10);
myfilein.open (filein);
if (!myfilein.is_open())
{
cout << "Unable to open file, sorry!" << endl;
cout << "Please enter the file name that you which to be processed : ";
cin.get (filein, 10);
}
}
while (!myfilein.is_open());
cout << "These are the contents of the file: " << endl;
while (myfilein.good()) // loop while extraction from file is possible
{
myfilein.get ( N ); // get character from file This is the changeif (myfilein.good())
cout << N;
}
myfilein.clear(); // set state to good
myfilein.seekg(0); // go to the beginning of the file
while (myfilein >> N)
{
sum += N;
}
myfilein.clear(); // set state to good
myfilein.seekg(0); // go to the beginning of the file
while (myfilein >> N)
{
mean = sum/5;
}
myfilein.clear(); // set state to good
myfilein.seekg(0); // go to the beginning of the file
while (myfilein >> N)
{
if (filein[10] > N)
max = filein[10];
}
myfilein.clear(); // set state to good
myfilein.seekg(0); // go to the beginning of the file
while (myfilein >> N)
{
if (filein[10] < N)
min = filein[10];
}
cout << endl << endl;
cout << "The sum of the data is: " << sum << endl;
cout << "The average value of the data is: " << mean << endl;
cout << "The biggest number is: " << max << endl;
cout << "The smallest number is: " << min << endl;
myfilein.close(); // close file
return 0;
}
I think maybe that my functions for doing the sums, don't understand that chars and get it wring, because when it was a double, (apart from the max and min) it came up with the correct answers
Ok I did that, it finds the second biggest number, it doesn't seem to reconise sayt 100 or 300, and it's not reading negative numbers. but apart from missing negative numbers, it's finding the smallest fine.
Sorry I don't think thats the problem, i think it's just going through it all and then it's not looking logically, it just thinks that it's finding the last biggest number, and saying thats the biggest compared to the smallest.