rookie question
-
I have this program I have to write for school that accepts two integers. hours and minutes. I then have to output the format in 6:00 format. I'm only allowed integer division and and modulus division. I've tried everything I can think of but my output still looks like this.
6:0
Any tips? BINARY -
I have this program I have to write for school that accepts two integers. hours and minutes. I then have to output the format in 6:00 format. I'm only allowed integer division and and modulus division. I've tried everything I can think of but my output still looks like this.
6:0
Any tips? BINARY -
I have this program I have to write for school that accepts two integers. hours and minutes. I then have to output the format in 6:00 format. I'm only allowed integer division and and modulus division. I've tried everything I can think of but my output still looks like this.
6:0
Any tips? BINARY -
or, without using printf formatting trick, try this:
int main(int argc, char* argv[])
{
int hours = 6;
int minutes = 7;printf("%d:%d%d\\n", hours, minutes / 10, minutes % 10); return 0;
}
little too advance for a student who just started learn C++ three weeks ago. Plus It has to advance the hour when the minutes go over 59. I am so stuck. I cant even use conditional operators Here's what I got:
//Preprocessing directives #include #include using namespace std; // Main Program int main() { // variable declarations int hours, minutes ; // User input cout << "Input the time as hours and minutes (e.g. 5 23 means 5:23) " ; cin >> hours >> minutes ; // Program constants const int minutes_per_hour = 59; // Computations minutes = minutes + 1; /*hours = hours + (minutes_per_hour % minutes);*/ //Output cout << " The time is: " << hours<< " :"<< setfill('0')< BINARY -- modified at 2:22 Sunday 23rd October, 2005
-
little too advance for a student who just started learn C++ three weeks ago. Plus It has to advance the hour when the minutes go over 59. I am so stuck. I cant even use conditional operators Here's what I got:
//Preprocessing directives #include #include using namespace std; // Main Program int main() { // variable declarations int hours, minutes ; // User input cout << "Input the time as hours and minutes (e.g. 5 23 means 5:23) " ; cin >> hours >> minutes ; // Program constants const int minutes_per_hour = 59; // Computations minutes = minutes + 1; /*hours = hours + (minutes_per_hour % minutes);*/ //Output cout << " The time is: " << hours<< " :"<< setfill('0')< BINARY -- modified at 2:22 Sunday 23rd October, 2005
Change
setw(1)
tosetw(2)
.
"You're obviously a superstar." - Christian Graus about me - 12 Feb '03 "Obviously ??? You're definitely a superstar!!!" - mYkel - 21 Jun '04 "There's not enough blatant self-congratulatory backslapping in the world today..." - HumblePie - 21 Jun '05 Within you lies the power for good - Use it!
-
little too advance for a student who just started learn C++ three weeks ago. Plus It has to advance the hour when the minutes go over 59. I am so stuck. I cant even use conditional operators Here's what I got:
//Preprocessing directives #include #include using namespace std; // Main Program int main() { // variable declarations int hours, minutes ; // User input cout << "Input the time as hours and minutes (e.g. 5 23 means 5:23) " ; cin >> hours >> minutes ; // Program constants const int minutes_per_hour = 59; // Computations minutes = minutes + 1; /*hours = hours + (minutes_per_hour % minutes);*/ //Output cout << " The time is: " << hours<< " :"<< setfill('0')< BINARY -- modified at 2:22 Sunday 23rd October, 2005
Couple more things: - your minutes_per_hour constant should 60, not 59. - your hours calculation, even though it is commented out, is not correct. - after incrementing minutes, you should probably adjust its value using the mod function (% function). Below is example code also using PJ's setw(2) change:
// Program constants const int minutes\_per\_hour = 60; // Computations minutes = minutes + 1; hours = hours + (minutes / minutes\_per\_hour); // Line 1 minutes = minutes % minutes\_per\_hour; // Line 2 // Output cout << " The time is: " << hours << ":"<< setfill('0') << setw(2) << minutes << endl;
In Line 1, the expression "x / y" returns the integral portion of x divided by y. When x is 60 and y is 60, this expression yields 1, which will increment the hours by 1. When x is less than y, this expression yields 0, and the hours value is unchanged. In Line 2, the expression "x % y" returns the remainder value that you get when you divide x by y. If x is less than y, it returns just the value x. If x is 60 and y is 60, the remainder is 0, which handles the case when the minutes goes past 59 (you want it to display 00).
-
Couple more things: - your minutes_per_hour constant should 60, not 59. - your hours calculation, even though it is commented out, is not correct. - after incrementing minutes, you should probably adjust its value using the mod function (% function). Below is example code also using PJ's setw(2) change:
// Program constants const int minutes\_per\_hour = 60; // Computations minutes = minutes + 1; hours = hours + (minutes / minutes\_per\_hour); // Line 1 minutes = minutes % minutes\_per\_hour; // Line 2 // Output cout << " The time is: " << hours << ":"<< setfill('0') << setw(2) << minutes << endl;
In Line 1, the expression "x / y" returns the integral portion of x divided by y. When x is 60 and y is 60, this expression yields 1, which will increment the hours by 1. When x is less than y, this expression yields 0, and the hours value is unchanged. In Line 2, the expression "x % y" returns the remainder value that you get when you divide x by y. If x is less than y, it returns just the value x. If x is 60 and y is 60, the remainder is 0, which handles the case when the minutes goes past 59 (you want it to display 00).
Thank you so much you people are my heroes BINARY