Noob Variable question

I just started fooling around in C++ yesterday and I am working on a simple code that converts seconds into minutes.

I have defined the variable for seconds and written the input/output. I am having trouble defining a variable for minutes (because I really have no idea what I'm doing at this point.)

I'm going to go ahead and post my code so that somebody may possibly point out what I'm doing wrong, as I'm sure it's a typical newbie mistake. So here it is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
using namespace std;
int main()
{
    int sec;  //seconds
    int min = sec / 60; //minutes (this is the problem)
    
  cout<< "Number of seconds:";
  cin>> sec;
  cin.ignore();
  cout<< ""<< sec <<" seconds is "<< min <<" minutes\n";
  cin.get();
  return 0;
}


I know I could go ahead and put "sec / 60" in the second cout line, but I would like to be able to define the variable "min" as "sec / 60" by default.

Can anybody please tell me how to do this?

Thanks in advance.
you have defined sec and min at the top of main(), at this point they have no value, so when you say int min = sec/60; sec has no value and therefore cannot be divided by 60 to get the calculate of number of minutes. remove that line of code from where it is now and place it below cin>>sec;
Worked perfectly. Thanks for your help.
quirkyusername wrote:
sec has no value and therefore cannot be divided by 60

It's not that it has no value. When you declare a variable without initializing it, the variable will be assigned a certain memory address, which very probably contains some garbage value. Until you initialize it, you can never be sure what that value is, and, obviously, it's an unwanted value.

@kultrva: remember that sec / 60 is integer division. So, for instance, 1 / 2 evaluates to 0, not 0.5.
@filipe

My next task was going to be to try to display "x seconds is y minutes and z seconds", 'z' being included if the number of seconds doesn't fall under a full minute. Meaning if I input 90 seconds, I would like for it to say "1 minute and 30 seconds".

Does anybody know how I could go about doing that?

I'm still brand new to programming, though, but I'm working on it.

Thanks once again.
Last edited on
Considering your seconds are stored in the variable called x, the following code would solve your problem using modulus and taking advantage of the limits of integer divisions:

cout << x << " seconds equals " << int(x/60) << " minutes with " << (x%60) << " seconds";

Why does this work? Simple.
Let's start with int(x/60): Integer division is a problem as much as it is a gift, it is relatively fast but not good to be used in most cases. However, in this situation it's literally perfect; an integer is a number within the range of 232 (equally divided signed and unsigned when the integer is signed, and only unsigned when the integer is unsigned). This means that it cannot have parts. (like 0.25, 1/3, etc) It will round them all down (aka, discard them). Thus, int(55/60) would mean that I evaluate the value of 55/60 and then cast the answer to an int. This means that it will discard the parts. Since 55/60 does not contain at least 1 "whole digit", the outcome is 0. Doing the same for int(60/60), we get 1. Doing it for int(72/60), we get 1. Doing it for int(123/60), we get 2.
On to (x%60), the % operator is referred to as the modulus operator. The modulus has different definitions on it's usage but they are mathematically equivalent. They are namely the limiting of values (mapping them back to 0) and as a result, this also defines it to be used for the returning of a "remainder value" when an integer division is performed. I will explain the latter, although the first is more mathematically correct:
Consider we have the expression 55%60 and we want to know the outcome. The computer would first try to divide by 60, when this is indeed possible, remove 60 from the first value, then check again and if possible subtract. The final outcome must be positively signed. In this case, we cannot subtract 60 without the outcome being negative (55-60 = -5). Therefor the first value is immediately returned.
Say we have 60%60 now. It's possible to subtract 60 without getting a negative outcome. 60-60 gives us 0. Since 0-60 gives a negative output, our returned result is 0. Finally, when we perform this action on 123%60, the computer first subtracts 60, which gives us 63. Then it would subtract again, and we would have our final result: 3.

Hope that was enough explanation and examples.
Last edited on
Topic archived. No new replies allowed.