Answer to Question #97928 in C++ for momina tahir

Question #97928
You are required to design an algorithm which calculates budget expenses of a family ,Your algorithm should ask for the number of children he/she has then ask for the monthly expenses of each child like(tution fee,school fee, Van charges etc) calculate total expenses and average expense of each child. Your program terminates when user enters ‘#’.
1
Expert's answer
2019-11-04T06:28:52-0500

This program will terminate when user enters '#', but there will be N colomns of information about children with 0 value of fee in the console, where N is a number of children.

include <iostream>


int getExpense()
{
	int res = 0;
	int buf = 0;
	std::cout << "Tution fee:\n";
	std::cin >> buf;
	res += buf;
	std::cout << "School fee:\n";
	std::cin >> buf;
	res += buf;
	std::cout << "Van charges:\n";
	std::cin >> buf;
	res += buf;
	return res;
}


int main()


{
	std::cout << "What number of childred do you have?\n";
	int n;
	std::cin >> n;
	int totalSum = 0;
	for (int i = 0; i < n; i++)
	{
		std::cout << "Entire information about " << i+1 << "th child.\n";
		int sum = getExpense();
		std::cout << "Total expense of the " << i + 1 << "th child is " << sum << ".\n";
		std::cout << "Average expense of the " << i + 1 << "th child is " << (double)sum / 3 << ".\n\n"; // 3 is a number of fees
		totalSum += sum;
	}


	std::cout << "Total monthly expenses is " << totalSum << ".\n";
	return 0;


}

This one totally terminates work:

#include <iostream>
#include <string>
#include <cstdlib>


int getSym()
{
	std::string s;
	std::cin >> s;
	if (s[0] == '#')
		exit(0);
	return std::stoi(s);
}


int getExpense()
{
	int res = 0;
	int buf = 0;
	std::cout << "Tution fee:\n";
	res += getSym();
	std::cout << "School fee:\n";
	res += getSym();
	std::cout << "Van charges:\n";
	res += getSym();
	return res;
}






int main()


{
	std::string s;
	std::cout << "What number of childred do you have?\n";
	int n = getSym();
	int totalSum = 0;
	for (int i = 0; i < n; i++)
	{
		std::cout << "Entire information about " << i+1 << "th child.\n";
		int sum = getExpense();
		std::cout << "Total expense of the " << i + 1 << "th child is " << sum << ".\n";
		std::cout << "Average expense of the " << i + 1 << "th child is " << (double)sum / 3 << ".\n\n"; // 3 is a number of fees
		totalSum += sum;
	}


	std::cout << "Total monthly expenses is " << totalSum << ".\n";
	return 0;


}




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!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS