Thursday, July 30, 2009

C++ help program is written just need some help with the loop?

int main ( )


{





InputData();


while(CostOfPurchase != -1)


{


CHANGE();





OutputData();


}


return 0;


}








void InputData()


{


cout%26lt;%26lt;"Enter cost of purchase : ";


cin%26gt;%26gt; CostOfPurchase;


cout%26lt;%26lt;endl%26lt;%26lt;endl;








cout%26lt;%26lt;"Enter amount tendered : ";


cin%26gt;%26gt; AmountTendered;


cout%26lt;%26lt;endl%26lt;%26lt;endl;








}








void CHANGE()


{


FinalChange= AmountTendered - CostOfPurchase;





(int)dollar= (FinalChange + 0.00001)/1.00;


change = (FinalChange + 0.00001) - dollar;





(int)quarter= change/0.25;


change = change - .25*quarter;





(int)nickel= change/0.05;


change = change - .05 * nickel;





(int)penny = change/0.01;





}





void OutputData()


{





cout%26lt;%26lt; fixed%26lt;%26lt;showpoint%26lt;%26lt;setprecision(2);





cout%26lt;%26lt;"The change is $"%26lt;%26lt; FinalChange %26lt;%26lt;endl;





cout%26lt;%26lt; dollar%26lt;%26lt;" dollar"%26lt;%26lt;endl;


cout%26lt;%26lt; quarter%26lt;%26lt;" quarter"%26lt;%26lt;endl;


cout%26lt;%26lt; nickel%26lt;%26lt; " nickel"%26lt;%26lt;endl;


cout%26lt;%26lt; penny%26lt;%26lt; " penny" %26lt;%26lt;endl%26lt;%26lt;endl;





}

C++ help program is written just need some help with the loop?
You need to enter it in InputData() just before the first line. So it should look like this:





void InputData()


{


while (CostOfPurchase != -1)


{


cout%26lt;%26lt;"Enter cost of purchase : ";


cin%26gt;%26gt; CostOfPurchase;


cout%26lt;%26lt;endl%26lt;%26lt;endl;


}





cout%26lt;%26lt;"Enter amount tendered : ";


cin%26gt;%26gt; AmountTendered;


cout%26lt;%26lt;endl%26lt;%26lt;endl;








}





At least that's what it seems like your asking





Bet of Luck!
Reply:I understand what you are saying....





You are using a sentinel value -1, to end the input, ie to know when the user is done. A sentinel value is a value, that would not make sense to be the input of the cost of a purchase of a -1, so the loop is intended to make your program run until a -1 has been entered








so it would go at the very begging of your main function








do this








int main(){





//double or int what ever type you are using for cost of purchase i'm gonna use double


// declare costofpurcahse


// with a value that satisfies the condition of the loop


// to sort of trick it into running the loop


double costofpurchase = 0;





while(costofpurchase != -1){





// the data is inputed and costofpurchase is assigned a new value


inputData();





// cost of purchase will be entered over and over


// until the user is done and enters -1


// and your program runs everytime





// add if statement so if the user is done and cost of


// purchase == -1 there is no output so surround the call to your OutputData()


// with





if(costofpurchase != -1){


outputdata()


}





/*


code for your program





*/





// end of loop you were asking about


};





return 0;


};








for your case this is the answer





int main ( )


{





double costofpurchase = 0;





while(costofpurchase != -1){


InputData();





// if it is -1 the user doesn't want to know the change


// because that means they are done


if(costofpurchase != -1){


CHANGE();








OutputData();


};








};//end of loop


return 0;


}








hope that helps


No comments:

Post a Comment