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
Please do this now
Ber [7]

here are some ideas

some things i wished i knew before coming into middle school:

1. be organized

2. dont worry about other peoples opinions

3. DO YOUR HOMEWORK

things you learned so far:

1. a lot of things...

2. take notes

3.people change

advice:

if you stress too much, it gets hard.

its okay to get a bad grade on an assignment every once and a while.

be flexable with the people around you

TAKE DEEP BREATHS

DONT TALK DURING CLASS

going into the next grade:

i would change my dynamics and sleeping habits

so there are some ideas that i hope will help!!!

8 0
3 years ago
Which of these is not a combination of a keyboard and percussion musical instrument?
Sergio [31]

Answer:

The Harmonium is not a combination of a key board and percussion musical instrument.

Explanation:

The Harmonium is not a combination of a key board and percussion musical instrument.

A harmonium, also called a "melodeon", "reed organ" or "pump organ", is a keyboard instrument that is a lot like an organ. It makes sound by blowing air through reeds, which are tuned to different pitches to make musical notes.

3 0
3 years ago
An air-conditioning system's automatic controller might directly control the
yaroslaw [1]
Control the temperature of the system and as well as how long the system will run
5 0
3 years ago
Read 2 more answers
Of the people working in concert with security teams to ensure data quality and protection, the head of information management i
Svet_ta [14]

Answer:

true

Explanation:

because Internet

because Internet

3 0
3 years ago
Write a statement to declare a variable x with data type int and initialise with the value 10​
nexus9112 [7]

Answer:

int x = 10;

Explanation:

This would work in many languages (C/C++/C#/Java).

6 0
3 years ago
Other questions:
  • Why should spain go to Africa ​
    15·1 answer
  • What do you understand by structured programmingapproach? Also Discuss about bottom up and top down approach.
    8·1 answer
  • How many of you got the right answer from this app​
    8·1 answer
  • Describe a way that you can envision using the power of the computer through
    13·1 answer
  • Npesta kenos reaction
    13·2 answers
  • What is an advantage of storing data in a Data Lake, without applying a specific schema to it initially?
    10·1 answer
  • What is the main function of processing unit​
    5·2 answers
  • • Use variable to create the number 0 at location 0, 400.
    11·1 answer
  • Which type of measurement would you use to determine the mass of sugar?
    12·1 answer
  • Im a beginner programmer. what languages should i learn and how do i get better
    13·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!