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
  • Write a program in C++ that reads in integer numbers from a file called scores.txt until...

    Write a program in C++ that reads in integer numbers from a file called scores.txt until the sentinel value -999 is read. The program should then output the total and average of the numbers read and output each of the numbers incremented by the overall average to a file called or scoresout.txt along with the sentinel value of -999 at the end So if 10, 20, 30 and -999 are read in then the program would display a total of...

  • Convert the contents of text file to upper case (C++ Programming) Convert the contents of a...

    Convert the contents of text file to upper case (C++ Programming) Convert the contents of a text file to upper case. Perform the following operations: Read the console input and create a file. ['$' character denotes end of content in file.] Close the file after creation. Now convert the contents of the file to upper case. Display the contents of the updated file. Input:     Music is like a dream.     Without music, life would be a mistake     I can chase you,...

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

  • Write a program in C that creates an array of 100 random numbers from 0-99. The...

    Write a program in C that creates an array of 100 random numbers from 0-99. The program sums the random numbers and prints the sum.  It then writes the numbers to a new file using open, close and write. Then looks in the current directory for files that match the pattern “numbers.XXXX”. For each file, open the file and read the file. You can assume that the file will contain 100 integers. Sum the integers. Print the filename and the sum...

  • 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

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