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]
3 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]3 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
A person is trying to remember a list of 12 different items:
erastova [34]

Answer:

for me personally, it would help if the items were rearranged with like items, for example: snow, sleet, rain, and hail would go together.

so the list should look something like this:

    <u>weather conditions</u>

  • snow
  • sleet
  • hail
  • rain

    <u>fruits</u>

  • strawberry
  • banana
  • apple
  • orange

   <u>sports</u>

  • football
  • soccer
  • tennis
  • rugby

hope i helped <3 have a nice day

5 0
3 years ago
Read 2 more answers
Most search engines provide specific pages on which you can search for____ and
vazorg [7]

Answer: c

Explanation: a

7 0
3 years ago
Read 2 more answers
Kelley is managing a project to deploy the company's new application offering by using a cloud service provider for the platform
Lapatulllka [165]

Answer:

Analyse performance

Explanation:

During the deployment of new services to a certain Platform such as the cloud which is geared towards enhancing scalability which is the ability of platforms to accommodate larger load level are serve an increased number of consumers. These will require these platforms to be Analysed as per performance in other to evaluate them . Through performance analysis, elasticity may need to be tweaked by dynamic rearrangement of the different modules, hardwares in other to ensure that the platform delivers at the required level to serve the increased number of consumers.

3 0
3 years ago
Please help What two Boolean operators can you use to narrow your search results?
daser333 [38]

Answer:

if and and not and or ans all

7 0
3 years ago
When are computers most vulnerable to threats or damage?
valentinak56 [21]
With virus protection
8 0
3 years ago
Other questions:
  • 11. You should type _
    6·2 answers
  • If you press the tab key when you're in in the last cell of a table,
    10·1 answer
  • “Green Technology” is also known as what?
    11·1 answer
  • Help me on this question
    14·1 answer
  • What is performance? Multiple Choice measures how quickly a system performs a process or transaction a system that is not operat
    9·2 answers
  • Assume that x is a string variable has been given a value. Write an expression whose value is true if and only if x is an hexade
    9·1 answer
  • ___________________ are aggregated collections of memory and cpu resources that can be shared among groups of virtual machines o
    11·1 answer
  • .<br>1.<br>CPU<br>f. ALU<br>. CU<br>h. CRT<br>g. LED<br>​
    15·1 answer
  • What read a page on website​
    15·1 answer
  • Helppppppppppppppppppppppppppppppppp
    13·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!