Answer to Question #180653 in C++ for Ishan

Question #180653

[10]

Write a program that asks the user to input one number at a time and then the program should add the number to previous sum. If the user enters -1, then no more addition is to be done. All these numbers along with the sum value are to be stored in the file.

Once this is done, user will enter value of n, the program should be able to display which was the nth number entered and what was the sum at that time?


1
Expert's answer
2021-04-13T13:16:52-0400
#include <iostream>
#include <fstream>

int main()
{
    int number;
    int sum = 0;
    std::ofstream file("output.txt");
    
    do
    {
        std::cin >> number;
        if (number != -1)
        {
            file << number << " ";
            sum += number;   
        }
    }
    while (number != -1);
    file << sum;
    file.close();

    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS