Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Goals: Practicing loop/repetition structures to read data from a file Create a p

ID: 3797850 • Letter: G

Question

Goals: Practicing loop/repetition structures to read data from a file
Create a program that will use a loop to read data from a file called Lab5input.dat. Your program should open the file input the values, determine how many values are stored in the file and determine the lowest value in the file. Output the number of values in the file and the lowest value in the file to the screen.

Do not use anything past chapter 5. Only use the #include <iostream> and #include<fstream directives. No void and only int main() and using namespace std.

Explanation / Answer

// Reads Lab5input.dat file

// Count number of values in a file

// Output the lowest value in a file

#include <iostream>
#include<fstream>
using namespace std;

int main()
{
    fstream myfile;
    myfile.open("Lab5input.dat");
    int a,count=0,min;
    while(myfile >> a) {
        cout << "a = " << a << "min = " << min << endl ;
        if(a < min) {
            min = a ;
        }
        count++;
    }
    cout << "Number of values in file is " << count << endl ;
    cout << "Lowest value in file is " << min << endl ;
    myfile.close();
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote