Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

//ITS-250, Week 5 Problem 4 //Task: Modify this code and replace the while loop

ID: 3791319 • Letter: #

Question

//ITS-250, Week 5 Problem 4

//Task: Modify this code and replace the while loop with a for loop

#include<iostream>

using namespace std;

int main()

{

   const int QUIT = 0;

   double purchase,

   total = 0;

   cout << "Enter amount of first purchase. $ ";

   cin >> purchase;

   while(purchase != QUIT)

   {

total += purchase;

cout << endl <<

"Enter amount of next purchase, or " <<   QUIT << " to quit ";

cin >> purchase;

   }

   cout << endl << "Your total is $" << total << endl;

   system("PAUSE");

   return 0;

}

Explanation / Answer

Hi

I have modified the code and highlighted the code changes below

#include<iostream>
using namespace std;

int main()
{
const int QUIT = 0;
double purchase,
total = 0;
cout << "Enter amount of first purchase. $ ";
cin >> purchase;
for(;purchase != QUIT;)
{
total += purchase;
cout << endl <<
"Enter amount of next purchase, or " << QUIT << " to quit ";
cin >> purchase;
}
cout << endl << "Your total is $" << total << endl;
system("PAUSE");
return 0;
}

Output:

sh-4.2$ g++ -o main *.cpp                                                                                                                                                                                                                                              

sh-4.2$ main                                                                                                                                                                                                                                                           

Enter amount of first purchase. $ 20                                                                                                                                                                                                                                   

                                                                                                                                                                                                                                                                       

Enter amount of next purchase, or 0 to quit 30                                                                                                                                                                                                                         

                                                                                                                                                                                                                                                                       

Enter amount of next purchase, or 0 to quit 40                                                                                                                                                                                                                         

                                                                                                                                                                                                                                                                       

Enter amount of next purchase, or 0 to quit 0                                                                                                                                                                                                                          

                                                                                                                                                                                                                                                                       

Your total is $90