1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Nonamiya [84]
4 years ago
15

Implement the function fileSum. fileSum is passed in a name of a file. This function should open the file, sum all of the intege

rs 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.
Computers and Technology
1 answer:
aliina [53]4 years ago
7 0

Answer:

The C++ code is given below with appropriate comments for better understanding

Explanation:

/*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

You might be interested in
Give short answers.
Mkey [24]

Answer:

H) Well it depends how you put it. I'm Doing virtual School for my education. And it is so easy when we have technology out their to provide the help that we need. So I think it has affected education pretty good. You can Also google the answers.

I) Looking up Answers at home

J) Parents can check up on their child's school work and see if they can help them. Also The law is involved in Education.

K) It depend's on how you put it

Explanation:

Short Answers

6 0
3 years ago
CH4 has how many of each type of atom?
quester [9]

Its easy that moderators that see this answer can think that my answer isn't without explanation.

• Type of atom C (Carbon)

C = 1

• Type of atom H (Hydrogen)

H = 4

<em>You</em><em> </em><em>dont</em><em> </em><em>underst</em><em>and</em><em>?</em><em> </em><em>JUST</em><em> </em><em>SEE</em><em> </em><em>THE</em><em> </em><em>FORMULA</em><em> </em><em>C</em><em> </em><em>MEANS </em><em>ONLY </em><em>HAVE </em><em>1</em><em> </em><em>CARBON </em><em>ATOM </em><em>A</em><em>ND</em><em> </em><em>H4</em><em> </em><em>MEANS</em><em> </em><em>4</em><em> </em><em>ATOM</em><em> </em><em>OF</em><em> </em><em>HYDRO</em><em>GEN</em>

<em>oK</em><em>.</em><em> </em><em>have</em><em> </em><em>a</em><em> </em><em>nice</em><em> </em><em>day</em><em> </em><em>hope</em><em> </em><em>you</em><em> </em><em>understands</em>

5 0
3 years ago
The article begins with the author mentioning the need to find something from “three years ago”. How long do you see yourself sa
Zigmanuir [339]
I'm going to ask this are you asking us how long we see ourselves saving files or what?
7 0
3 years ago
What are the features of the Outline view in Word? Select three options.
Lynna [10]

Answer:

A,B,C

Explanation:

Outlines can be used in different ways:

1) to create the structure of a new document by manipulating Headings

2) Help to navigate across large document

3) Display the text or an abstract of the content with Headers

5 0
3 years ago
Is the PS5 worth it?
ratelena [41]
Yes it is worth it!!!!!!!!!!!
3 0
3 years ago
Read 2 more answers
Other questions:
  • Find Multiplication of Binary Numbers: 1100112 x 10012
    9·1 answer
  • What skills and characteristics do you think would be necessary for a lighting board operator to be successful?
    8·2 answers
  • What is the answer to this?
    12·1 answer
  • How to show neither precious nor accurate
    13·1 answer
  • Why might you complete a 1040 instead of a <br> 1040ez
    11·2 answers
  • Soft skills are: A) Personal qualities, habits, attitudes, and social graces that make up a person B) Hereditary C) The ability
    5·2 answers
  • 2.20 Write an expression involving a three-letter string s that evaluates to a string whose characters are the characters of s i
    6·1 answer
  • .13 LAB: Library book sorting Two sorted lists have been created, one implemented using a linked list (LinkedListLibrary linkedL
    14·1 answer
  • In half-wave rectification process, Vi=
    8·1 answer
  • Python 3.12 LAB: Input and formatted output: Right-facing arrow Given input characters for an arrowhead and arrow body, print a
    15·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!