输入函数的逻辑错误
Logical Error with input function
我正在做一个关于继承的项目,其中 DollarAmount 是 class SpendingRecord 的父级 class。这两个输入函数似乎有问题,因为每当我输入 -1(定界符)时,它都不会在 main 中输入 if 语句,所以程序永远不会中断并且它接受 -1 作为可接受的美元金额(它如果用户不再希望输入美元金额,则应该只接受 -1 )。 getDollar、getCent 和 getDELIM 是 DollarAmount class 的函数,所以我假设 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;
}
}
什么类型 return 函数 getDELIM()
?应该是 int
因为我希望避免溢出。不管怎样,它在哪个循环中无限循环?
对我来说,循环中的条件
while (!setCent(cent) || !setDollar(dollar) || dollar == 0 && cent == 0 && dollar != getDELIM())
{ cout << "Invalid dollar/cent amount, please reenter: $"; }
看起来很奇怪,评论对我来说更易读。我猜当值小于零时 setCent()
可能 return 为假。
我认为您在那种情况下缺少括号。
据此我会这样写:
while ((!setCent(cent) || !setDollar(dollar)) || (dollar == 0 && cent == 0) || dollar != getDELIM())
{...}
我正在做一个关于继承的项目,其中 DollarAmount 是 class SpendingRecord 的父级 class。这两个输入函数似乎有问题,因为每当我输入 -1(定界符)时,它都不会在 main 中输入 if 语句,所以程序永远不会中断并且它接受 -1 作为可接受的美元金额(它如果用户不再希望输入美元金额,则应该只接受 -1 )。 getDollar、getCent 和 getDELIM 是 DollarAmount class 的函数,所以我假设 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;
}
}
什么类型 return 函数 getDELIM()
?应该是 int
因为我希望避免溢出。不管怎样,它在哪个循环中无限循环?
对我来说,循环中的条件
while (!setCent(cent) || !setDollar(dollar) || dollar == 0 && cent == 0 && dollar != getDELIM())
{ cout << "Invalid dollar/cent amount, please reenter: $"; }
看起来很奇怪,评论对我来说更易读。我猜当值小于零时 setCent()
可能 return 为假。
我认为您在那种情况下缺少括号。
据此我会这样写:
while ((!setCent(cent) || !setDollar(dollar)) || (dollar == 0 && cent == 0) || dollar != getDELIM())
{...}