Write a program total_cumulatively.cpp to keep a running total of integers it in
ID: 3620120 • Letter: W
Question
Write a program total_cumulatively.cpp to keep a running total ofintegers it inputs from a file integers_in.txt, where a 0 ends input. Let
the program output to totals_out.txt. Specifically, if integers_in.txt
contains
20 10 5 3 4 0 15 18
then your program should write
20 : 20
10 : 30
5 : 35
3 : 38
4 : 42
to totals_out.txt. Your program is to include at least two while loops,
one for input and the other for output. Be sure to scope loop counters
and other variables properly. Show that your program works.
Explanation / Answer
please rate - thanks #include <iostream>#include <fstream>
using namespace std;
int main()
{ifstream in;
ofstream out;
int sum=0,num[50],i=0,j=0;
in.open("integers_in.txt");
if(in.fail())
{ cout<<"input file did not open please check it ";
system("pause");
return 1;
}
out.open("totals_out.txt");
in>>num[i];
while(num[i]!=0&&cin)
{i++;
in>>num[i];
}
while(j<i)
{sum+=num[j];
out<<num[j]<<" : "<<sum<<endl;
j++;
}
out.close();
in.close();
system("pause");
return 0;
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.