Sunday, August 2, 2009

C++ question, I know someone out there knows?

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...





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++ question, I know someone out there knows?
it is saying that you are going to be a math person or something to do with money...like a bank...you are counting change, and you end up with a "dollar" "quarter" "nickle" "penny" so youreally end up with $1.31 all together...CORRECT?? probably not...oh well im bored..hah
Reply:like i said earlier you must declare cost of purchase with a value so the loop is entered then you input data in the loop








int main ( )


{





double CostOfPurcase = 0;


//YOU HAVE TO DECLARE THIS VARIABLE HERE AND IT HAS TO BE = 0. TAKE ALL OTHER DECLARATIONS OUT EXCEPT THIS ONE!!!





while(CostOfPurchase != -1)


{





inputData();


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;








}








if you are still doing











#include%26lt;iostream%26gt;


#include%26lt;iomanip%26gt;


using namespace std;








int dollar, quarter, nickel,penny;


double CostOfPurchase, AmountTendered, change, CHANGE;





int main ( )


{


cout%26lt;%26lt;"Enter cost of Purchase: "%26lt;%26lt;endl;


cin%26gt;%26gt; CostOfPurchase;


cout%26lt;%26lt;endl;


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


cin%26gt;%26gt; AmountTendered;


cout%26lt;%26lt;endl;











that is WRONG DECLARE COST OFPURCAHSE IN YOUR MAIN FUNCTION WITH A VALUE THAT IS NOT == to -1 so the loop is entered and input can take place
Reply:Are you declaring the functions in a header file before you use them? If not either make a header file, or put the functions before the main function, so that main is last. This way the compiler can find them before the main function calls them.
Reply:you're wrong, we don't know unless you actually ask us a question - you simply said 'I know someone out there knows?' then pasted a bunch of source code.





we don't possess psychic abilities so unless you explain what the question is you're not going to have any luck.





are you trying to say there's an error in the code? you don't know what the code does? how the code could be improved? what?
Reply:No they don't, because you forgot to ask the question.
Reply:It needs to be in the InputData function:





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


cin%26gt;%26gt; CostOfPurchase;





RIGHT HERE





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





---------------------------------


Put an if statement RIGHT HERE, checking the value of CostOfPurchase. If it equals -1, then exit the program.
Reply:i use the exit(); command to just end the programs, however im using the customized compiler of C++ so im not sure if yours is the same... it shold be. in the while loop add if(answer == -1) exit();
Reply:I answered this question a few minutes ago, reposting is frowned upon, you should have editted your first question. I will put my answer here along with the additional question you have to answer.





You need to enter it in InputData() just before the first line. Also to break out of the void function without entering Amount Tendered, you need a return statement. 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;


}





return;





}








You will have to create a separate function for getting Amount Tendered or else it will never reach there.





At least that's what it seems like your asking





Bet of Luck!
Reply:so umm what's your question exactly?





int main ( )


{





while(CostOfPurchase != -1)


{


InputData();


}


return 0;


}





void InputData()


{


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


cin%26gt;%26gt; CostOfPurchase;


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





if(CostOfPurchase!=-1)


{


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


cin%26gt;%26gt; AmountTendered;


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


CHANGE();


OutputData();


}





}





PS I'm assuming CostOfPurchase is a "global" variable, bad practice btw.
Reply:Maybe this:





int main ( )


{





InputData();


while(CostOfPurchase != -1)


{


CHANGE();





OutputData();





InputData(); //I just added this


}


return 0;


}


No comments:

Post a Comment