Compiler returning errors with calculations and declarations. Screenshot of File.txt included.
//importing necessary header files #include<iostream> #include<fstream> #include<string> using namespace std; //writing a function to open the file and checking the file failure bool openTheFile(ifstream& x,string fileName){ //open the file x.open(fileName); //if file doesnt open if(x.fail()) { //then return false return false; } //otherwise returns true return true; } //writing a function to read file and counting the number of integers, //calculating sum and average of the given function*/ void calculateFile(ifstream& myFile,int& sum,int& total_num,double& average ){ //declaring variables int count=0; string output; //checking end of file by using eof while (!myFile.eof()) { //displaying data myFile >> output; sum+=stoi(output); //incrementing count variable count++; } //assigning value of count variable to total_num total_num = count; //calculating average of the file average = double(sum)/total_num; return ; } // main method int main(){ // creating input stream ifstream myFile; //open the file here, file name is file.txt bool checkFileOpen = openTheFile(myFile,"File.txt"); //checking for failure of file if(!checkFileOpen){ //displaying message to user if opening of file fails cout<<"Failed to open the file"; return 0; } //declaring variables int sum=0; int total_num=0; double average = 0.0; // calling function calculate file calculateFile(myFile,sum,total_num,average); //displaying sum, average and total number of values to user cout<<" sum of given integer value: "<<sum<<endl; cout<<" total number of integers: "<<total_num<<endl; cout<<" average of given data: "<<average<<endl;
return 0; } |
Trending nowThis is a popular solution!
Step by stepSolved in 2 steps with 1 images
- python function that creates and saves data in a file. the saved data represents exam grades. in the function , you will create n random numbers in the range 1-100, where n is the number of students. The function can be called as follows: createFile(filename, n) main function, in which the user inputs the file name and the number of students, then the main calls function createfile. the main should preform validation for n (should be > 0), and the filename(should end with .txt)arrow_forwardPython 3 Given that a variable named pathName contains a string describing a directory path, write a single Python statement that will store the names of all files and subdirectories in that path in a List structure named contents. DO NOT traverse the path/directory. You are only interested in the immediate path and its contents. You may assume that any necessary import statements already exist.arrow_forwardPls help ASAParrow_forward
- Thanks for the code, can you help me fix this two errors, i dont know how to do it.arrow_forwardQUESTION 14 [*) filelOtest.cpp #include 2 #include #include #include using namespace std; 3 4 5 6. main() //Declare variables; Step 1 6 ifstream infile; ofstream outFile; double testi, test2, test3, test4, test5; double average; string firstName; string lastName; 10 11 12 13 14 15 16 infile.open("test.txt"); //Step 2 outFile.open("testavg.txt"); //Step 3 outFile > firstName >> 1lastName; //Step 5 outFile > testi >> test2 >> test3>> test4 >> test5; //Step 7 outFile <« "Test scores: " « setw(6) << testi<< setw(6) <« test2 « setw(6) 17 18 19 20 21 22 23 24 25 26 « test3<« setw(6) « test4 « setw(6) <« test5<< endl; //Step 8 27 28 average = (testi + test2 + test3 + test4+ test5) / 5.e; //Step 9 29 30 outFile <« "Average test score: " <« setw(6)<< average << endl; //Step 10 31 infile.close(); //Step 11 outFile.close(); //Step 11 32 33 34 35 return e; 36 37 5 dh Compile Log V Debug Find Results If the file "test.txt" did not exist before you run this program, there will be a syntax error…arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education