Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 循环和ifs流程中的逻辑不正确_C++_Loops_File Io_Struct_Ifs - Fatal编程技术网

C++ 循环和ifs流程中的逻辑不正确

C++ 循环和ifs流程中的逻辑不正确,c++,loops,file-io,struct,ifs,C++,Loops,File Io,Struct,Ifs,所以我试图在pat.txt中找到最低年份,在don.txt中找到最高年份: Pat.txt Androf O kidney 24 2012 Blaren B kidney 35 2010 Cosmer A kidney 35 2000 Eralod O heart 53 2009 Forend B kidney 31 2003 Don.txt Zerk B kidney 20 2009 Rampe A kidney 31 2005 Darech B kidney 34 2008 Seo A k

所以我试图在pat.txt中找到最低年份,在don.txt中找到最高年份:

Pat.txt

Androf O kidney 24 2012
Blaren B kidney 35 2010
Cosmer A kidney 35 2000
Eralod O heart 53 2009
Forend B kidney 31 2003
Don.txt

Zerk B kidney 20 2009
Rampe A kidney 31 2005
Darech B kidney 34 2008
Seo A kidney 26 2010
Yuio B kidney 26 2013
代码如下:

 struct Person {

    string surname;
    string BType;
    string organ;
    int age;
    int year, ID, IDp;
} Patient[50], Donor[50];
然后是兴趣代码:

int Date = 5000;
        int Datel = 1000;
            for (i = 0; i < 6; i ++){
                    for (i1 = 0; i1 < 6; i1++){

                            if ((Patient[i].BType == Donor[i1].BType) && (Patient[i].organ == Donor[i1].organ)){

                                    if (Patient[i].year < Date){
                                        Date = Patient[i].year;

                                    //}
                                        if ((Patient[i].year == Date ) && (Donor[i1].year > Datel)){
                                            Date = Patient[i].year;
                                            Datel = Donor[i1].year;
                                            cout << Date << "   " << Datel << "\n";

                                        }

                                    }
                            }
                        }
                }
int-Date=5000;
int Datel=1000;
对于(i=0;i<6;i++){
对于(i1=0;i1<6;i1++){
如果((患者[i].B类型==供体[i1].B类型)和&(患者[i].器官==供体[i1].器官)){
if(患者[i]。年份<日期){
日期=患者[i]。年;
//}
如果((患者[i]。年==日期)和&(捐赠者[i1]。年>日期)){
日期=患者[i]。年;
Datel=捐赠者[i1]。年份;

我很困惑,为什么不简单的代码呢

int oldestPatientDate = 5000;
int newestDonorDate = 1000;
for (i = 0; i < 6; i++)
    if (oldestPatientDate > Patient[i].year)
        oldestPatientDate = Patient[i].year;
for (i = 0; i < 6; i++)
    if (newestDonorDate < Donor[i].year)
        newestDonorDate = Donor[i].year;
cout << oldestPatientDate << "   " << newestDonorDate << "\n";
int-oldestPatientDate=5000;
int newestDonorDate=1000;
对于(i=0;i<6;i++)
如果(最老的患者日期>患者[i].年)
oldestPatientDate=患者[i]。年;
对于(i=0;i<6;i++)
if(最新日期<捐赠者[i].年)
newestDonorDate=捐赠者[i]。年份;

你能试着调试它吗?请使用更好的变量名称。它使人们阅读你的代码更容易理解。什么是“日期”和“日期1”?应该是最老患者的年份吗?为什么不叫他们“最新患者日期”和“最老患者日期”?转到您最不自信的代码,提出一个问题,然后尝试回答该问题。