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
belka [17]
2 years ago
11

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. 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).

Computers and Technology
1 answer:
maria [59]2 years ago
5 0

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.

You might be interested in
Which best compares and contrasts visual and performing arts? Both careers use communication skills; however, people involved in
Vlad1618 [11]

Answer:

Both careers speak to an audience; however, people involved in the performing arts speak only to a live audience.

Explanation:

sorry im late but im not sure

3 0
3 years ago
Read 2 more answers
One problem with using e-mail is that ______. a. It can be perceived as being too casual b. It is never appropriate for business
Free_Kalibri [48]
I personally feel that e-mail can be used for more business settings and as a more formal approach to things as opposed to a text, but option C seems to be more of an opinion.  I would go with C or D.
6 0
3 years ago
PLEASE HELP ASAP I WILL GIVE BRAINLIEST TO CORRECT ANSWER
salantis [7]
The correct answer is letter b
8 0
3 years ago
Read 2 more answers
A spreadsheet program of a computerized version of _______
Romashka-Z-Leto [24]

the answer is "Paper-based Accounting"

8 0
3 years ago
18. The push rod connects to the _______, which is designed to pivot back and forth to open and close the valve.
spin [16.1K]
In an overhead valve engine, pushrods connect to A. rocker arm which pivots and opens valves
6 0
3 years ago
Read 2 more answers
Other questions:
  • Which type of attack modifies the fields that contain the different characteristics of the data that is being transmitted?
    7·1 answer
  • Technician A says that a ratio is a comparison between two values. Technician B says that in a compression ratio, the main value
    5·1 answer
  • To use an imported image, simply drag it from the desktop onto the stage. true or false?
    14·1 answer
  • Which of the following is the SECOND step of the intuitive​ lowest-cost method​?
    12·1 answer
  • When performing a basic search with Bing, you first type in your search expression and then click a button to begin the search;
    13·1 answer
  • If data from a DOS system is electronically sent to an EHR or other Windows-based system via an interface to populate an indexed
    7·1 answer
  • What is network topology? PLZZZ HURRY
    9·1 answer
  • Fill in this function that takes three parameters, two Strings and an int. 4- // Write some test function calls here! The String
    9·1 answer
  • Display all the lines in unixPasswd that contain at least 10 consecutive lowercase letters. How many names are there in total th
    12·1 answer
  • 9.19 LAB: Words in a range (lists) Write a program that first reads in the name of an input file, followed by two strings repres
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!