Consider the program below which is meant to do the following: The program is me
ID: 3862833 • Letter: C
Question
Consider the program below which is meant to do the following: The program is meant to read the contents of a file that contains an int followed by a float followed by another int, e.g., 15.52 The program should read one line at a time and add each number to a running total. The program should print the running total right before exiting. Fix the errors in the code-there are 8 errors in total. write changes in place in the code or in the margins with an arrow indicating where the change goes. If you are unsure of the exact code to write, write a comment indicating what needs to be done (you will receive full credit for clear and correct comments). The code is not necessarily fully commented, but the comments below describe correct operation. None of the existing lines of code should be rearranged, but some pieces of code are missing. #include #include #include #include using namespace std; int main() { float read Float; //value to store floats from file int firstInt, secondInt; //to store ints from file //create input file stream variable for file reading //open the file, the file is in path "c:ile.txt" open // if file fails to open, print error message and quit if (fail()) { cout readFloat >> secondInt; //add each value (both ints and the float) to the running total } coutExplanation / Answer
//header files
#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
//Change 1 and 2
/***add stdlib.h for exit(0) and add fstream for file streams***/
#include <fstream>
#include <stdlib.h>
using namespace std;
int main()
{
float readFloat;
int firstInt,secondInt;
//Change2: add the variable sum which can hold the overall sum, it should be float
float sum=0;
//create input file stream variable for file reading
/***Change3: add the line to create input file stream variable****/
ifstream infile;
//open the file, the file in path c:ile.txt
/*** Change4: Change the below file to open the file correctly
Also have double back slash '\' for printing '' ***/
infile.open("C:\file.txt");
//if file fails to open, print error message and quit
/***Change5: change the fails() to is_open() and add ! as isopen returns true if opened properly else false***/
if(!infile.is_open())
{
cout << "Failed to open file!" << endl;
exit(1); // can be used only when stdlib.h is included in the header
}
//read each line using EOF method
/***Change6: Change the below if to while as you need to keep reading the file till it ends***/
while(!infile.eof())
{
//read next line
infile >> firstInt >> readFloat >> secondInt;
//add each value (both ints and float) to the running total
/***Change7: to add all the variables to read float***/
sum += firstInt + readFloat + secondInt;
}
cout << "Sum of all floats and ints is : " << sum << endl;
///***Suggested Change :: one should close the file opened before exiting the program ***/
infile.close();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.