Click here to Skip to main content
15,881,938 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
So I am trying to finish an assignment which involves the user to enter the original video time as a floating point value then have the user enter the playback speed factor, again as a floating point value. Then once I have this information I will need to convert the original time into seconds then, use the factor to determine what the new video time would be. Display the results in seconds. (Note that you must use an integer data type to store the new video time.) Now as I already have mass majority of this code already done I'm confused on when it comes to converting part of the code.

C#
using System;

class Program
{
    public static void Main(string[] args)
    {
        float og_videotime, playback_speed;
        int og_videotime_seconds, new_videotime_seconds;

        Console.WriteLine("[Fast-Forward]");
        Console.Write("What is the original video time? ");
        og_videotime = float.Parse(Console.ReadLine());
        Console.Write("What is the playback speed factor? ");
        playback_speed = float.Parse(Console.ReadLine());

        // convert time to seconds
        og_videotime_seconds = (int)(og_videotime * 60);
        new_videotime_seconds = (int)(og_videotime_seconds / playback_speed);

        // space
        Console.WriteLine();

        // output
        Console.WriteLine("The new video time would be {0} second(s).", new_videotime_seconds);
        Console.WriteLine("That saves you {0} second(s) from the original video speed.", og_videotime_seconds - new_videotime_seconds);
    }
}


What I have tried:

A sample output provided: [Fast-Forward] What is the original video time? 2.30 What is the playback speed factor? 2 The new video time would be 75 second(s). That saves you 75 second(s) from the original video speed.

But my code produces: What is the original video time? 2.30 What is the playback speed factor? 2 The new video time would be 69 second(s). That saves you 69 second(s) from the original video speed.

Another Sample output: [Fast-Forward] What is the original video time? 3.59 What is the playback speed factor? 1.75 The new video time would be 136 second(s). That saves you 103 second(s) from the original video speed.

But my code produces: [Fast-Forward] What is the original video time? 3.59 What is the playback speed factor? 1.75 The new video time would be 122 second(s). That saves you 93 second(s) from the original video speed.

Math for the first sample: Now when I do the exact same number both my new video time and save seconds come out to be 69 instead of 75 which is where my confusion comes in. I know 60 * 2.3 = 138. 138 / 2 = 69. If I am correct to receive 75 I would have to do 2 * 60 = 120 then 120 + 30 = 150 then 150 / 2 to get 75 but I don't understand how I can break this down.

Thank you for all the help!
Posted
Updated 28-Jan-23 21:25pm

1 solution

A float has two parts: the integer portion (which in your case holds the minutes) and the fractional portion (which holds your seconds). Since you need an integer number of seconds as a result, start by extracting the minutes:
C#
int mins = (int) myTime;
Then get the seconds. First remove the minutes:
C#
float justSecs = myTime - mins;
Then "promote" the seconds to the integer portion:
C#
int secs = (int)(justSecs * 100.0f);
You can reduce this to two lines:
C#
int mins = (int) myTime;
int secs = (int)((myTime - mins) * 100.0f);
But I wouldn;t reduce it any further as you probably want to check if teh seconds is valid: i.e. between 0 and 59 inclusive.

Generating the total seconds is then trivial:
C#
int totalSecs = mins * 60 + secs;
And you can do whatever maths you need with that.
 
Share this answer
 
Comments
Maciej Los 30-Jan-23 13:56pm    
5ed!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900