| | |
About the modulo operator
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Aug 2008
Posts: 2
Reputation:
Solved Threads: 0
I'm working on some C++ excercies in my book and now I'm supposed to write a program that converats a number of seconds into days, hours, minutes and seconds with the help of arithmetic operators and integer and float variables. Nothing to fancy that is.
It ended up looking something like this:
It feels like it's very inefficient coding considering the program has to repeat the same arithemtic argument for every "cout" and that's why I ended up posting this, I want to know whether it can improved without getting to advanced.
And yea, the variable containing the number of seconds had to be a long integer but if the code can be improved if that condition is overseen please tell. =)
It ended up looking something like this:
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; int main () { long Sec; const int HouPDay = 24; const int MinPHou = 60; const int SecPMin = 60; cout << "Type in the number of seconds: "; cin >> Sec; cout << "\n" << Sec << " seconds = " << Sec / (HouPDay * MinPTim * SecPMin) << " days, "; //Calculates and prints the number of whole days that Sek make out cout << (Sec % (HouPDay * MinPHou * SecPMin))/(MinPHou * SecPMin) //Calculates and prints the number of whole hours that the remainder of the previous argument make out << " hours, "; cout << ((Sec % (HouPDay * MinPHou * SecPMin))%(MinPHou * SecPMin))/SecPMin // Calculates and prints the number of whole minutes the the remainder of the previous argument make out << " minutes and "; cout << ((Sec % (HouPDay * MinPTim * SecPMin))%(MinPTim * SecPMin))%SecPMin // Lastly; calculates and prints the number of whole seconds that're still left. << " seconds.\n"; }
It feels like it's very inefficient coding considering the program has to repeat the same arithemtic argument for every "cout" and that's why I ended up posting this, I want to know whether it can improved without getting to advanced.
And yea, the variable containing the number of seconds had to be a long integer but if the code can be improved if that condition is overseen please tell. =)
Last edited by Rawfel; Aug 18th, 2008 at 10:55 am.
•
•
Join Date: Aug 2008
Posts: 2
Reputation:
Solved Threads: 0
Ah! Thanks, you just made me realize how the modulo operator works! I had some serious trouble grasping this yesterday night, well that might've been 'couse I tried to around 3 AM, but still, thanks!
So, for example:
Would equal to the amount of whole minutes Sec make out.
So, for example:
•
•
•
•
cout << (Sec % (60 *60)) / 60
•
•
•
•
Originally Posted by Rawfel
It feels like it's very inefficient coding considering the program has to repeat the same arithemtic argument for every "cout" and that's why I ended up posting this, I want to know whether it can improved without getting to advanced.
If at first you don't succeed, keep on sucking until you do succeed.
![]() |
Similar Threads
- C++ TMoney Class - I Can't Figure This Out. (C++)
- C++ Random Numbers (C++)
- Entering time in form MM:SS (PHP)
- cards (C)
- how to display one image per day from an array? (JSP)
- do calculation on 4 integers that are inputed as whole number (C++)
- returning number of characters with ASCII code (C)
- Pseudocode for area and diameter of circle (Computer Science)
- Brain Fart (C++)
Other Threads in the C++ Forum
- Previous Thread: Can't use Strings :'(
- Next Thread: Understanding binary data and algorithms--
| Thread Tools | Search this Thread |
api array arrays based beginner binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






