//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
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.