In: Computer Science
Code the FSM in C++, and show that the program works.
Construct a Finite State Machine that models an old-fashioned soda machine that accepts nickels, dimes, and
quarters. The soda machine accepts change until 35 cents have been put in. It gives change back
for any amount greater than 35 cents. Then the customer can push buttons to receive either a
cola, a root beer, or a ginger ale.
-----------------------------Screenshot-----------------------------
-----------------------------Code-----------------------------
#include <iostream>
using namespace std;
string products[] = {"Cola", "Root Beer", "Ginger Ale"};
int acceptMoney(){
int money = 0;
while(money<35){
cout << "Add Money: ";
int temp; cin >> temp;
if(temp<0) continue;
money += temp;
}
return money;
}
int chooseProduct(){
int i = 0;
cout << i << ". " << products[i++]
<< endl;
cout << i << ". " << products[i++]
<< endl;
cout << i << ". " << products[i++]
<< endl;
int ind = -1;
while(ind>2 || ind<0){
cout << "Choose
product(0/1/2) : ";
cin >> ind;
}
return ind;
}
void giveBackChange(int money){
if(money>35){
cout << "Giving back "
<< money-35 << "." << endl;
}
}
void giveProduct(int ind){
cout << "Giving product: " <<
products[ind] << endl;
}
int main(){
while(true){
cout << endl << "Soda
machine is ready!!" << endl;
int money = acceptMoney();
int ind = chooseProduct();
giveBackChange(money);
giveProduct(ind);
}
}