CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Mar 2013
    Posts
    2

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

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    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
  •  





Click Here to Expand Forum to Full Width

Featured