this is my program and i want 2 put it in terms of modules(functions):

#include<iostream>
#include<string>
#include<iomanip>
using namespace std;


int main()
{       //initialization phase.
string month;
int day=0,cones;
char choice;
double price;
//processing phase.
while(day <= 31)//day must not be greater than 31.
{
cout<<"Welcome to The Ze_Viru$ Ice Cream Producers!!\n";
//print text
cout<<"Please enter month:";
//prompt for input.
cin>>month;
cout<<"Please enter day:";
//prompt for input.
cin>>day;
while(day >=32 )//day must not be greater than 31.
{
cout<<"Sorry,the month range from 1 to 30 or 31:not that: "<<day<<endl;
//print text.
cout<<"Please enter day:";
//prompt for input
cin>>day;
}
cout<<"The codes are as follows:\n";
//print text.
cout<<"Strawberry:\tS\nVanilla:\tV\nMixed:\tM\nApple:\tA\nYoghurt:\tY\nPlease enter only one from the above\n";
//print text.
cin>>choice;
switch(choice)      {
case 'S':
case 's':


if(day >= 1 && day <= 10)
{
cout<<"You have selected the Strawberry\n";
//print text.
cout<<"How many cones would you like to have?\n";
//promt for input
cin>>cones;
price = 4.50*cones;          //assignment of price.
cout<<"Amount due is :R"<<setprecision(2)
<<setiosflags(ios::fixed | ios::showpoint)
<<price<<endl;
cout<<"Thank you and call again!!\n";
//print text.
}
else if(day >= 11 && day <= 20)
{
cout<<"You have selected the Strawberry\n";
//print text.
cout<<"How many cones would you like to have?\n";
//print text.
cin>>cones;
price = 4.00*cones;          //assignment of price.
cout<<"Amount due is :R"<<setprecision(2)
<<setiosflags(ios::fixed | ios::showpoint)
<<price<<endl;
cout<<"Thank you and call again!!\n";
//print text.
}
else if(day >=21 && day <= 31)
{
cout<<"You have selected the Strawberry\n";
//print text
cout<<"How many cones would you like to have?\n";
//print text.
cin>>cones;
price = 3.25*cones;          //assignment of price.


cout<<"Amount due is :R"<<setprecision(2)
<<setiosflags(ios::fixed | ios::showpoint)
<<price<<endl;
cout<<"Thank you for shopping in our store!!\n";
//print text.
}
break;
case 'V':
case 'v':
if(day >= 1 && day <=10)
{
cout<<"You have selected the Vanilla\n";
//print text.
cout<<"How many cones would you like to have?\n";
//print text.
cin>>cones;
price = 8.00*cones;             //assignment of price
cout<<"Amount due is :R"<<setprecision(2)
<<setiosflags(ios::fixed | ios::showpoint)
<<price<<endl;
cout<<"Thank you and call again!!\n";
//print text.
}
else if(day >=11 && day <= 20)
{
cout<<"You have selected the Vanilla\n";
//print text.
cout<<"How many cones would you like to have?\n";
//print text
cin>>cones;
price = 7.50*cones;             //assignment of price


cout<<"Amount due is :R"<<setprecision(2)
<<setiosflags(ios::fixed | ios::showpoint)
<<price<<endl;
cout<<"Thank you and call again!!\n";
//print text.
}
else if(day >=21 && day <= 31)
{
cout<<"You have selected the Vanilla\n";
//print text.
cout<<"How many cones would you like to have?\n";
//print text.
cin>>cones;
price = 6.75*cones;             //assignment of price.
cout<<"Amount due is :R"<<setprecision(2)
<<setiosflags(ios::fixed | ios::showpoint)
<<price<<endl;
cout<<"Thank you and call again!!\n";
//Print text.
}
break;
case 'A':
case 'a':
if(day >=1 && day <= 10)
{
cout<<"You have selected the Apple\n";
cout<<"How many cones would you like to have?\n";
cin>>cones;
price = 6.75*cones;
cout<<"Amount due is :R"<<setprecision(2)
<<setiosflags(ios::fixed | ios::showpoint)
<<price<<endl;
cout<<"Thank you and call again!!\n";
}
else if(day >=11 && day <= 20)
{
cout<<"You have selected the Apple\n";
cout<<"How many cones would you like to have?\n";
cin>>cones;
price = 6.25*cones;
cout<<"Amount due is :R"<<setprecision(2)
<<setiosflags(ios::fixed | ios::showpoint)
<<price<<endl;
cout<<"Thank you and call again!!\n";
}
else if(day >=21 && day <= 31)
{
cout<<"You have selected the Apple\n";
cout<<"How many cones would you like to have?\n";
cin>>cones;
price = 5.50*cones;
cout<<"Amount due is :R"<<setprecision(2)
<<setiosflags(ios::fixed | ios::showpoint)
<<price<<endl;
cout<<"Thank you and call again!!\n";
}
break;
case 'Y':
case 'y':
if(day >=1 && day <= 10)
{
cout<<"You have selected the Yoghurt\n";
cout<<"How many cones would you like to have?\n";
cin>>cones;
price = 9.65*cones;
cout<<"Amount due is :R"<<setprecision(2)
<<setiosflags(ios::fixed | ios::showpoint)
<<price<<endl;
cout<<"Thank you and call again!!\n";
}
else if(day >=11 && day <= 20)
{
cout<<"You have selected the Yoghurt:\n";
cout<<"How many cones would you like to have?\n";
cin>>cones;
price = 9.15*cones;
cout<<"Amount due is :R"<<setprecision(2)
<<setiosflags(ios::fixed | ios::showpoint)
<<price<<endl;
cout<<"Thank you and call again!!\n";
}
else if(day >=21 && day <= 31)
{
cout<<"You have selected the Yoghurt\n";
cout<<"How many cones would you like to have?\n";
cin>>cones;
price = 8.40*cones;
cout<<"Amount due is :R"<<setprecision(2)
<<setiosflags(ios::fixed | ios::showpoint)
<<price<<endl;
cout<<"Thank you and call again!!\n";
}
break;
case 'M':
case 'm':
if(day >=1 && day <= 10)
{
cout<<"You have selected the Mixed\n";
cout<<"How many cones would you like to have?\n";
cin>>cones;
price = 13.00*cones;
cout<<"Amount due is :R"<<setprecision(2)
<<setiosflags(ios::fixed | ios::showpoint)
<<price<<endl;
cout<<"Thank you and call again!!\n";
}
else if(day >=11 && day <= 20)
{
cout<<"You have selected the Mixed\n";
cout<<"How many cones would you like to have?\n";
cin>>cones;
price = 12.50*cones;
cout<<"Amount due is :R"<<setprecision(2)
<<setiosflags(ios::fixed | ios::showpoint)
<<price<<endl;
cout<<"Thank you and call again!!\n";
}
else if(day >=21 && day <= 31)
{
cout<<"You have selected the Mixed\n";
cout<<"How many cones would you like to have?\n";
cin>>cones;
price = 11.85*cones;
cout<<"Amount due is :R"<<setprecision(2)
<<setiosflags(ios::fixed | ios::showpoint)
<<price<<endl;
cout<<"Thank you and call again!!\n";
}
break;
default:
cout<<"You have entered the wrong code!!\n";
cout<<"Please check your spelling!\n";
break;
}
cout<<"Next customer please!!\n";



}


return 0;


}

:sad: :sad:

Edited 3 Years Ago by Nick Evan: Fixed formatting

just one extra method will make your code alot better, we will call it FindPrice
so

private double FindPrice(int day, char choice)
{
swich(choice) {
case 's':
case 'S' : //You may want to add output message
if (day >=1 && day <= 10)
return 4.50;
elseif (day >= 11 && day <= 20)
return 4.00;
else // day less than 31
return 3.25


// ... and all the other cases,
//note it would be good if you return -1 with there
//wasn't a match
}

In main call this method like

int price =0;
if (price = FindPrice(day, choice) != -1);
and then ask user for the number of cones and you know the rest
else
wrong choice

this was only a quick answer but i hope this helps

Edited 3 Years Ago by Nick Evan: Fixed formatting

i have read the lil problem and i believe you can improve your code once again by changing FindPrice to FindBasicPrice(char choice), so you just have the switch statements but no if statements. Then have a another method private double FindTodaysPrice(int day,double basic)
{
if(day >= 1 && day <= 10)
return basic - .50;
else if(day >=11 && day <= 20)
return basic + 1.5;
else
return basic; // i have forgot the price differences
}

This article has been dead for over six months. Start a new discussion instead.