Thread: Need help with program

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    7

    Need help with program

    Hello, I'm new to this forum, but I need help really bad and can't seem to figure out how to fix my overflow problem.

    I'm taking a class where I have to write a program that calculates a given number of seconds and generates how many centuries, years, days, hours, and minutes that given number is. The output needs to be displayed in a table with the given numbers written above it, but at the moment I'm still trying to figure out the overflow issue and I'm using a user input of any number of seconds to give me an answer and I can't even seem to get it to do that... any help is MUCH appreciated...

    Thank you in advance

    Alex.
    Code:
    /***********
    * Alex Carpenter
    * ACCarpenter
    * CS1300/project 2/Path_Length
    * Due Date: September 7, 2011
    * A program created to calculate an "x" amount of seconds
    * into minutes, hours, days, years, and centuries.
    ***********/
    #include <stdio.h> /* use standard I/O functions */
    
    int main ()
    { /*begin main */
    
    const int seconds_in_centuries = 3153600000;
    const int seconds_in_years = 31536000;
    const int seconds_in_days = 86400;
    const int seconds_in_hours = 3600;
    const int seconds_in_minutes = 60;
    
    long long int centuries;
    long long int years;
    long long int days;
    long long int hours;
    long long int minutes;
    
    
    
    /*calculates number of centuries-minutes the amount of seconds equals*/
    centuries=seconds/seconds_in_centuries;
    years = years % seconds_in_centuries;
    days = days % seconds_in_years;
    hours = hours % seconds_in_days;
    minutes = minutes % seconds_in_hours;
    seconds = seconds % seconds_in_minutes;
    
            printf("How many seconds?");
            scanf("%llu",&seconds);
            printf("%llu",seconds);
         return 0;
    }

    remember i just started learning this so we haven't advanced to anything past the basics..

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > const int seconds_in_centuries = 3153600000;
    This overflows an int on a 32-bit machine.

    How about declaring everything as a long long int ?

    Also, which compiler are you using?

    > /*calculates number of centuries-minutes the amount of seconds equals*/
    Do these AFTER reading in a value for seconds.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    7
    Quote Originally Posted by Salem View Post
    > const int seconds_in_centuries = 3153600000;
    This overflows an int on a 32-bit machine.

    How about declaring everything as a long long int ?

    Also, which compiler are you using?

    > /*calculates number of centuries-minutes the amount of seconds equals*/
    Do these AFTER reading in a value for seconds.
    Wow thank you for the very fast reply! I'm using jgrasp to actually write my code but we are using a linux server to compile everything..

  4. #4
    Registered User
    Join Date
    Sep 2011
    Posts
    7

    Anyone?

    I still need help with this... I really just need to get it to convert seconds into centuries, and I can't figure out what i'm doing wrong...

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well posting your latest code would be a start.
    What thing(s) did you do following the previous suggestions.
    We can't see your screen - remember.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by alex33zebras View Post
    I still need help with this... I really just need to get it to convert seconds into centuries, and I can't figure out what i'm doing wrong...
    Did you follow Salem's advice? Post your updated code with the corrections you made.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-03-2009, 04:47 PM
  2. Replies: 5
    Last Post: 08-16-2007, 11:43 PM
  3. Replies: 18
    Last Post: 11-13-2006, 01:11 PM
  4. making a program leave a msg for background program when it closes
    By superflygizmo in forum Windows Programming
    Replies: 2
    Last Post: 02-06-2006, 07:44 PM
  5. Math Equation Program (I can't find the problem with my program!)
    By masked_blueberr in forum C Programming
    Replies: 14
    Last Post: 07-06-2005, 11:53 AM