•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 456,583 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,596 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: 1797 | Replies: 0
![]() |
•
•
Join Date: Jun 2004
Location: Zimbabwe
Posts: 6
Reputation:
Rep Power: 0
Solved Threads: 0
Hie
I have got a problem , the problem I cannot extract the values from a file (eg I created a file called gray.txt for extraction
and a file called average.nbr for insertion) the program is running but if I check in the file average.nbr there is nothing
written.
here is the program:
// Prompts user for a file and then calculates
// the average of the values in that file
#include <fstream>
#include <iostream>
#include <stdlib.h>
#include <string>
using namespace std;
int main() {
cout << "File of values to be averaged: ";
string FileName;
cin >> FileName;
ifstream fin(FileName.c_str());
if (! fin) {
cerr << "Cannot open " << FileName
<< " for averaging." << endl;
exit(1);
}
int ValuesProcessed = 0;
float ValueSum = 0;
float Value;
while (fin >> Value) {
ValueSum += Value;
++ValuesProcessed;
}
if (ValuesProcessed > 0) {
float Average = ValueSum / ValuesProcessed;
ofstream fout ("average.nbr");
fout << Average << endl;
}
else {
cerr << "No values to average in "
<< FileName << endl;
exit(1);
}
return 0;
}
Output
Press any key to continue...
Help!!!!!!!
I have got a problem , the problem I cannot extract the values from a file (eg I created a file called gray.txt for extraction
and a file called average.nbr for insertion) the program is running but if I check in the file average.nbr there is nothing
written.
here is the program:
// Prompts user for a file and then calculates
// the average of the values in that file
#include <fstream>
#include <iostream>
#include <stdlib.h>
#include <string>
using namespace std;
int main() {
cout << "File of values to be averaged: ";
string FileName;
cin >> FileName;
ifstream fin(FileName.c_str());
if (! fin) {
cerr << "Cannot open " << FileName
<< " for averaging." << endl;
exit(1);
}
int ValuesProcessed = 0;
float ValueSum = 0;
float Value;
while (fin >> Value) {
ValueSum += Value;
++ValuesProcessed;
}
if (ValuesProcessed > 0) {
float Average = ValueSum / ValuesProcessed;
ofstream fout ("average.nbr");
fout << Average << endl;
}
else {
cerr << "No values to average in "
<< FileName << endl;
exit(1);
}
return 0;
}
Output
Press any key to continue...
Help!!!!!!!
![]() |
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- Xml file extraction query (XML, XSLT and XPATH)
- file pointer problem (C)
- file extraction (Computer Science and Software Design)
- zip file extraction (Java)
- batch file help - extraction (Windows NT / 2000 / XP / 2003)
Other Threads in the C++ Forum
- Previous Thread: need help in creating class string
- Next Thread: Help with error checking code


Linear Mode