Question

Use C++ Read numbers from a file and find the sum of numbers. Perform the following...

Use C++

Read numbers from a file and find the sum of numbers.

Perform the following operations

  • Read the console input and create a file.txt.
  • $ in the console input is considered as an end of content for a file.
  • Close the file after creation.
  • Open the file in a read mode and read the numbers from a file and print the sum of all numbers.

Input

77

124

2333

$

    

    Where

  • $ indicate an end of console input for a file.

Output

2534

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <iostream>
#include <fstream>
#include <stdlib.h>

using namespace std;

int main() {

  // create file stream object
  fstream f;
  int num;
  int sum = 0, x = 0;

  // open file to write the data
  f.open("file.txt", ios::out);

  // read numbers and write to file
  cin >> num;
  while (num) {
    f << num << endl;
    cin >> num;
  }

  // close file
  f.close();
  
  // open file in read mode
  f.open("file.txt", ios::in);

  // summing numbers from file
  while(f >> x) {
    sum = sum + x;
  }

  // close file
  f.close();

  // display sum
  cout << "\nSum: " << sum;
  return 0;
}

file:

output:

For help please comment.
Thank You.

Add a comment
Know the answer?
Add Answer to:
Use C++ Read numbers from a file and find the sum of numbers. Perform the following...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Create a Word doc file (name of the file: numbers) using Python. Write the following numbers...

    Create a Word doc file (name of the file: numbers) using Python. Write the following numbers to the Word document as shown below and read these numbers from the file and calculate and print the sum and average of these numbers. 9 5 7 6 3 After that, read these values from the file and calculate the sum and average of these numbers.   I have wrote this so far, but doesnt work! def numbersfile(): f = open("numbers.docx, "w") for i...

  • Write a program that writes a series of random numbers to a file. Each random number...

    Write a program that writes a series of random numbers to a file. Each random number should be in the range of 1 through 500 inclusive. 1.Use a file called randoms.txt as a input file and output file a. If you go to open the file and its not there then ask the user to create the file     and then quit 2. Ask the user to specify how many random numbers the file will hold. a.Make sure that the...

  • Ask the user for the name of a file. Read a list of numbers of unknown...

    Ask the user for the name of a file. Read a list of numbers of unknown length from the file. Find and print the MEDIAN and the MODE(S) of the set of numbers. Do not use python statistics functions to find the medium or mode To find the MODE(S) Create a dictionary using the numbers as the keys, and the values are how often that number appears in the list. For instance, with this list [1,1,5], the dictionary would be...

  • The correct C statement to close an input or output file is closef. True False In...

    The correct C statement to close an input or output file is closef. True False In C, fscanf() is used to: print data to the computer screen. read in data from a file. read input from the keyboard. write data to a file. To read data from an input file in C, you would use: scaff fscanff fscan fscanf What is the C statement required to send output to a data file? fprintf printff printf writef When a file nointer...

  • 2. Read in an unknown number of real values from a file called “data.txt”. Calculate the...

    2. Read in an unknown number of real values from a file called “data.txt”. Calculate the average of the real numbers. Output the average of the real numbers to a file called “output.txt”. Some skeleton code is provided for you below. #include    int main (void) {                         FILE *infile = NULL;                         FILE *outfile = NULL;                         /* Fill in the code to open a file. Make sure you check that the file was open successfully. */                         while (!feof (infile))                         {                                     /*...

  • Using FLA arrays For this exercise, I want you to read 20 integer numbers from a...

    Using FLA arrays For this exercise, I want you to read 20 integer numbers from a given data file and average them. You must use an array and 2 functions. The first function will load an array with the data from the input file. The second function will average the data in the file. FILE* and the naming and location (path) of the text file for input. The mode of the user defined file stream. The function declaration. Ensure you...

  • Java Create an object that will read from a file: The file name is: yourNameln.txt Example:...

    Java Create an object that will read from a file: The file name is: yourNameln.txt Example: JeanIn.txt Your input file contains integers. 3/3 You will read in the numbers until end of input file; and calculate the average of all the numbers. 10/10

  • write a c++ code to read multiple integers from input.dat until the end of file. Edit...

    write a c++ code to read multiple integers from input.dat until the end of file. Edit input.dat and put several integers in it. Compile and execute the code then check output.dat to verify all the integers are in there. Update the code to check to see if input.dat exists before reading from it. If it doesn’t exist print an error message (and don’t read!). Compile and execute your code. Delete input.dat and output.dat and execute – did you see your...

  • Description: Create a program called numstat.py that reads a series of integer numbers from a file...

    Description: Create a program called numstat.py that reads a series of integer numbers from a file and determines and displays the name of file, sum of numbers, count of numbers, average of numbers, maximum value, minimum value, and range of values. Purpose: The purpose of this challenge is to provide experience working with numerical data in a file and generating summary information. Requirements: Create a program called numstat.py that reads a series of integer numbers from a file and determines...

  • Binary files The binary file data.dat contains characters and numbers- it has 16 characters, foll...

    java Binary files The binary file data.dat contains characters and numbers- it has 16 characters, followed by numbers of type int alternating with numbers of type double -so after the first 16 characters, there will be an int, followed by a double, followed by an int, followed by a double, and so on. Write a program to open the file and until the end of file is reached, read in the values in the appropriate data types (that is, read...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT