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;
}
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.