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
A(n) _________________________ is a character that word displays on the screen but is not visible on a printed document.
oksano4ka [1.4K]
Nonprint ...but im like 99 percent sure
6 0
3 years ago
Plz go sub 2 "Shyy096" he is a 11 y/o boy and makes music fortnite vids, it would rly help him if u subscribe cuz he only has 8.
Pachacha [2.7K]

Answer:

OKK

Explanation:

5 0
2 years ago
Write a loop that reads strings from standard input where the string is either "land", "air", or "water". The loop terminates wh
Setler79 [48]

The loop terminates when "xxxxx" is read in. If the word is land, the string followed by "land" get outputted. The same with air, and water.

Explanation:

int land = 0;

int air = 0;

int water = 0;

String word = "";

while(!(word.equals("xxxxx"))) {

word = stdin.next();

if(word.equals("land")) {

   land++;

}else if(word.equals("air")) {

   air++;

}else if(word.equals("water")) {

   water++;

}  

}

System.out.println("land:" + land);

System.out.println("air:" + air);

System.out.println("water:" + water);

5 0
3 years ago
Can someone give me the code for the edhesive 3.7 code practice for python? Thanks.
belka [17]

Answer:

Its me I think u might be able to know :)

Explanation:

4 0
2 years ago
Which of these is a physical health benefit provided by playing team sports? A. a spiritual connection to others B. lower choles
AfilCa [17]

Answer:

B. lowering cholesterol

Explanation:

Edge 2021, made a 100 on the quiz. Good luck :)

3 0
2 years ago
Read 2 more answers
Other questions:
  • PLEASE HELP ME ASAP!!!!
    11·1 answer
  • How to block someone from watching your youtube videos?
    12·2 answers
  • What is the impact of information technology in your daily life?
    12·1 answer
  • What are the ethical implications of online social media after someone has died
    8·2 answers
  • What is the difference between amplifier and the repeater?
    11·1 answer
  • in java how do i Write a program that reads a set of integers, and then prints the sum of the even and odd integers.
    6·1 answer
  • Assume phrase= "Peter Piper picked a peck of pickled peppers", What will be returned if phrase.search(/[aeiou]/) is called?
    9·1 answer
  • Which statement is false? Select one: a. Function printf does not care how large the array it is printing is. b. Function scanf
    9·1 answer
  • Why computer literacy is vital to access in business work
    7·1 answer
  • Does any of yall play rob lox?
    5·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!