Question

Write a C++ program that open the text file “numbers.txt”. (the numbers.txt looks like: ' 1....

Write a C++ program that open the text file “numbers.txt”.

(the numbers.txt looks like: ' 1. 6 8 ')

Have a “output.txt”

If summation is less than 5, your code must write last number inside “numbers.txt” in “output.txt”, else it must write the number in between in “output.txt”

(use conditional statement for comparison)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

`Hey,

Note: Brother in case of any queries, just comment in box I would be very happy to assist all your queries

#include <iostream>
#include <fstream>
using namespace std;

int main() {
int sum_of_num = 0, fist_num, lst_num, cnt = 0; // summation of the numbers in file initialized to 0
int a; // variable 'a' holds each integer after reading it
ifstream File; // File object creation

File.open("numbers.txt"); // numbers.txt is opened
if (!File) {
cout << "Error opening file";
return 0;
}
freopen("output.txt", "w", stdout);
int i=0;
while (File >> a) { // reading each integer and summing it up
if(i==0){
fist_num = a;
i++;
}
lst_num = a;
sum_of_num = sum_of_num + a;
cnt++;
}

File.close();
if(sum_of_num>10){
cout << fist_num << "\n";
}
else{
if(sum_of_num<5){
cout << lst_num << "\n";
}else{
File.open("numbers.txt");
i = 0;
while (File >> a) { // reading each integer and summing it up
if(i!=0 && i < cnt-1 ){
cout << a << "\n";
}
i++;
}
File.close();
}

}
return 0;
}

Kindly revert for any queries

Thanks.

Add a comment
Know the answer?
Add Answer to:
Write a C++ program that open the text file “numbers.txt”. (the numbers.txt looks like: ' 1....
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 that reads the integer numbers in a text file (numbers.txt) and stores the...

    Write a program that reads the integer numbers in a text file (numbers.txt) and stores the numbers to a binary file (binaryNumber.dat). (5 pts) The number.txt file contains the following numbers: 1 2 3 4 5 6 7 8 9 0 7. Write a program that reads the integer numbers in a text file (numbers.txt) and stores the numbers to a binary file (binaryNumber.dat). (5 pts) The number.txt file contains the following numbers: 1 2 3 4 5 6 7...

  • Write a program that reads the integer numbers in a text file (numbers.txt) and stores the...

    Write a program that reads the integer numbers in a text file (numbers.txt) and stores the numbers to a binary file (binaryNumber.dat). (5 pts) The number.txt file contains the following numbers: 1 2 3 4 5 6 7 8 9 0 IN JAVA PLZ 7. Write a program that reads the integer numbers in a text file (numbers.txt) and stores the numbers to a binary file (binaryNumber.dat). (5 pts) The number.txt file contains the following numbers: 1 2 3 4...

  • Lab 10 Write a program that reads the integer numbers in a text file (numbers.txt) and...

    Lab 10 Write a program that reads the integer numbers in a text file (numbers.txt) and stores the numbers to a binary file (binaryNumber.dat). The number.txt file contains the following numbers: 1 2 3 4 5 6 7 8 9 0

  • Lab 10 Write a Java program that reads the integer numbers in a text file (numbers.txt)...

    Lab 10 Write a Java program that reads the integer numbers in a text file (numbers.txt) and stores the numbers to a binary file (binaryNumber.dat).The number.txt file contains the following numbers: 1 2 3 4 5 6 7 8 9 0

  • python Create a program to open a text file for reading, find the maximum number in...

    python Create a program to open a text file for reading, find the maximum number in the file, determine if that maximum number is even, and write to an output text file. You should either write Yes if the number is even, otherwise write the maximum number. You should note the following: • Your input file must be named input.txt • The input file has one integer number per line • Your output file must be named output.txt • Your...

  • Write a C++ program that reads text from a file and encrypts the file by adding...

    Write a C++ program that reads text from a file and encrypts the file by adding 6 to the ASCII value of each character. See section 5.11 in Starting out with C++ for information on reading and writing to text files. Your program should: 1. Read the provided plain.txt file one line at a time. Because this file has spaces, use getline (see section 3.8). 2. Change each character of the string by adding 6 to it. 3. Write the...

  • If you have a file named phone-numbers.txt that looks like this: 207-568-6723 617-277-2343 207-822-9645 603-314-5892 800-233-3413...

    If you have a file named phone-numbers.txt that looks like this: 207-568-6723 617-277-2343 207-822-9645 603-314-5892 800-233-3413 Doing "sort phone-numbers.txt" will end up sorting by area code. What if you want to sort by the last 4 numbers? sort -n phone-numbers.txt sort -t\- -k3 phone-numbers.txt sort [[:allnum:]]$ phone-numbers.txt cat -r phone-numbers.txt | sort -rn

  • How to write a Java file out that that reads from numbers.txt with these numbers 2...

    How to write a Java file out that that reads from numbers.txt with these numbers 2 6 7 9 5 4 3 8 0 1 6 8 2 3 and write to a file called totalSum.txt that looks like: 2+6+7+9=24 and so on using this code import java.io.*; import java.util.Scanner; public class FileScanner {    public static void main(String[] args)    {        /* For Homework! Tokens*/        Scanner file = null;        PrintWriter fout= null;   ...

  • Write a simple telephone directory program in C++ that looks up phone numbers in a file...

    Write a simple telephone directory program in C++ that looks up phone numbers in a file containing a list of names and phone numbers. The user should be prompted to enter a first name and last name, and the program then outputs the corresponding number, or indicates that the name isn't in the directory. After each lookup, the program should ask the user whether they want to look up another number, and then either repeat the process or exit the...

  • Write a PYTHON program that allows the user to navigate the lines of text in a...

    Write a PYTHON program that allows the user to navigate the lines of text in a file. The program should prompt the user for a filename and input the lines of text into a list. The program then enters a loop in which it prints the number of lines in the file and prompts the user for a line number. Actual line numbers range from 1 to the number of lines in the file. If the input is 0, the...

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