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;
}
Comments
Leave a comment