Implement the function fileSum. fileSum is passed in a name of a file. This function should open the file, sum all of the integers within this file, close the file, and then return the sum. If the file does not exist, this function should output an error message and then call the exit function to exit the program with an error value of 1. The exit function is provided by the cstdlib library. Here is how you call the exit function if you want the exit function to return a 1 (has the same result as the main function returning a 1).

Answer :

kendrich

Answer:

/*C++ program that prompts user to enter the name of input file(input.txt in this example) and print the sum of the values in the file to console. If file dosnot exist, then close the program */

//header files

#include <fstream>

#include<string>

#include <iostream>

#include <cstdlib> //needed for exit function

using namespace std;

//function prototype

int fileSum(string filename);

int main()

{

string filename;

cout << "Enter the name of the input file: ";

cin >> filename;

cout << "Sum: " << fileSum(filename) << endl;

system("pause");

return 0;

}

/*The function fileSum that takes the string filename and

count the sum of the values and returns the sum of the values*/

int fileSum(string filename)

{

//Create a ifstream object

ifstream fin;

//Open a file

fin.open(filename);

//Initialize sum to zero

int sum=0;

//Check if file exist

if(!fin)

{

cout<<"File does not exist ."<<endl;

system("pause");

exit(1);

}

else

{

int value;

//read file until end of file exist

while(fin>>value)

{

sum+=value;

}

}

return sum;

}//end of the fileSum

Explanation:

This is a C++ program that prompts user to enter the name of input file(input.txt in this example) and print the sum of the values in the file to console. If file dosnot exist, then close the program.

Check attachment for sample output screenshot.

${teks-lihat-gambar} kendrich

Following are the program to the given question:

Program Explanation:

  • Header file.
  • Defining a method "fileSum" that takes string variable in the parameter and calculates the sum of the file value.
  • Outside the method the main method is defined that input file and check if exist and call the above method.

Program:

#include <fstream>//header file

#include <iostream>

#include <cstdlib> //needed for exit function

using namespace std;

int fileSum(string filename)//defining a method fileSum that takes string variable inside the parameter

{

  ifstream in(filename.c_str());//calling ifstream  that inputs file  

  if(in.bad() || !in.is_open())//defining if block that check file is open or bad

  {

      cout<<"Error: File \""<<filename<<"\" not found.";// print message that says file not found  

      exit(1);//calling exit method

  }

  int t = 0,n;//defining integer variable

  while(in>>n)//defining loop that adds file value

  t+=n;//adding value in t variable

  in.close(); //close the file

  return t;

}

int main()//defining main method  

{

string filename;//defining string variable

cout << "Enter the name of the input file: ";//print message

cin >> filename;//input file name  

cout << "Sum: " << fileSum(filename);//print value

return 0;

}

Output:

please find the attached file.

Learn more:

brainly.com/question/12908540

${teks-lihat-gambar} codiepienagoya
${teks-lihat-gambar} codiepienagoya

Other Questions