Friday, July 31, 2009

C++ new comer question?

int minutes, TotalHours;


const int hour = 60;


double Charge, Fee,ChargeC ;





char C,T,S, Type, Car, Truck, Senior, Minutes, Hours;





double main ( )


{





TotalHours = minutes / hour;











cout%26lt;%26lt;" PENTAGON VISITOR PARKING"%26lt;%26lt;endl%26lt;%26lt;endl;





cout%26lt;%26lt;" Type.....";


cin%26gt;%26gt; Type ;


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





cout%26lt;%26lt;" Minutes..";


cin%26gt;%26gt; minutes ;


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





cout%26lt;%26lt;" Hours...." %26lt;%26lt; TotalHours%26lt;%26lt;endl%26lt;%26lt;endl;





cout%26lt;%26lt;" Charge...";


cout%26lt;%26lt; ChargeC ;


ChargeC=Fee * Hours;


Fee=2.00;





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



































return 0 ;





I can not get TotalHours. I need to divide the minutes by 60 to get the hours and it won't pull up, I just keep getting 0. Anyone have any advice

C++ new comer question?
do you need a simple C++ program or what ?? what is this COMER some type of machine to log the cars and all or a reader..





Cotnact me thru email, i can help you to the fullest ;)








**************************EDIT********...





WEll i tried to put some soul into your program according to a C++ program should be.. though it is not the way programs should be written, but i wanted to edit your program and not make one mine.. it includes all the un-necessary varibles, which you declared, but as i said, i just edited it.. so here it is :)








#include"iostream.h"


#include"conio.h"


void main ( )


{


clrscr();





int minutes;


float TotalHours,Fee=2,ChargeC;


int hour = 60;


double Charge;





char C,T,S, Type, Car, Truck, Senior, Minutes, Hours;











cout%26lt;%26lt;" PENTAGON VISITOR PARKING"%26lt;%26lt;endl%26lt;%26lt;endl;





cout%26lt;%26lt;" Type.....";


cin%26gt;%26gt; Type ;


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





cout%26lt;%26lt;" Minutes..";


cin%26gt;%26gt; minutes ;


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


TotalHours = minutes / hour;





cout%26lt;%26lt;" Hours...." %26lt;%26lt; TotalHours%26lt;%26lt;endl%26lt;%26lt;endl;





cout%26lt;%26lt;" Charge...";


ChargeC=Fee * TotalHours;


cout%26lt;%26lt; ChargeC ;








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











getch();


}











Good luck and enjoy :)
Reply:if minutes == 0, then minutes/hour will be 0 too?
Reply:You need to change TotalHours from int to double


then you can customize the cout to not display decimals or two decimals if you wish.





A common programming tip - constant variables are often written in upper case - const int hour is better if written


const int HOUR = 60;





you may need to write TotalHours = (double)(minutes/hours)


--%26gt; type cast it.





hope this helps





v3ks
Reply:Some general points here: post compilable code. How can you claim a runtime problem when it won’t even compile without warnings?





main is declared as int main. Not void. Not double. Not float. Not anything else. How did you even get it to compile?





Stop polluting your global space. Why declare minutes, TotalHours, etc. globally? You can declare constants globally, but general variables? What design issue requires that?





You may want to declare minutes and TotalHours as doubles, not ints. Otherwise, you’ll be doing integral division, and the rounding will get you 0 even if the value is something like .95.





Fix your naming convention. It’s confusing.


No comments:

Post a Comment