Answer to Question #196319 in C++ for Simon

Question #196319
Write a program that emulates a soft drink vending machine.
Use an if-else-if block to handle a menu of the following options:
1) Coke, 2)Sprite, 3) Sprite, 4) Ice tea.
Output Example
Vending Machine
1) Coke
2) Sprite
3) Water
4) Ice tea
Select: 4
You selected an Ice tea.
1
Expert's answer
2021-05-20T18:21:12-0400
#include <iostream>

using namespace std;

string drink[4] {
    "Coke",
    "Sprite",
    "Water",
    "Ice tea"
};
  
int main() 
{
  cout << "Vecding Machine\n";
  for (int i = 0; i < 4; i++) {
    cout << i + 1 << ") " << drink[i] << "\n";
  }
  int n;
  cout << "Select: ";
  cin >> n;
  cout << "You selected a" << (n == 4 ? "n " : " ");
  
  if (n == 1)
    cout << drink[0];
  else if (n == 2)
    cout << drink[1];
  else if (n == 3)
    cout << drink[2];
  else if (n == 4)
    cout << drink[3];
  else 
    cout << "=(";
  return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog