0

I'm trying to write a simple program that takes input in minutes then converts that input to seconds through a function then returns the updated value.

I've tried to do a debugging and trace the values, but everything seems to be working. The problem is when I run the program in the console.

#include <iostream>
using namespace std;

int convert(int minutes);

int main() {
    //obj: write a function that takes in integer minutes and converts to secs

    // inital values
    int min = 0;

    //only allow correct input
    do {

        cout << "\t\nPlease enter a number of minutes: ";
        cin >> min;
    } while (min < 0);

    cout << "\t\nYou entered " << min << " minutes";

    //conversion
    convert(min);

    cout << ", and that is " << min << " in seconds.\n";

    system("PAUSE");
    return 0;
}

int convert(int minutes) {
    return minutes * 60;
}

For example, the user enters 5. convert(5)

I expected to get 300 back, but when the next cout runs I get:

"You entered 5 minutes, and that is 5 in seconds."

EDIT: Thank you for the help guys. I'm trying to teach myself C++ (doing these programming challenges) before I go off to school, and am very very new.

#include <iostream>
using namespace std;

int convert_Min_to_Secs(int& minutes);

int main() {
    //obj: write a function that takes in integer minutes and converts to secs

    // inital values
    int min = 0;

    //only allow correct input
    do {

        cout << "\t\nPlease enter a number of minutes: ";
        cin >> min;
    } while (min < 0);

    cout << "\t\nYou entered " << min << " minutes";

    //conversion
    int sec = convert_Min_to_Secs(min);

    cout << ", and that is " << sec << " in seconds.\n";

    system("PAUSE");
    return 0;
}

int convert_Min_to_Secs(int& minutes) {
    return minutes * 60;
}

Fixed the code with your suggestions, but I want to understand. When I initialize a primitive int min = 0. Then I allow a user to input say '2'. Then min has '4' inside of it.

When it it passed by the convert function it makes another min inside of convert? or what is happening exactly.

Nithroel
  • 23
  • 4
  • 4
    You are not using the returned value. You could write min = convert(min); – Vlad from Moscow Nov 11 '19 at 14:53
  • Call it in the cout stream or store it in a variable and print out that like you do min – Tyler Nov 11 '19 at 14:54
  • 1
    You do not store the converted value in any parameter ! Try this one: min = convert(min) – macmuri Nov 11 '19 at 14:54
  • Check this how-to: https://www.tutorialspoint.com/cprogramming/c_functions.htm – Pedro Isaaco Nov 11 '19 at 14:57
  • I'm having trouble understanding why you thought it *would* update anything. Functions don't return by reference through an argument unless you passed by reference and told them to. – underscore_d Nov 11 '19 at 14:57
  • Thank you guys. I am teaching myself C++ before I go off to school, and I'm very very new. I feel so dumb for missing min = convert(min). – Nithroel Nov 11 '19 at 15:01
  • 1
    `min = convert(min)` is bad because now `min` holds a number of seconds, not minutes... I like Paul's idea of declaring a new variable `sec` and assigning to that. Also, the function should instead have a descriptive name, for instance `convert_minutes_to_seconds()`. – underscore_d Nov 11 '19 at 15:02
  • @Nithroel You understand correctly, `min` indeed is copied unless you use reference. Your suggested code, however, still has two style issues: 1) with a simple type as int is it is not more expensive to copy than to create a reference, so it really does not make much sense to use reference performance-wise (it is a good practice with more complex types, though) 2) when you use a reference and do not intend to modify the value, it is much cleaner to make it const, ie `convertMinToSec(const &min)` so that a) everyone sees the const-ness right away b) compiler won't let you to change by accident – Mori Nov 12 '19 at 08:49

2 Answers2

0

You're passing your augment by value:

int convert(int minutes)

so minutes is copied and then you just return a value:

return minutes * 60;

so nothing changes and the new value is lost:

convert(min);

If you want to alter minutes, then either pass by reference:

void convert(int& minutes)
{
   minutes *= 60;
}

and then:

convert(min);

actually changes min to the new value. But that's rather convoluted and confusing.

So it's better to keep convert as is:

int convert(int minutes)
{
    return minutes * 60;
}

and assign the new value:

int sec = convert(min);

cout << ", and that is " << sec << " in seconds.\n";
Paul Evans
  • 27,315
  • 3
  • 37
  • 54
  • 2
    And then they'd also have to assign to it in the body. But we should promote value semantics. So it should return an `int` after all, and just *actually use* that result. But you propose to return through a reference argument *and* return an `int`. What would the latter be doing in that case? If the user just applies your suggested change, without actually assigning through the reference in the body of the function, nothing will change in the observed behaviour of their code, but now it'll be accepting a modifiable reference for no reason. – underscore_d Nov 11 '19 at 14:58
  • 1
    You should still explain that in order to really modify `minutes` (passed as a reference) one should *actually modify* it, ie write something like `minutes = minutes*60` – Mori Nov 11 '19 at 15:05
  • So if I am understanding this correctly. I made a primitive min [ 0 ] inside of it. Then when I pass convert(Min) it makes another instance of minutes [ 0 ] to the function convert. So there are two mins in memory? – Nithroel Nov 11 '19 at 15:13
  • Edited answer to make it clearer. – Paul Evans Nov 11 '19 at 15:21
  • @Nithroel Only for the function call, a copy of `min` is made and stored in `minutes`. This is ok and what you want. The value `minutes * 60` is then calculated and returned. So store this new value in, say, `sec`. – Paul Evans Nov 11 '19 at 16:01
0

Plainly speaking, you forgot to assign the return value into a new variable to print...

Lior
  • 284
  • 1
  • 6