fork download
  1. #include <iostream>
  2. using namespace std;
  3. class food
  4. {
  5. public:
  6. string item[4];
  7. double price[4],fat[4],carbs[4],fiber[4];
  8. food()
  9. {
  10. item[0]="Hamburger";
  11. item[1]="Salad";
  12. item[2]="French fries";
  13. item[3]="Soda";
  14. }
  15.  
  16. // member function
  17. public :
  18. double getDetail(string ip);
  19. void setfood(string it,double p,double f, double c, double fb);
  20.  
  21.  
  22. };
  23. void food::setfood(string it,double p,double f, double c, double fb)
  24. {
  25. int i;
  26. for(i=0;i<4;i++)
  27. {
  28. if(item[i].compare(it)==0)
  29. break;
  30. }
  31. price[i]=p;
  32. fat[i]=f;
  33. carbs[i]=c;
  34. fiber[i]=fb;
  35.  
  36. }
  37. double food::getDetail(string ip)
  38. {
  39. int i;
  40. for(i=0;i<4;i++)
  41. {
  42. if(item[i].compare(ip)==0)
  43. break;
  44. }
  45. cout<<"Each "<<item[i]<<" has "<<fat[i]<<"g of fat, "<<carbs[i]<<"g of carbs, and "<<fiber[i]<<"g of fiber"<<endl;
  46. return price[i];
  47. }
  48.  
  49. int main()
  50. {
  51. double order=0,n,price;
  52. food user;
  53. user.setfood("Hamburger",1.85,9,33,1);
  54. user.setfood("Salad",2.00 ,1 ,11 ,5);
  55. user.setfood("French fries", 1.30, 11, 36 ,4);
  56. user.setfood("Soda" ,0.95, 0 ,38, 0);
  57.  
  58. cout<<"Enter number of hamburgers: "<<endl;
  59. price=user.getDetail("Hamburger");
  60. cin>>n;
  61. order+=price*n;
  62.  
  63. cout<<"Enter number of Salad: "<<endl;
  64. price=user.getDetail("Salad");
  65. cin>>n;
  66. order+=price*n;
  67.  
  68. cout<<"Enter number of French fries: "<<endl;
  69. price=user.getDetail("French fries");
  70. cin>>n;
  71. order+=price*n;
  72. cout<<"Enter number of Soda: "<<endl;
  73. price=user.getDetail("Soda");
  74. cin>>n;
  75. order+=price*n;
  76.  
  77.  
  78.  
  79. cout<<"Your Order is :"<<order<<endl;
  80. return 0;
  81. }
  82.  
  83.  
Success #stdin #stdout 0s 3480KB
stdin
3
2
1
stdout
Enter number of hamburgers: 
Each Hamburger has 9g of fat, 33g of carbs, and 1g of fiber
Enter number of Salad: 
Each Salad has 1g of fat, 11g of carbs, and 5g of fiber
Enter number of French fries: 
Each French fries has 11g of fat, 36g of carbs, and 4g of fiber
Enter number of Soda: 
Each Soda has 0g of fat, 38g of carbs, and 0g of fiber
Your Order is :11.8