-
March 13th, 2013, 08:19 PM
#1
A bit of help with importing data from a file
This code will show the data from the .txt file "file1" the data is (0.0 0.1 0.2 0.5 0.8 0.9 1.0)
What I'm trying to do is take these float values and add them all up and divide them by 7 (finding the average and output that)
Convert it into a percent and print it in a field:
Width of 6 with 2 digits after the decimal point...
I tried doing something like cout << "the average is: " << data/7 << endl; but that didn't work. I got an error that said "/" was not an operator.
Code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
ifstream myfile("file1.txt");
string data;
getline (myfile, data);
cout << data << endl;
myfile.close();
return 0;
}
-
March 13th, 2013, 08:38 PM
#2
Re: A bit of help with importing data from a file
You need to show the real code, not something like it, but the error was that you tried to use a mathematical operation on a string instead of a numeric data type.
Last edited by GCDEF; March 14th, 2013 at 07:34 AM.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|