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]
3 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]3 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
High-level languages must be translated into machine language before they can be executed. _________________________
Rufina [12.5K]
High-level language programs must be translated into machine language before they can be executed. (Machine language instructions are encoded as binary numbers that are meant to be used by a machine, not read or written by people. The Java compiler translates Java programs into a language called Java bytecode.
4 0
2 years ago
How do you code to find the surface area 6 s2, volume s3 in python
Nana76 [90]

Answer: you code to find the surface area 6 s2, volume s3 in python

Explanation:if you dont code the surface area 6 s2, volume s3 in python then you  cant code

4 0
3 years ago
Who is willam afton from five nights at freddy
diamong [38]

Answer:

the man behind the murder

8 0
3 years ago
Read 2 more answers
1. My grandma and I went shopping to look for old
docker41 [41]

Answer:

uncommon

Explanation:

it's not common or usual because no one else finds the things

it's not kind because inanimate objects don't show feelings

6 0
2 years ago
What is data and instructions entered into the memory of a device
lyudmila [28]

A POINTING DEVICE IS AN INPUT DEVICE THAT ALLOWS A USER TO CONTROL A POINTER ON THE SCREEN. ... A KEYBOARD IS AN INPUT DEVICE THAT CONTAINS KEYS USER TO ENTER DATA AND INSTRUCTIONS INTO A COMPUTER.                                                                            

SORRY FOR CAPS MY CAPS KEY GOT STUCK

3 0
4 years ago
Other questions:
  • Write down the complete procedure for creating ExcelSheet.
    8·1 answer
  • Identify the key that would allow you to move to previous column in a row.
    11·1 answer
  • Write a dialogue between two friends who are debating on popularity or property​
    6·1 answer
  • What takes information entered into a given system and sends it automatically to all upstream systems and processes?
    6·2 answers
  • Egovernment involves the use of strategies and technologies to transform government by improving the delivery of services and en
    7·1 answer
  • The length of the hypotenuse of a right-angled triangle is the square root of the sum of the squares of the other two sides. Wri
    7·1 answer
  • Which of the following statements is true? The Wireshark protocol analyzer has limited capabilities and is not considered multi-
    15·1 answer
  • Which Tab provides the command to add a hyperlink to a document?
    11·2 answers
  • Hiiiiiiiiihwdawdfgthnythgrfergthyjuhgfd
    12·2 answers
  • When you _______ the top row or the left column, you can keep column headings and row labels visible as you scroll through your
    8·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!