Any one who could help me to solve this in C++ PLZ

Thread Starter

maksud

Joined Feb 26, 2009
4
Write a program to control the action of a chocolate machine. Display a message to ask what kind of chocolate is required: Plain (30p), Brazil nut (35p), Luxury (50p). Use keyboard to get the input character. This input must be validated as P, B or L depending on the choice. Then money must be requested, in the form: “Please insert 30p “depending on the selection. Display the message until the required amount has been input. If too much has been inserted, then display a message indicating the amount of change owing. And also display a message indicating ‘Thanks for Buying’. Try to use the function.
 

Salgat

Joined Dec 23, 2006
218
#include <iostream>
using namespace std;


int main()
{
char input;
cout << "What kind of chocolate is required: ";
cin >> input;

while ( (input != 'P') && (input != 'B') && (input != 'L') )
{
cout << "Please re-enter selection: ";
cin >> input;
}
int cost;
if (input == 'P')
{
cost = 30;
}
else if (input == 'B')
{
cost = 35;
}
else
{
cost = 50;
}

int input2;
cout << "Please insert " << cost << "p :" ;
cin >> input2;


while ( input2 < cost )
{
cost = cost - input2;
cout << "You still owe " << (int)cost;
cout << "\nPlease insert remaining cost: ";
cin >> input2;
}

if ( input2 > cost )
cout << "You are owed " << (int)(input2 - cost) << '\n';
cout << "Thanks for Buying";


return 0;
}
Was bored at job, botched together for you, have fun.
 

Thread Starter

maksud

Joined Feb 26, 2009
4
hi thank you some much ......thats really great job.but unfortunetly i made a mistake when i post the topics ....i need to do that using function and SWITCH command . i didn't mention that when i post it ....relly sorry for that ....could any one solve it by using function and SWITCH command .....that would helpful for me ......Thanks
 

Salgat

Joined Dec 23, 2006
218
Take the program I made and modify it. Replace the if tests with switch cases and place whatever you're supposed to into a separate function.
 
Top