Tuesday, July 14, 2009

What is wrong with this c++ program??

I am trying to get this program to work but i can't - I am very new to this





#include %26lt;iostream%26gt;





using namespace std;





int Main()


{


const int seconds_per_minute = 60;


const int minutesPerHour = 60;





int timeInSeconds; // time that is input -- totally in seconds


int hours; // how many hours in time


int minutes; // how many minutes in time


int seconds; // how many seconds in time





cout %26lt;%26lt; "Enter the time in seconds only: " %26lt;%26lt; endl;


cin %26gt;%26gt; timeInSeconds;





seconds = timeInSeconds % seconds_per_minute;


minutes = timeInSeconds / seconds_per_minute % minutesPerHour;


hours = timeInSeconds / seconds_per_minute * seconds_per_minute;


// check the values of seconds, minutes, and hours


return 0;


}

What is wrong with this c++ program??
It would be helpful to know the error if any this program is trying to generate. Obviously you are trying to get it to display the Values you have setup.





This is going to sound stupid, but the code looks fine with exception to the modulo calls. If your trying to use the remainder's for your values that might be where you are getting messed up (i.e. the % calls). If your trying to output your code definitely need to insert some cout's in there. This is pretty much all I can tell you from the pseudo code without knowing the error it generates. ALWAYS output your variables to see what is going on before committing it to a function.
Reply:First you need to check if they have inputted over 60 seconds(1 minute) so you can calculate minutes. Do the same for hour, and check to see if you have at least 60 minutes to calculate hrs.





after "cin%26gt;%26gt; timeInSeconds" it should be:





if(timeInSeconds%26gt;=60) then //check to see if you have atleast 60 seconds


{


minuteSoFar = timeInSeconds / seconds_per_minute;


//minuteSoFarwill give you the number of minutes so far


//before calculating hours.Also since it is integer it wont give


//you a decimal value..e.g 70/60 will give you 1 not 1.167.





seconds=timeInSeconds%seconds_per_minute


// left over amount from mins, which is the seconds





if(minutesSoFar%26gt;=60)//check to see you have at least 60mins(1hr)


{


hours= minuteSoFar/minutesPerHour;


minutes=minuteSoFar%minutesPerHour;


}


else


hours = 0;


}


else


{


hours = 0;


minutes = 0;


}





cout %26lt;%26lt; "Hours = " %26lt;%26lt; hours %26lt;%26lt;endl;


cout %26lt;%26lt; "Minutes = " %26lt;%26lt; minutes %26lt;%26lt;endl;


cout %26lt;%26lt; "Seconds =" %26lt;%26lt; seconds %26lt;%26lt;endl;


return 0;





i think that should be right
Reply:hours = timeInSeconds / seconds_per_minute * seconds_per_minute;





operations "/" and "*" has equal priority so they will be executed consequently, one after one: first, the time in seconds will be diveded by the number of seconds per minute ( i.e. we get number of minutes), then this result will be multiplied by the number of seconds per minute.
Reply:Well, for starters, 'Main' should be 'main'.

salary survey

No comments:

Post a Comment