Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 10-25-2004, 05:29 PM   #1 (permalink)
verdell32
Registered User
 
verdell32's Avatar
 
Join Date: Oct 2004
Posts: 35
verdell32 is on a distinguished road
Smile seconds cpp problem

I am taking my first programming class and we are using Borland C++ and I am baffled.

Here is what I am supposed to do:

Write a program that inputs seconds and outputs the equivalent in days, hours, minutes and seconds. Example:


This is how the output should look exactly with a box around it.
3032767 seconds = 35 days
2 hours
26 minutes
7 seconds

Note: The result days + hours + minutes + seconds should add up to the number of seconds that were input.

Algorithm
get seconds
Divide seconds by SecondsInDay giving NumberOfDays
Modulus divide seconds by SecondsInDay giving RemainingSeconds
Divide RemainingSeconds by SecondsInHour giving NumberOfHours
Modulus divide RemainingSeconds by SecondsInHour giving RemainingSeconds
Divide RemainingSeconds by SecondsInMinutes giving NumberOfMinutes
Modulus divide RemainingSeconds by SecondsInMinutes giving RemainingSeconds
output NumberOfDays, NumberOfHours, NumberOfMinutes, RemainingSeconds

This is the understanding that I have so far.

// Include Files //////////////////////////////////////////////////////////////
#include <iostream>
#include <conio.h>
#include <math.h>
// Function Prototypes ////////////////////////////////////////////////////////

// Program Mainline ///////////////////////////////////////////////////////////

int main( )
{
const unsigned int SECONDS_IN_DAY;
const unsigned int seconds = 60*60*24;
const unsigned int hours = 60*60;
const unsigned int days = ;
const unsigned int minutes;


//Get input data
cout << "Enter seconds: ";
cin >> seconds;

//The forumla that will be calculated

<< days*24*60*60 + hours*60*60 + mins*60 + secs << endl;
SECONDS_IN_DAY =;
days = seconds / SECONDS_IN_DAY;
hours = 60*60 ;
minutes = minutes % 60;
seconds = seconds - (SECONDS_IN_DAY * days);


//Output results
cout << "SECONDS_IN_DAY"<< seconds << "=" << "days" << days <<endl;
cout << " hours" <<hours<<endl;
cout << " minutes"<<minutes<<endl;
cout << " seconds"<<seconds<<endl;
getch();
return 0;
}//endmn
verdell32 is offline   Reply With Quote
Old 10-26-2004, 07:05 AM   #2 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Code:
#include <iostream>
#include <string>
#include <climits>

using std::cin;
using std::cout;
using std::numeric_limits;
using std::streamsize;
using std::string;
using std::endl;

//Optional function: depends on IDE. Remove if not needed.
void wait_for_enter();

//Additional helper. Remove if not needed.
void reset_istream();

//-- Define a day, hour and minute in terms of seconds. "static" is optional right now.
static const unsigned SECONDS_IN_DAY = 86400;
static const unsigned SECONDS_IN_HOUR = 3600;
static const unsigned SECONDS_IN_MINUTES = 60;

int main(int argc, char *argv[])
{
   unsigned TotalSeconds;
   //Get input data.
   cout << "Enter Total amount of Seconds: ";
   cin >> TotalSeconds;
   
   // Using the following formula:
   // Days = TotalSeconds / SECONDS_IN_DAY
   // Hours = (TotalSeconds % SECONDS_IN_DAY) / SECONDS_IN_HOUR
   // Minutes = ((TotalSeconds % SECONDS_IN_DAY) % SECONDS_IN_HOUR) / SECONDS_IN_MINUTES
   // Seconds = ((TotalSeconds % SECONDS_IN_DAY) % SECONDS_IN_HOUR) % SECONDS_IN_MINUTES
   unsigned Days = TotalSeconds / SECONDS_IN_DAY;
   unsigned RemainingSeconds = TotalSeconds % SECONDS_IN_DAY;
   unsigned Hours = RemainingSeconds / SECONDS_IN_HOUR;
   RemainingSeconds %= SECONDS_IN_HOUR;
   unsigned Minutes = RemainingSeconds / SECONDS_IN_MINUTES;
   unsigned Seconds = RemainingSeconds % SECONDS_IN_MINUTES;
   
   
   //Output results.
   cout<<endl;
   cout<<"\t\t\t"<<TotalSeconds<<" seconds equals:"<<endl;
   cout<<"\t\t\t"<<Days<<" days"<<endl;
   cout<<"\t\t\t"<<Hours<< " hours"<<endl;
   cout<<"\t\t\t"<<Minutes<<" minutes"<<endl;
   cout<<"\t\t\t"<<Seconds<<" seconds"<<endl;
   cout<<endl;

   reset_istream();
   wait_for_enter();
   return 0;
}

//---------------------------------------------------

void reset_istream()
{
   if(cin.eof())
   {
      cin.clear();
   }
   else
   {
      cin.clear();
      cin.ignore(numeric_limits<streamsize>::max(), '\n');
   }
}

//---------------------------------------------------

void wait_for_enter()
{
  cout << "press <enter> to continue...";
  // Reset failstate, just in case.
  cin.clear();
  string line;
  getline( cin, line);
}
__________________
Valmont is offline   Reply With Quote
Old 10-26-2004, 03:13 PM   #3 (permalink)
verdell32
Registered User
 
verdell32's Avatar
 
Join Date: Oct 2004
Posts: 35
verdell32 is on a distinguished road
Thumbs up Thank you very much

I appreciate all the help since this is my first programming class and next I will be moving to Object Oriented programming
verdell32 is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
strange posting problem on my website......... trevor PHP 2 12-19-2003 12:25 PM
Web Design Problem elusionsdesign HTML, XML, Javascript, AJAX 2 11-09-2003 07:01 PM
Help debugging a power problem Belisarius Lounge 0 10-25-2003 04:44 PM
This is a windows/C problem UnderWing Standard C, C++ 6 03-28-2003 06:17 AM
Problem printing, please help me laurence10 Java 0 03-11-2003 04:26 PM


All times are GMT -8. The time now is 03:54 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting