1. question1.cpp:
//Vending machine menu
#include <iostream>
#include <iomanip> using
namespace std;
int main()
{
int beverage, number; //define an integer variable
const float COFFEE = 15.00, TEA = 12.50, HOT_CHOCOLATE = 17.00, CAPPUCCINO = 22.50; float cost;
cout << "Please enter the choice of drink "
<< "(a number from 1 to 4 or 0 to quit) " << endl; cout
<< "Hot beverage menu" << endl << endl;
cout << "1: Coffee " << endl; cout
<< "2: Tea " << endl; cout << "3:
Hot Chocolate " << endl; cout << "4:
Cappuccino " << endl;
cout << "0: QUIT " << endl << endl << endl; cin >>
beverage;
while (beverage > 4 || beverage < 0)
{
cout << "Invalid choice - Please re-enter "; cin
>> beverage;
}
cout << "You have selected option number " << beverage << endl;
cout << "How many cups would you like? "; cin >> number;
cout.setf(ios::fixed); cout.precision(2);
switch(beverage)
{
case 1: cost = COFFEE * number;
cout << "The total cost is R" << cost << endl; break;
case 2: cost = TEA * number;
cout << "The total cost is R" << cost << endl; break;
case 3: cost = HOT_CHOCOLATE * number; cout
<< "The total cost is R" << cost << endl; break;
case 4: cost = CAPPUCCINO * number; cout
<< "The total cost is R" << cost << endl; break;
case 0: cout << "Please come again" << endl;
break; default:
cout << "Invalid selection"
<< "Try again please" << endl;
}
return 0;
}
,2. Question 2:
a. question2a.cpp:
//Convert for loop to while loop
//Loop must execute 10 times
//Add any variable initializitions that are deemed necessary
#include <iostream> using
namespace std;
int main()
{ int i, n; n =
10; i = 1;
while (i <= n)
{
cout << i; //Displays the amount of times the loop executes if (i
< 5 && i != 2)
cout << 'X'; //Displays X for everytime i is less than 5 and also not equal to 2 cout
<< endl;
i++;
}
return 0;
}
b. Question 2b:
, b.i. Explain what the output of the code will be: next = 2 , which initiates loop - while (next
<=5) { next++; product = product * next;}
1st while loop (next = 2): count is initialized (next++) next = 2 + 1 = 3, product = 1 * 3 =
3
2nd while loop (next = 3): count is initialized (next++) next =3 + 1 = 4, product = 3 * 4 =
12
3rd while loop (next = 4): count is initialized (next++) next = 4 + 1 = 5, product = 12 * 5 = 60
4th while loop (next = 5): count is initialized (next++) next = 5 + 1 =6, product = 60 * 6 =
360
5th while loop (Next = 6): loop terminates because condition is not met
What is the logical error?:
Program is written in such a way that it’s calculating the product of numbers 3 through
6
instead of 2 through 5.
b.ii. Make necessary changes to to fix the code so that it displays what it is intended to display:
Option A:
If you wish to keep next that it initializes count before product is calculated then you need
to:
- change next before the loop such that next = 1
- change the condition of the loop to while (next < 5)
question2b.cpp:
//Calculate the product of numbers 2 through 5
#include <iostream> using
namespace std; int main()
{
int next = 1, product = 1; while
(next < 5)
{
The benefits of buying summaries with Stuvia:
Guaranteed quality through customer reviews
Stuvia customers have reviewed more than 700,000 summaries. This how you know that you are buying the best documents.
Quick and easy check-out
You can quickly pay through EFT, credit card or Stuvia-credit for the summaries. There is no membership needed.
Focus on what matters
Your fellow students write the study notes themselves, which is why the documents are always reliable and up-to-date. This ensures you quickly get to the core!
Frequently asked questions
What do I get when I buy this document?
You get a PDF, available immediately after your purchase. The purchased document is accessible anytime, anywhere and indefinitely through your profile.
Satisfaction guarantee: how does it work?
Our satisfaction guarantee ensures that you always find a study document that suits you well. You fill out a form, and our customer service team takes care of the rest.
Who am I buying this summary from?
Stuvia is a marketplace, so you are not buying this document from us, but from seller KRS2020. Stuvia facilitates payment to the seller.
Will I be stuck with a subscription?
No, you only buy this summary for R161,00. You're not tied to anything after your purchase.