Change total_cumulatively2.cpp to total_cumulatively3.cpp. In the new program, u
ID: 3620148 • Letter: C
Question
Change total_cumulatively2.cpp to total_cumulatively3.cpp. In thenew program, use for loops rather than while where appropriate. Show
that the new program works.
Code:
#include <iostream>
#include <fstream>
#include <iomanip>
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];
}
out<<"number running total ";
while(j<i)
{sum+=num[j];
out<<setw(5)<<num[j]<<setw(15)<<sum<<endl;
j++;
}
out.close();
in.close();
system("pause");
return 0;
}
Explanation / Answer
// so far this program successfully builds. #include #include #include using namespace std; int main() {ifstream in; ofstream out; int sum=0,num[50],i=0; in.open("integers_in.txt"); if(in.fail()){ coutnum[i]; while(num[i]!=0&&cin){ // not appropriate here to use the for loop. in>>num[i]; i++; } outRelated 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.