6.11.9 完成编程练习6,但从文件中读取所需的信息。该文件的第一项应为捐款人数,余下的内容应为成对的行。在每一对中,第一行为捐款人姓名,第二行为捐款数额。

这篇文章描述了一个C++程序,它从一个文本文件中读取捐赠者信息,根据捐赠金额将其分为大额捐赠者(>10000)和小额捐赠者(<10000),并输出相应的名单。
摘要由CSDN通过智能技术生成
#include <iostream>
#include <string>
#include <cstdlib>
#include <fstream>

using namespace std;

struct member
{
    string name;
    double donation;
};


int main()
{
    int num;
    int count1 = 0; // grand donator
    int count2 = 0; // nomal donator
    string filename;
    ifstream file;

    cout << "Enter the filename: ";
    getline(cin, filename);
    file.open(filename);

    if (!file.is_open())
    {
        cout << "This file: " << filename << " can't open!" << endl;
        cout << "Program terminating.\n";
        exit(EXIT_FAILURE);
    }

// 获取第一行数据
    file >> num;
    cout << "\nThere are " << num << " donators." << endl;
    file.get(); // 读取缓冲区的换行符

// 将数据存储到结构体数组(指针)
    member * pd = new member [num];
    for (int i = 0; i < num; i++)
    {
        getline(file, pd[i].name);
        file >> pd[i].donation;
        file.get(); // 读取缓冲区的换行符
    }

//  seprate discussions
    cout << "\nThe grand donator: " << endl;
    for (int i = 0; i < num; i++)
    {
        if (pd[i].donation >= 10000)
        {
            cout << pd[i].name << ": " << pd[i].donation << endl;
            ++count1;
        }     
    }
    if (count1 == 0)
        cout << "none!" << endl; 

    cout << "\nThe normal donator: " << endl;
    for (int i = 0; i < num; i++)
    {
        if (pd[i].donation < 10000)
        {
            cout << pd[i].name << ": " << pd[i].donation << endl;
            ++count2; 
        }
    }
    if (count2 == 0)
        cout << "none!" << endl; 

    delete [] pd;
    return 0;
}

  • 14
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值