Thread: C++ help with accumulators and infile/outfile

  1. #1
    Registered User
    Join Date
    Oct 2017
    Posts
    5

    C++ help with accumulators and infile/outfile

    I am back, got my last program to run properly and I thank this forum for helping.
    I am working on an assignment so I can use to study for my midterm tonight but can't seem to properly outfile data.

    I've came across similar problems online but can't seem to understand out to set my accumulator so my total, max, min, avg can be set for each of the lines and then outfiled.

    I tried running this code but it froze my computer when opening the outfile on my desktop so please do not try it, I dont want anyones computer to freeze.

    My assignment:
    A file contains 7 numbers per line and contains several records. Write a program to input each of the numbers, find the highest number, the lowest number, their total and average. Output the numbers, the highest, lowest, total and average for each set of 7 numbers to another file.A file contains 7 numbers per line and contains several records. Write a program to input each of the numbers, find the highest number, the lowest number, their total and average. Output the numbers, the highest, lowest, total and average for each set of 7 numbers to another file.
    Data File
    346 130 982 90 656 117 595
    415 948 12 64 558 571 87
    42 360 412 721 463 47 119
    441 190 985 214 509 2 571
    77 81 681 651 995 93 74
    310 9 995 561 92 14 288
    466 664 892 8 766 34 639
    151 64 98 813 67 834 369


    Here is my code, please tell me what i am doing wrong:

    Code:
    #include <iostream>
    #include <fstream>
    #include <iomanip>
    #include <string>
    using namespace std;
    
    
    int main()
    {
        int   num, max=0, min=0, total, row=0;
        float avg=0;
        total=0;
        ifstream infile;
        ofstream outfile;
        infile.open("//Users//Eddie//Desktop//TestData.txt");
        if(!infile)
        {
            cout << "File could not be openned, terminating program." << endl;
            exit(1);
        }
        outfile.open("//Users//Eddie//Desktop//TestDataOut.txt");
        if(!outfile)
        {
            cout << "Output file could not be openned, terminating program." << endl;
            exit(1);
        }
        while(!infile.eof())
        {
            infile >> num;
            total=num;
            for(row=1; row<=7, row++; )
            {
            for(total=0, total<=7; total++;)
            {
                do
                {
                    infile >>num;
                    total=num+1;
                    if(num>total)
                    {
                        max=num;
                    }
                    if(num<total)
                    {
                        min=num;
                    }
                    while(total=7)
                    {
                        avg=total/7;
                    }
                    total=0;
                    num=0;
                    max=0;
                    min=0;
                    avg=0;
                }
                while(row<=7);
                outfile << "The greatest value is: " << max << endl;
                outfile << "The least value is:" << min << endl;
                outfile << "The total is: " << total << endl;
                outfile << "The average of the values is: " << avg << endl;
            }
            }
        }
        
        infile.close();
        outfile.close();
    }

  2. #2
    Registered User
    Join Date
    Jun 2017
    Posts
    157
    I am not sure if you are allowed to use everything but a simple way to do it would be:
    Code:
    #include <iostream>
    #include <fstream>
    #include <iomanip>
    #include <string>
    #include <sstream>
    #include <vector>
    #include <algorithm>
    #include <numeric>
    
    using namespace std;
    
    
    int main()
    {
      int num;
    
      ifstream infile("//Users//Eddie//Desktop//TestData.txt");
      if (!infile)
      {
        cerr << strerror(errno) << "\n\n";
        exit(1);
      }
      ofstream outfile("//Users//Eddie//Desktop//TestDataOut.txt");
      if (!outfile)
      {
        cerr << strerror(errno) << "\n\n";
        exit(1);
      }
      string line;
      stringstream ss;
      vector<int> numbers;
      while (getline(infile, line))
      {
        ss.clear();
        numbers.clear();
        ss << line;
        while (ss >> num)
        {
          numbers.push_back(num);
        }
        int min_value = *min(numbers.begin(), numbers.end());
        int max_value = *max(numbers.begin(), numbers.end());
        int sum = accumulate(numbers.begin(), numbers.end(), 0);
        double avg = sum / numbers.size();
    
        // TO DO 
        // write data to output file
      }
    }
    CAUTION: This is just a demo, it might not be 100% correct. I just hacked it together in a few minutes.
    Last edited by OldGuy2; 11-14-2017 at 03:47 PM. Reason: Added the caution

  3. #3
    Registered User
    Join Date
    Oct 2017
    Posts
    5
    @Oldguy2
    Thanks, I appreciate the input but we haven't learned some of those headers and our midterms are written on paper -.-
    I think I have figured out my problem, I cant seem to have the program read the integers when I " infile>> " or read the different lines.

  4. #4
    Registered User
    Join Date
    Oct 2017
    Posts
    5
    Thanks for the input @Oldguy2
    Unfortunately we haven't used those headers and since lots of inclass stuff including tests are written out, so I'm trying to stick to what I have learned so far.
    The problem I am having in my program is i cant get the infile to read the integers or how to get it to read multiple rows without using break statements.

  5. #5
    Registered User
    Join Date
    Jun 2017
    Posts
    157
    It's too late now to edit the last post but lines 41-42 should be like this:
    Code:
      int min_value = *min(numbers.begin(), numbers.end());  int max_value = *max(numbers.begin(), numbers.end());

  6. #6
    Registered User
    Join Date
    Jun 2017
    Posts
    157
    After a bit of thinking I got a better idea.
    Code:
    #include <iostream>
    #include <fstream>
    #include <iomanip>
    #include <string>
    #include <sstream>
    #include <vector>
    #include <algorithm>
    #include <numeric>
    #include <exception>
    #include <cstdio>
    
    const int NUMBER_COUNT = 7;
    
    using namespace std;
    
    class Record
    {
    public:
      int min_value() const
      {
        check_valid();
        return *min_element(numbers.begin(), numbers.end());
      }
      int max_value() const
      {
        check_valid();
        return *max_element(numbers.begin(), numbers.end());
    
      }
      int sum() const
      {
        check_valid();
        return accumulate(numbers.begin(), numbers.end(), 0);
    
      }
      double avg() const
      {
        return double(sum()) / numbers.size();
      }
    private:
      void check_valid() const
      {
        if(numbers.size() != NUMBER_COUNT)
          throw exception("numbers.size() != NUMBER_COUNT");
      }
      vector<int> numbers;
      friend ostream& operator<<(ostream& os, const Record& rec);
      friend istream& operator>>(istream& os, Record& rec);
    };
    
    
    int main()
    {
      ifstream infile("numbers.txt");
      if (!infile)
      {
        cerr << strerror(errno) << "\n\n";
        exit(1);
      }
      ofstream outfile("numbers_out.txt");
      if (!outfile)
      {
        cerr << strerror(errno) << "\n\n";
        exit(1);
      }
      try
      {
        for(Record rec; infile >> rec;)
        {
          cout << rec << "\n";
          // to do - save to file 
        }
      }
      catch (const exception& ex)
      {
        cerr << ex.what() << "\n\n";
        return errno;
      }
    }
    
    ostream & operator<<(ostream & os, const Record & rec)
    {
      os << "Min: " << rec.min_value() << "\tMax: " << rec.max_value() <<
            "\tSum: " << rec.sum() << "\tAvg: " << rec.avg();
    
      return os;
    }
    
    istream & operator >> (istream & is, Record & rec)
    {
      string line;
      stringstream ss;
      int num = 0;
      rec.numbers.clear();
      getline(is, line);
      ss << line;
      while (ss >> num)
      {
        rec.numbers.push_back(num);
      }
      return is;
    }
    Output:
    Min: 90 Max: 982 Sum: 2916 Avg: 416.571
    Min: 12 Max: 948 Sum: 2655 Avg: 379.286
    Min: 42 Max: 721 Sum: 2164 Avg: 309.143
    Min: 2 Max: 985 Sum: 2912 Avg: 416
    Min: 74 Max: 995 Sum: 2652 Avg: 378.857
    Min: 9 Max: 995 Sum: 2269 Avg: 324.143
    Min: 8 Max: 892 Sum: 3469 Avg: 495.571
    Min: 64 Max: 834 Sum: 2396 Avg: 342.286

  7. #7
    Guest
    Guest
    @Edd99: In the future, start your threads in the C++ section, instead of putting C++ in the title.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Win32 API, can it inFile and outFile, like Win32 console?
    By neutral87 in forum Windows Programming
    Replies: 6
    Last Post: 05-04-2013, 07:19 AM
  2. Win32 API, can it inFile and outFile, like Win32 console?
    By neutral87 in forum C++ Programming
    Replies: 0
    Last Post: 04-28-2013, 09:22 PM
  3. infile,outfile,struct
    By Deo3 in forum C++ Programming
    Replies: 3
    Last Post: 03-16-2013, 04:33 PM
  4. 2 outfile questions
    By monki000 in forum C++ Programming
    Replies: 2
    Last Post: 04-26-2010, 10:45 PM
  5. need help with counter and accumulators
    By adrea in forum C++ Programming
    Replies: 5
    Last Post: 12-14-2002, 06:04 AM

Tags for this Thread