// page148.cpp : 定义控制台应用程序的入口点。
//C++面向对象程序设计
//计算绿秃鹰产卵总数
//由保护区的所有动物保护着统计
#include "stdafx.h"
#include <iostream>
using namespace std ;
int get_one_total();
//前条件:用户输入一系列鸡蛋数量
//最后以一个负数结束
//后条件:总数等于所有鸡蛋计数的总和
int main()
{
cout << " This program tallies conservationst reports\n"
<< " on the green-necked vulture .\n"
<< " Each conservationist`s report consists of \n"
<< " a list of numbers . Each number is the count of \n"
<< " the eggs observed in one "
<< " green-necked vulture nest .\n"
<< " this program then tallies"
<< " the total number of eggs .\n";
int number_of_reports;
cout << " How many conservationist reports are there? ";
cin >> number_of_reports;
int grand_total = 0 , subtotal , count ;
for (count = 1; count <= number_of_reports ; count++)
{
cout << endl << " Enter the report of "
<< " conservationist number "<< count << endl;
subtotal = get_one_total();
cout << " Total egg count for conservationist "
<< " number " << count << " is "
<< subtotal << endl ;
grand_total = grand_total + subtotal ;
}
cout << endl <<" Total egg count for all reports ="
<< grand_total << endl ;
cin >> number_of_reports;
return 0 ;
}
//使用iostream
int get_one_total()
{
int total ;
cout << " Enter the number of eggs in each nest .\n"
<< " Place a negative integer"
<< " at the end of your list.\n ";
total = 0 ;
int next ;
cin >> next ;
while ( next >= 0 )
{
total = total + next ;
cin >> next ;
}
return total ;
}