Question #113864

You are required to create a software for a restaurant. The restaurant charges $ 7 for people over the age of 17 and for children under or equal the age of 17 are charged $ 3.5. Write a C++ program that inputs how many people are eating at the restaurant and asks each person’s age and calculates the total amount to be charged using while and if else statements.

Expert's answer

#include <iostream>
using namespace std;
int main()
{
	//define price for person
	const unsigned short PRICE = 7;
	//define age for full price
	const unsigned short ADULT_AGE = 18;
	//define minimal possible age
	const unsigned short MIN_AGE = 0;
	//define maximal possivle age
	const unsigned short MAX_AGE = 110;

	int peopleNum;
	double total = 0;

	//input people number and check if this number is more than 0
	cout << "Input number of people:" << endl;
	cin >> peopleNum;
	while (peopleNum < 1) {
		cout << "incorrect people number: at least 1 is required. Input again" << endl;
		cin  >> peopleNum;
	}
	
	//get age of each person
	int i = 0;
	while (i < peopleNum) {
		short age;
		cout << i+1 << " person. Input age:" << endl;
		cin >> age;
		//check if it is possible age
		if (age < MIN_AGE || age > MAX_AGE) { 
			cout << "person age should be more or equal " << MIN_AGE << " and less or equal " << MAX_AGE << endl;
			continue; 
		}
		//check if discount is needed and add price
		else if (age < ADULT_AGE) total += PRICE/2.0;
		else total += PRICE;
		++i;
	}
	//output result
	cout << "amount to be charged: " << total << "$" << endl;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

LATEST TUTORIALS
APPROVED BY CLIENTS