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
A major ozone depletion happened in the 1900s. What was it caused by
melomori [17]
It was caused by air pollution and the extreme heat built up underneath it. <span />
3 0
4 years ago
Read 2 more answers
In this lab you will write a C program to sort an array of strings. The program will: Create an array of 50 strings; each string
andrezito [222]

Answer:

See explaination

Explanation:

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

void swap(char *arr[], int i, int minIdx)

{

char temp[30];

strcpy(temp, arr[i]);

strcpy(arr[i], arr[minIdx]);

strcpy(arr[minIdx], temp);

}

void sort(char *arr[], int n)

{

for (int i = 0; i < n - 1; i++)

{

int minIdx = i;

int minV = 10000;

char min[30];

strcpy(min, arr[i]);

for (int j = i + 1; j < n; j++)

{

int a = strcmp(min, arr[j]);

if (a < 0)

{

strcpy(min, arr[i]);

minIdx = j;

}

}

swap(arr, i, minIdx);

}

}

void print(char *arr[], int n)

{

for (int i = 0; i < n; i++)

printf("%s", arr[i]);

}

void read(char *arr[], int *n)

{

FILE *file = fopen("strings.dat", "r");

char line[30];

*n = 0;

printf("Reading from file...\n");

while (fgets(line, 30, file))

{

strcpy(arr[(*n)++], line);

}

print(arr, *n);

}

int main(int argc, char const *argv[])

{

char *arr[50];

for (int i = 0; i < 50; i++)

arr[i] = (char *)malloc(sizeof(char) * 30);

int len;

int n = 0;

read(arr, &n);

printf("\n-------------------------\nAfter sorting\n");

sort(arr, n);

print(arr, n);

}

6 0
3 years ago
What is hyperlink and its used in website
snow_tiger [21]

Explanation:

Hyperlink is the primary method used to navigate between webpages.

Hyperlink can redirect us to another webpages, such as websites that has graphics, files, sounds on the same webpage.

6 0
3 years ago
You purchase a new microphone for your computer to use on S.kype calls. You plug it into the microphone jack, but it doesn’t pic
satela [25.4K]

Answer:

B

Explanation:

Your computer may not have a pre-installed audio input driver or driver corrsponding to said device

4 0
3 years ago
Wanda manages an application that fluctuates in usage quite a bit. Sometimes it uses quite a few servers with lots of memory and
TEA [102]

Answer:

c. Elasticity

Explanation:

When referring to Cloud Servers this capability is known as Elasticity. It basically describes how able and efficient a server is to automatically implement resources and remove resources in order meet the specific amount of demand at any given moment. Many times a server will not have much demand but will still have all the resources being used by the server. Being able to remove these unused resources so that they are available for other usage is what makes the servers elastic.

8 0
3 years ago
Other questions:
  • Which element of the word program window contains buttons for saving a document and for undoing, redoing, and repeating a change
    5·1 answer
  • 1. A formula =A1+B2 is in cell D8. If you copy that formula to cell D9, what is the new formula in cell D9?
    7·1 answer
  • The most important protocol at the internet layer is ____.
    13·1 answer
  • Who like fortnite??????
    12·2 answers
  • Attributes of a website indicates a more reliable source of information
    14·1 answer
  • Most of the revenue that magazine companies generate comes from?
    8·2 answers
  • Electronic business includes which of the following
    5·2 answers
  • How many tables are needed to implement an REA data model that has five distinct entity-sets, two many-to-many relationships and
    6·1 answer
  • (Correct Answer Recieves Brainliest)
    8·2 answers
  • Alguien que me pueda ayudar diciéndome las características de Visual Object en programacion porfavor?!
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!