首页 > 解决方案 > 为什么 if 语句在应该为真时返回假?

问题描述

例如,当我输入一个 8 3 2020 时,它应该使 if 语句为 true,因为 8 3 2020 是可以在数组中找到的值,但它返回 false

这里主要

/*
 * Homework 4  --  UPDATE as needed
*/ 

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>

#include "Appointment.h"
using namespace std;

                             
void callPrint (Time &TimeOrApptObject) { TimeOrApptObject.print();} 
int main(){
    int month, day, year, hour, minute,howLong;
    Appointment  myAppointments[19];

    ifstream HW4DataFileHandle;

    HW4DataFileHandle.open("Lab6Data.txt");
    while (!HW4DataFileHandle.eof( )) {
        for (int i = 1; i < 20;  i++) {
            HW4DataFileHandle>>month;
            HW4DataFileHandle>>day;
            HW4DataFileHandle>>year;
            HW4DataFileHandle>>hour;
            HW4DataFileHandle>>minute;
            HW4DataFileHandle>>howLong;
            myAppointments[i] = Appointment( month, day, year, hour, minute, howLong);
        }
        cout <<"enter a month" <<endl;
        cin >> month;
        cout <<"enter a day" <<endl;
        cin >> day;
        cout <<"enter a year"<<endl;
        cin >> year;
        Date myDate( month, day, year);

        cout <<"Appointments for" << month <<"/" << day <<"/" << year <<":"<< endl;

        for (int i = 0; i <13; i++){
            if ( friendTorCompare2Dates(myAppointments[i], myDate))
            { Time thisTime = myAppointments[i];
                thisTime.print();
                cout << endl;
            } 
        }
    }
}

日期.h

// Date.h -- Class Date    UPDATE  as needed
#ifndef DATE_H
#define DATE_H
class Date  {  
private:
    int month;
    int day;
    int year;
public:
Date(int m, int d, int y) : month(m), day(d), year(y)
    {
    }
    Date() = default;
    friend  bool friendTorCompare2Dates (const Date&,const Date& );

};
bool friendTorCompare2Dates (const Date& Right, const Date& Left)
{
    if (Right.month == Left.month && Right.day == Left.day && Right.year== Left.year)
        return true;
    else
        return false;
}

#endif

时间.h

//Time.h -- Class Time UPDATE  as needed
#ifndef TIME_H
#define TIME_H
using namespace std;
#include<iostream>

class Time {
private :
    int hour; int minute;
public:
Time(int h, int m) : hour(h)
    {
    }
    Time() = default;
    virtual void print() {
        cout << hour << " " << minute <<" "  ;
    }


};
#endif

约会.h

// Appointment.h -- Class Appointment   UPDATE as needed
//
#include "Time.h"
#include "Date.h"
#ifndef APPOINTMENT_H
#define APPOINTMENT_H
using namespace std;
class Appointment:  public Date, public Time {  
private:
    int howLong;
public:
Appointment(int month, int day, int year, int hour, int minute, int howLong) : 
    Date(month, day, year), Time(hour, minute), howLong(howLong)
    {
    }
 
    Appointment() = default; 
};

#endif

我需要对我的代码进行哪些更改,以便当我输入正确的值时它会返回 true?请在您的回答中提供一个示例,我们将不胜感激。感谢您的时间。

标签: c++classif-statement

解决方案


这里的错误不是在 if 中处于不正确的状态(应该可以完美地工作),这是由于程序的不正确行为造成的。你在这里越界了:

for (int i = 1; i < 20;  i++) {
            HW4DataFileHandle>>month;
            HW4DataFileHandle>>day;
            HW4DataFileHandle>>year;
            HW4DataFileHandle>>hour;
            HW4DataFileHandle>>minute;
            HW4DataFileHandle>>howLong;
            myAppointments[i] = Appointment( month, day, year, hour, minute, howLong);
}

当您将 myAppointments 声明为 19 个元素的数组时,它的索引合法范围为 0 到 18。不是从 1 到 20。myAppointments[0]从未被分配,第 19 条和第 20 条记录在 Great Undefined Undefined 中消失了。

在这里你只检查数组的一部分,前 13 个元素(包括未分配的元素),这是有意的吗?

    for (int i = 0; i <13; i++){
        if ( friendTorCompare2Dates(myAppointments[i], myDate))
        { Time thisTime = myAppointments[i];
            thisTime.print();
            cout << endl;
        } 
    }

这是“幻数”谬误的一个例子。


推荐阅读