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.