ccpp5编程练习6.9

// ex6.9
// !!!!!!!!!!!!!!!!!!!!!!
//     注意清除队列中的换行符
// !!!!!!!!!!!!!!!!!!!!!!
// ex609.cpp:28:22: 错误: 对‘std::basic_ifstream<char>::open(std::string&)’的调用没有匹配的函数

#include <iostream>
#include <fstream>
#include <cstdlib> // support for exit()
#include <string>

const int SIZE = 60;
using namespace std;
struct donor {
	string name;
	double amount;
};

int main ()
{
	char filename[SIZE];  // 就我目前的知识,不要用 string,打不开
	ifstream inFile; // object for handling file input

	cout << "Society for the Preservation of Rightful Influence.\n\n";
	cout << "Enter filename to open: ";
	cin.getline(filename, SIZE);  // 输入文件名
		
	inFile.open(filename);   // 关联文件
	if (!inFile.is_open())   // failed to open file
	{
		cout << "Could not open the file " << filename << endl;
		cout << "Program terminating.\n";
		exit(EXIT_FAILURE);
	}
	//------------------------------------------------
	cout << "\nSuccess, opening file " << filename;
	int num = -1; // number of contributors

	inFile >> num; // 获取捐款人数
	if (num <= 0) {
		cout << "Bad data! num <= 0.\nexiting...\n";
		return 1;
	}

	inFile.get();   // 清除换行符

	cout << "\nThe number of donors: " << num << endl;

	donor * Mydonor = new donor [num];  // 分配内存

	// 怎样排除各种错误情况?暂时先不考虑吧。
	for (int i = 0; i < num; i++)
	{
		getline(inFile, Mydonor[i].name);
		cout << "Donor's Name #" << (i+1) << ": " << Mydonor[i].name << endl;
		(inFile >> Mydonor[i].amount).get();
		cout << "Amount #" << (i+1) << ": " << Mydonor[i].amount << endl;
	}
	 
	
	//----------------------------------------------------------
	cout << "\nGrand Patrons\n----------\n";
	int count = 0;    // 统计,检查是否 0 个
	for (int i = 0; i < num; i++)
	{
		if (Mydonor[i].amount >= 10000)
		{
			cout << Mydonor[i].name << ": $" 
			     << Mydonor[i].amount << endl;
			++count;
		}
	}
	if (count == 0)
		cout << "none\n";

//----------------------------------------------------------
	cout << "\nPatrons\n----------\n";
	if (count < num)
	{
		for (int i = 0; i < num; i++)
		{
			if (Mydonor[i].amount < 10000)
			{
				cout << Mydonor[i].name << ": $" 
				     << Mydonor[i].amount << endl;
				++count;
			}
		}
	}
	else
		cout << "none\n";

	delete [] Mydonor;
	inFile.close();
	return 0;
}

你可能感兴趣的:(C++,练习题)