(a) Write an application for Cody’s Car Care Shop that shows a user a list of available services: oil change, tire rotation, battery check, or brake inspection. Allow the user to enter a string that corresponds to one of the options, and display the option and its price as $25, $22, $15, or $5, accordingly. Display an error message if the user enters an invalid item.
(b) It might not be reasonble to expect users to type long entries such as ‘oil change’ accurately. Modify the program in (a) so that as long as the user enters the first three characters of a service, the choice is considered valid.
#include <iostream>
#include <string>
using namespace std;
struct CareShop
{
string servic;
double price;
};
void displayService(CareShop* p, int n)
{
cout << "List of all provided services" << endl;
for (int i = 0; i < 4; i++)
{
cout << p[i].servic << endl;
}
}
int getPrice(string s, CareShop* p, int n)
{
for (int i = 0; i < 4; i++)
{
if (s == p[i].servic)
{
cout << "servic: " << p[i].servic << " price:" << p[i].price << endl;
return 0;
}
}
cout << "This service " << s << " is not provided" << endl;
return 1;
}
int findPrice(string s, CareShop* p, int n)
{
for (int i = 0; i < 4; i++)
{
if (s[2] == p[i].servic[2])
{
cout << "servic:" << p[i].servic << " price: " << p[i].price << endl;
return 0;
}
}
cout << "This service " << s << " is not provided" << endl;
return 1;
}
int main()
{
CareShop test[4];
test[0] = { "oil change",25 };
test[1] = { "tire rotation",22 };
test[2] = { "battery check",15 };
test[3] = { "brake inspection",5 };
displayService(test, 4);
string choise;
// price search by full service name
cout << "Enter the full name of the service: " ;
getline(cin, choise);
getPrice(choise, test, 4);
//price search by initial 3 letters of the service
cout << "Enter the name of the service: ";
getline(cin, choise);
findPrice(choise, test, 4);
return 0;
}
Comments
Leave a comment