C++ 输入函数的逻辑错误

C++ 输入函数的逻辑错误,c++,inheritance,operator-overloading,C++,Inheritance,Operator Overloading,我正在从事一个关于继承的项目,其中DollarAmount是SpendingRecord类的父类。这两个输入函数似乎有问题,因为每当我输入-1(分隔符)时,它不会在主语句中输入if语句,因此程序不会中断,它接受-1作为可接受的美元金额(如果用户不再希望输入美元金额,它应该只接受-1)。getDollar、getCent和getDELIM是DollarAmount类的函数,所以我假设SpendingRecord继承了它们。有人能帮忙吗 void DollarAmount::inputDollar(

我正在从事一个关于继承的项目,其中DollarAmount是SpendingRecord类的父类。这两个输入函数似乎有问题,因为每当我输入-1(分隔符)时,它不会在主语句中输入if语句,因此程序不会中断,它接受-1作为可接受的美元金额(如果用户不再希望输入美元金额,它应该只接受-1)。getDollar、getCent和getDELIM是DollarAmount类的函数,所以我假设SpendingRecord继承了它们。有人能帮忙吗

void DollarAmount::inputDollar( istream &ins )
{
        char decimal;
        int dollar, cent;

        cout << "\n$PrintDollar " << endl;
        cout << "Enter the expenditure record (e.g., 1.95, coffee, enter -1 to end): $";

        ins >> dollar;

        if ( dollar != getDELIM())
        {
                ins >> decimal >> cent;
        }
                //
                //check to make sure that the user inputs the correct amount
                //ensures that dollar is in between 0 and 9999
                //ensures that the user does not put 0 dollars and 0 cents
                //ensures that the cents amount is between 0 and 99
                //
        while ( !setCent(cent) || !setDollar(dollar) || dollar == 0 && cent == 0 && dollar != getDELIM())
        {
                cout << "Invalid dollar/cent amount, please reenter: $";
                ins >> dollar >> decimal >> cent;
        }
        while( ins.fail() ){
                ins.clear();
                ins.ignore(1000,'\n');
                cout << "Wrong input types, please reenter: $";
                ins >> dollar >> decimal >> cent;
        }

        setDollar(dollar);
        setCent(cent);
}
void SpendingRecord::inputDollar( istream & in )
{
        string itemName;
        DollarAmount::inputDollar(in);

        if ( dollar != getDELIM() )
        {
                in >> itemName;
                getline( in, itemName );
                SpendingRecord::itemName = itemName;
        }
}

  //in main
      SpendingRecord *ptr = NULL;

        ptr = new SpendingRecord[MAX];

        for ( int i = 0; i < psize; i++ )
        {
                cin >> ptr[i];
                //
                //if user has put in the last element of the array
                //allocate a new array of a larger size and set it to NULL
                //copy the contents of smaller array into larger array
                //delete the old array
                //assign address location of new array to older array
                //increase the physical size of the array
                //
                if ( i == psize - 1 )
                {
                        SpendingRecord *tmp = NULL;
                        tmp = new SpendingRecord[psize*2];
                        for ( int j = 0; j < psize; j++ )
                                tmp[i] = ptr[i];

                        delete [] ptr;
                        ptr = tmp;
                        psize *= 2;
                }
                //if the user enters -1, break and do not include the last element ( i.e. -1 ). The program does not seem to enter this break statement
                if ( ptr[i].getDollar() == getDELIM() && ptr[i].getCent() == 0 )
                {
                        numElements = i;
                        break;
                }
    }
void DollarAmount::inputDollar(istream&ins)
{
字符十进制;
整数美元,美分;
cout>十进制>>分;
}
//
//检查以确保用户输入的金额正确
//确保美元介于0和9999之间
//确保用户不会投入0美元和0美分
//确保美分金额介于0和99之间
//
而(!setCent(cent)| |!setDollar(dollar)| | dollar==0&¢==0&&dollar!=getDELIM())
{
cout>美元>>十进制>>美分;
}
while(ins.fail()){
ins.clear();
ins.忽略(1000,“\n”);
cout>美元>>十进制>>美分;
}
美元(美元);
仙(仙);
}
无效支出记录::输入美元(istream&in)
{
字符串itemName;
DollarAmount::inputDollar(单位:英寸);
如果(美元=getDELIM())
{
在>>项目名称中;
getline(in,itemName);
SpendingRecord::itemName=itemName;
}
}
//大体上
SpendingRecord*ptr=NULL;
ptr=新的消费记录[MAX];
对于(int i=0;i>ptr[i];
//
//如果用户已放入数组的最后一个元素
//分配一个较大的新数组并将其设置为NULL
//将较小数组的内容复制到较大数组中
//删除旧数组
//将新阵列的地址位置分配给旧阵列
//增加阵列的物理大小
//
如果(i==psize-1)
{
SpendingRecord*tmp=NULL;
tmp=新消费记录[psize*2];
对于(int j=0;j
类型返回什么函数
getDELIM()
?应该是
int
,因为我希望避免溢出。不管怎样,在哪个循环中它是无限的? 对我来说,循环中的条件

while (!setCent(cent) || !setDollar(dollar) || dollar == 0 && cent == 0 && dollar != getDELIM())
{ cout << "Invalid dollar/cent amount, please reenter: $"; } 
while ((!setCent(cent) || !setDollar(dollar)) || (dollar == 0 && cent == 0) || dollar != getDELIM())
{...}