c++ - Logical Error with input function -


i working on project on inheritance, dollaramount parent class of class spendingrecord. there seems problem these 2 input functions because whenever type in -1 ( delimiter ) not enter if statement in main, program never breaks , accepts -1 acceptable dollar amount ( should accept -1 if user no longer wishes enter dollar amounts ). getdollar, getcent, , getdelim functions of dollaramount class, assumed spendingrecord inherited them. can help?

void dollaramount::inputdollar( istream &ins ) {         char decimal;         int dollar, cent;          cout << "\n$printdollar " << endl;         cout << "enter expenditure record (e.g., 1.95, coffee, enter -1 end): $";          ins >> dollar;          if ( dollar != getdelim())         {                 ins >> decimal >> cent;         }                 //                 //check make sure user inputs correct amount                 //ensures dollar in between 0 , 9999                 //ensures user not put 0 dollars , 0 cents                 //ensures cents amount between 0 , 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];          ( int = 0; < psize; i++ )         {                 cin >> ptr[i];                 //                 //if user has put in last element of array                 //allocate new array of larger size , set null                 //copy contents of smaller array larger array                 //delete old array                 //assign address location of new array older array                 //increase physical size of array                 //                 if ( == psize - 1 )                 {                         spendingrecord *tmp = null;                         tmp = new spendingrecord[psize*2];                         ( int j = 0; j < psize; j++ )                                 tmp[i] = ptr[i];                          delete [] ptr;                         ptr = tmp;                         psize *= 2;                 }                 //if user enters -1, break , not include last element ( i.e. -1 ). program not seem enter break statement                 if ( ptr[i].getdollar() == getdelim() && ptr[i].getcent() == 0 )                 {                         numelements = i;                         break;                 }     } 

what type returns function getdelim()? should int expect avoid overflow. anyway in loop goes infinite? me condition in loop

while (!setcent(cent) || !setdollar(dollar) || dollar == 0 && cent == 0 && dollar != getdelim()) { cout << "invalid dollar/cent amount, please reenter: $"; }  

looks strange , comment more readable me. guess setcent() return false when value less zero. think missing parenthesis in condition.

according write this:

while ((!setcent(cent) || !setdollar(dollar)) || (dollar == 0 && cent == 0) || dollar != getdelim()) {...}  

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -