注册 登录
编程论坛 C++教室

求大神来看一下错在哪里了!!!

KG爱sky 发布于 2015-08-22 17:12, 621 次点击
程序代码:
int main()
{
    int i;
    int num;
    cout << "                 /////////维护合法权利团体//////////" << endl;
    cout << "请输入捐款者数目:";
    cin >> num;
    Number* pal = new Number[num];
    for (i = 0; i < num; i++)
    {
        cout << "请输入捐款者的姓名:";
        cin >> pal[i].name;
        cout << "请输入捐款者的款项:";
        cin >> pal[i].money;
    }
    if (pal[i].money >= 10000)
    {
        for (i = 0; i < num; i++)
        {
        cout << "This Is Grand Patrons:" << pal[i].name << endl;
        }
    }
    system("pause");
    return 0;
}
哪里错了?目的是当pal[i].money>10000时,输出相对应的pall[i].name的值
4 回复
#2
hjx11202015-08-22 17:24
楼主可参考掌柜的前段时候做这个题目写的代码
#include <iostream>
#include <string>
using namespace std;
const double Grand_Patrons = 10000;
int main(){
    struct Patrons{
        string name;
        double patrons;
    };
    int num,tem_a = 0,tem_b = 0;
    cout << "请输入捐款人数量:";
    (cin >> num).get();
    Patrons * ps = new Patrons[num];
    for (int i = 0; i < num; i++){
        cout << "请输入第" << i+1 << "个捐款人的名字:";
        getline(cin, ps[i].name);
        cout << "请输入" << ps[i].name << "捐款数额:";
        (cin >> ps[i].patrons).get();
    }
    cout << "Grand Patrons:" << endl;
    for (int i = 0; i < num; i++){
        
        if (ps[i].patrons > Grand_Patrons){
            cout << ps[i].name << " : "<< ps[i].patrons << endl;
            tem_a++;
        }        
    }
    if (tem_a == 0)
        cout << "none" << endl;

    cout << "Patrons" << endl;
    for (int i = 0; i < num; i++){
        if ( ps[i].patrons <= Grand_Patrons){
            cout << ps[i].name << " : "<< ps[i].patrons << endl;
            tem_b++;
        }            
    }
    if (tem_b == 0)   
        cout << "none" << endl;
   
    delete [] ps;
    return 0;
}

https://bbs.bccn.net/thread-456408-1-1.html
原始代码在6楼
#3
KG爱sky2015-08-22 17:57
回复 2楼 hjx1120
谢谢,知道错哪里了
#4
yangfrancis2015-08-23 23:00
    if (pal[i].money >= 10000)
    {
        for (i = 0; i < num; i++)
        {
        cout << "This Is Grand Patrons:" << pal[i].name << endl;
        }
    }
变成
for(i = 0; i < num; i++)
{
    if (pal[i].money >= 10000)
         cout << "This Is Grand Patrons:" << pal[i].name << endl;
}
#5
苍穹之舞2015-08-31 15:30
回复 4楼 yangfrancis
变得好
1